微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Exchange Web 服务 (EWS) 返回 403 Forbidden

如何解决Exchange Web 服务 (EWS) 返回 403 Forbidden

问题:几周以来,当我们尝试使用 403 Forbidden 通过 EWS 登录 Exchange Server 2019 (CU7) 时,我们收到了 independentsoft.Exchange.Service

代码

var lCredential = new NetworkCredential("MyUsername","MyPassword");
m_Service = new independentsoft.Exchange.Service("https://mail/EWS/Exchange.asmx",lCredential);
m_Service.RequestServerVersion = RequestServerVersion.Exchange2016;
FindFolderResponse lResponse = m_Service.FindFolder(StandardFolder.MailBoxRoot);

异常消息: System.Net.WebException: 'The Remote Server returned an error: (403) Forbidden.'

我们进行了以下更新:

TEMPLATE USING

我们尝试了多个不同用户的访问。但没有成功。访问我们的OWA成功。

问题:我们如何修复 403 forbidden


我们还测试了获得与 404 相同结果的日历项目:

FindItemResponse lFindItemResponse = m_Service.FindItem(StandardFolder.Calendar,AppointmentPropertyPath.AllPropertyPaths);

我们通过不同的库测试了访问:

enter image description here

。该请求似乎有效。发送电子邮件也有效:

class Program
{
    static void Main(string[] args)
    {
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
        service.Credentials = new WebCredentials("username","password");
        service.TraceEnabled = true;
        service.TraceFlags = TraceFlags.All;
        service.Url = new Uri("https://hostname/EWS/Exchange.asmx");
        EmailMessage email = new EmailMessage(service);

        // query root folder
        try
        {
            service.FindFolders(WellKNownFolderName.Root,new FolderView(100)); // throws no exception
        }
        catch (Exception e)
        {
            throw;
        }

        // send email:
        email.torecipients.add("address@hostname.com");
        email.subject = "helloworld";
        email.body = new messagebody("this is the first email i've sent by using the ews managed api");
        email.send(); // works
    }

    private static bool RedirectionUrlValidationCallback(string redirectionUrl)
    {
        // The default for the validation callback is to reject the URL.
        bool result = false;
        Uri 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;
    }
}

解决方法

通过 PowerShell 清除 EWSAllowList 可以解决问题。

显示当前配置:

[PS] C:\> Get-OrganizationConfig | select EWS*

清除列表:

[PS] C:\> Set-OrganizationConfig -EwsApplicationAccessPolicy:$null

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。