Ale*_*dro 8 .net c# asp.net exchange-server office365api
我有一个Web应用程序调用EWS托管API连接到office365.
我在MSDN上关注了EWS Managed API 2.0客户端应用程序入门文档.
在web.config我指定的代理pac:
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="false">
<proxy autoDetect="False" bypassonlocal="True" scriptLocation="http://example.com:8080/proxy.pac" usesystemdefault="False" />
</defaultProxy>
</system.net>
[...]
</configuration>
Run Code Online (Sandbox Code Playgroud)
我尝试以下列方式连接到Exchange:
public static ExchangeService getExchangeService(String username)
{
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
service.Credentials = new WebCredentials(USER_365, PWD_365, DOMAIN_365);
service.UseDefaultCredentials = true;
//I've tried both WebProxy settings, this:
service.WebProxy = WebRequest.GetSystemWebProxy();
//And this (with no success):
//service.WebProxy = WebRequest.DefaultWebProxy;
//I've also tried Autodiscover...
service.AutodiscoverUrl(USER_365, RedirectionUrlValidationCallback);
//...and direct url
//service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, username);
return service;
}
Run Code Online (Sandbox Code Playgroud)
以下是从MSDN复制和粘贴的方法:
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;
}
private static bool CertificateValidationCallBack(object sender,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
// If the certificate is a valid, signed certificate, return true.
if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
{
return true;
}
// If there are errors in the certificate chain, look at each error to determine the cause.
if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
{
if (chain != null && chain.ChainStatus != null)
{
foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
{
if ((certificate.Subject == certificate.Issuer) &&
(status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
{
// Self-signed certificates with an untrusted root are valid.
continue;
}
else
{
if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
{
// If there are any other errors in the certificate chain, the certificate is invalid,
// so the method returns false.
return false;
}
}
}
}
// When processing reaches this line, the only errors in the certificate chain are
// untrusted root errors for self-signed certificates. These certificates are valid
// for default Exchange server installations, so return true.
return true;
}
else
{
// In all other cases, return false.
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我试图评论出这条线:
//service.Url = new Uri("https://outlook.office365.com/ews/Exchange.asmx");
Run Code Online (Sandbox Code Playgroud)
并添加自动发现:
service.AutodiscoverUrl(username);
Run Code Online (Sandbox Code Playgroud)
是否设置代理,注释掉该行:
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
Run Code Online (Sandbox Code Playgroud)
但似乎ExchangeService直接调用服务器而没有通过代理...我错过了什么?
谢谢
尝试从 web.config 中的代理配置中完全删除bypassonlocal 属性。将此属性与 scriptLocation 一起设置时存在问题。
此外,web.config 默认代理配置应该足够了,以便您可以删除代码中的任何代理设置。
也许这里就是这种情况。
更新:在 web.config 中指定 pac 脚本位置而proxyaddress不是属性应该可以解决此问题。scriptLocation
| 归档时间: |
|
| 查看次数: |
781 次 |
| 最近记录: |