2016-04-19 86 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

相關問題