编程语言
首页 > 编程语言> > c# – Exchange Web服务(EWS)调用在ASP.NET下失败,但在控制台应用程序中失败

c# – Exchange Web服务(EWS)调用在ASP.NET下失败,但在控制台应用程序中失败

作者:互联网

当我尝试通过ASP.NET连接到Exchange Web服务时出现错误.

如果我通过控制台应用程序调用它,则以下代码可以工作,但是在ASP.NET Web表单页面上执行时,相同的代码会失败.正如旁注,我在整个代码示例中使用自己的凭据.

“将请求作为没有邮箱的帐户发出时,必须为任何可识别的文件夹ID指定邮箱主SMTP地址.”

我以为我可以通过指定模拟用户来解决问题.

exchangeservice.ImpersonatedUserId = 
    new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "email@domain.com");

但后来我得到了一个不同的错误.

“该帐户无权模拟所请求的用户.”

运行Web应用程序的App Pool也是我自己的帐户(与控制台应用程序相同),因此我不知道可能导致此问题的原因.

我正在使用.NET framework 3.5.

这是完整的代码.

var exchangeservice =
                        new ExchangeService(ExchangeVersion.Exchange2010_SP1)
                        {
                            Timeout = 10000
                        };

 var credentials = new System.Net.NetworkCredential("username", "pass", "domain");


exchangeservice.AutodiscoverUrl("email@domain.com")

FolderId rootFolderId = new FolderId(WellKnownFolderName.Inbox);

var folderView = new FolderView(100)
            {
                Traversal = FolderTraversal.Shallow
            };

            FindFoldersResults findFoldersResults =
                service.FindFolders(rootFolderId, folderView);

解决方法:

您是否尝试直接针对服务设置凭据?

this.exchangeService.Credentials = new WebCredentials("username", "pass");

this.exchangeService.AutodiscoverUrl("username", this.RedirectionUrlValidationCallback);

并使用它作为验证回调..

    /// <summary>
    /// Redirections the URL validation callback.
    /// </summary>
    /// <param name="redirectionUrl">The redirection URL.</param>
    /// <returns>true if https</returns>
    private bool RedirectionUrlValidationCallback(string redirectionUrl)
    {
        // The default for the validation callback is to reject the URL.
        var result = false;

        var redirectionUri = new Uri(redirectionUrl);

        // Validate the contents of the redirection URL. In this simple validation
        // callback, the redirection URL is considered valid if it is using HTTPS
        // to encrypt the authentication credentials. 
        if (redirectionUri.Scheme == "https")
        {
            result = true;
        }

        return result;
    }

标签:c,exchangewebservices,exchange-server
来源: https://codeday.me/bug/20190704/1372535.html