2016-04-19 41 views
3

问题是什么?如何通过当前Windows用户凭据连接到Exchange Server?

我们在VS2013中安装了Exchange Server 2010 SP2,IIS 7.5和控制台应用程序。

所以我们做了以下内容:

TimeZoneInfo timeZone = TimeZoneInfo.Local; 
ExchangeService EWSservice = new ExchangeService(ExchangeVersion.Exchange2010_SP2, timeZone); 
//EWSservice.Url = new Uri("our url"); 
EWSservice.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback); 

两种方式来定义的URL都在工作。 试图设置凭据:

EWSservice.Credentials = new WebCredentials("[email protected]", "mypass"); 

这也适用。但我想通过使用当前的Windows用户凭据进行连接。已经使用和崩溃的一些代码示例:

  • EWSservice.Credentials = new WebCredentials(CredentialCache.DefaultCredentials);

  • EWSservice.UseDefaultCredentials = true;

  • EWSservice.Credentials = (NetworkCredential)CredentialCache.DefaultCredentials;

所有这一切都返回401错误:未经授权

此外,我已编辑的app.config这样的 - 但它不工作:

<authentication mode="Windows"> 
    </authentication> 
    <identity impersonate="false"/> 
    <!--Other stuff--> 
</authentication> 

然后,我看了一下认证方式,发现这一点: Windows User getting "access denied" from exchange server

管理有设置这个选项代表,但连接仍然没有工作...

现在我已经加入此:

EWSservice.TraceEnabled = true;

和我在控制台的交换响应:

HTTP/1.1 401 Unauthorized 
Content-Length: 0 
Date: Wed, 20 Apr 2016 07:59:01 GMT 
Set-Cookie: exchangecookie=bfab7cdda62a4bf8be5b43689199fcf6; expires=Thu, 20-Apr 
-2017 07:59:02 GMT; path=/; HttpOnly 
Server: Microsoft-IIS/7.5 
WWW-Authenticate: Basic realm="our-domain" 
X-Powered-By: ASP.NET 

任何想法?

+0

所以是应用失败或你想做到这一点在ASP控制台。净?你可能想看看fiddler中的一些请求,并看看它试图使用的Authentication头(例如Basic或NTLM)。这是旧的,但考虑到EWS托管API仍然使用Httpwebrequest仍然相对https://blogs.msdn.microsoft.com/buckh/2004/07/28/authentication-in-web-services-with-httpwebrequest/ –

+0

它的发生在控制台App中。使用HttpWebRequest也会返回401错误 –

回答

0

我不认为你的做法是可行的,但你可能想看看这个 ....

How to get the current user's Active Directory details in C#

+0

我会从AD得到什么样的细节?用户邮件已知 –

+0

您的问题是:如何获取当前用户凭据,我误会了? 如果您已经拥有当前的用户邮件,那么我认为您正在以错误的方式处理问题。 您需要一个具有模拟 其他帐户https:// msdn的权限的SINGLE帐户凭据。microsoft.com/en-us/library/office/dd633680(v=exchg.80).aspx 通过这样做你只需要有一个用户名和密码,然后如果你要访问别人的邮箱,所有你需要的是邮件 – rojobo

相关问题