Exchange 服务器 - 未经授权

Exchange Server - Unauthorized

我们有一个连接到 Exchange 服务器的 MVC 应用程序。我们曾经使用此代码连接到本地服务器来创建服务:

if (string.IsNullOrEmpty(Current.UserPassword))
        {
            throw new UnauthorizedAccessException("Exchange access requires Authentication by Password");
        }
        return new ExchangeService
            {
                Credentials = new NetworkCredential(Current.User.LoginName, Current.UserPassword),
                Url = new Uri(ConfigurationManager.AppSettings["ExchangeServiceUrl"]),
            };

这很好用,但现在我们的 IT 部门正在将 Exchange 服务器迁移到云端,一些用户在云服务器上,而另一些用户在本地。所以我将代码更改为:

if (string.IsNullOrEmpty(Current.UserPassword))
        {
            throw new UnauthorizedAccessException("Exchange access requires Authentication by Password");
        }
        var user = ConfigurationManager.AppSettings["ExchangeUser"];
        var password = ConfigurationManager.AppSettings["ExchangePassword"];
        var exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
        {
            Credentials = new NetworkCredential(user, password),
        };
        exchangeService.AutodiscoverUrl(Current.EmaiLuser + "@calamos.com", RedirectionCallback);
        exchangeService.Credentials = new NetworkCredential(Current.EmaiLuser + "@calamos.com", Current.UserPassword);
        return exchangeService;

我正在使用服务帐户进行自动发现(出于某种原因它不适用于普通帐户),然后我将服务的凭据更改为登录用户,以便他可以访问收件箱。问题是,随机地,服务器 returns "The request failed. The remote server returned an error: (401) Unauthorized."。 我让IT部门检查了Exchange日志,但没有任何关于这个错误的信息,所以我不知道如何解决...

那么您所说的云是指 Office365 吗?

I am using a service account to do the autodiscovery ( for some reason it doesn't work with a regular account)

对于云中的用户,您需要确保将请求发送到云服务器,或者启用跟踪 https://msdn.microsoft.com/en-us/library/office/dd633676(v=exchg.80).aspx,然后查看失败请求的路由位置。根据您的说法,您的发现将始终指向您的内部服务器,这就是为什么基于云的用户的请求会失败的原因。您需要有一种方法来识别云中的用户,然后我建议您只使用单个 Office365 端点(例如,您不需要自动发现)https//outlook.office365.com/EWS/Exchange .asmx