问题描述
问题:几周以来,当我们尝试使用 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.'
我们进行了以下更新:
我们尝试了多个不同用户的访问。但没有成功。访问我们的OWA成功。
问题:我们如何修复 403 forbidden
?
我们还测试了获得与 404 相同结果的日历项目:
FindItemResponse lFindItemResponse = m_Service.FindItem(StandardFolder.Calendar,AppointmentPropertyPath.AllPropertyPaths);
我们通过不同的库测试了访问:。该请求似乎有效。发送电子邮件也有效:
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