2015-10-19 1042 views
5

我試圖找到一個單一的項目fronm在目前情況下的所有項目,但我似乎不斷收到此錯誤信息:EWS:「遠程服務器返回錯誤(401)未授權」

The request failed. The remote server returned an error: (401) Unauthorized.

首先,我把一切都弄好訪問Exchange服務:

var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; 
var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 

AuthenticationResult authenticationResult = null; 
AuthenticationContext authenticationContext = new AuthenticationContext(
      SettingsHelper.Authority, new model.ADALTokenCache(signInUserId)); 

authenticationResult = authenticationContext.AcquireToken(
      SettingsHelper.ServerName, 
      new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret)); 

ExchangeService exchange = new ExchangeService(ExchangeVersion.Exchange2013); 
exchange.Url = new Uri(SettingsHelper.ServerName + "ews/exchange.asmx"); 
exchange.TraceEnabled = true; 
exchange.TraceFlags = TraceFlags.All; 
exchange.Credentials = new OAuthCredentials(authenticationResult.AccessToken); 

然後我定義我希望收到(通過ID)什麼項目:

ItemView view = new ItemView(5); 
view.PropertySet = new PropertySet(BasePropertySet.IdOnly); 

var tempId = id.Replace('-', '/').Replace('_', '+'); 
SearchFilter.IsEqualTo searchid = new SearchFilter.IsEqualTo(ItemSchema.Id, tempId); 

最後但並非最不重要的,我嘗試搜索這個項目,我的項目中:

FindItemsResults<Microsoft.Exchange.WebServices.Data.Item> results = exchange.FindItems(WellKnownFolderName.Inbox, searchid, view); 

這是我的錯誤發生。我嘗試了各種其他方式來做到這一點,但無論我做什麼,我都會被授權。

有人可能以正確的方式引導我,爲了解決這個問題?

編輯

我從收到的訪問令牌:

authenticationResult = authenticationContext.AcquireToken(
      SettingsHelper.ServerName, 
      new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret)); 

,我可以通過調試代碼中看到。

enter image description here

沒有刷新令牌存在,雖然,我不知道這是否有話要說?

編輯

我勉強我一路調試到在那裏我看到這個exchange.ResponseHeaders

The access token is acquired using an authentication method that is too weak to allow access for this application. Presented auth strength was 1, required is 2

decoded the JWT,因爲這是我的結果:

{ 
    typ: "JWT", 
    alg: "RS256", 
    x5t: "MnC_VZcATfM5pOYiJHMba9goEKY", 
    kid: "MnC_VZcATfM5pOYiJHMba9goEKY" 
}. 
{ 
    aud: "https://outlook.office365.com/", 
    iss: "https://sts.windows.net/d35f5b06-f051-458d-92cc-2b8096b4b78b/", 
    iat: 1445416753, 
    nbf: 1445416753, 
    exp: 1445420653, 
    ver: "1.0", 
    tid: "d35f5b06-f051-458d-92cc-2b8096b4b78b", 
    oid: "c5da9088-987d-463f-a730-2706f23f3cc6", 
    sub: "c5da9088-987d-463f-a730-2706f23f3cc6", 
    idp: "https://sts.windows.net/d35f5b06-f051-458d-92cc-2b8096b4b78b/", 
    appid: "70af108f-5c8c-4ee4-a40f-ab0b6f5922e0", 
    appidacr: "1" 
}. 
[signature] 

從哪裏出發?

+0

我建議強制設置「exchange.Credentials」,直到你已經工作了認證... 。因爲它看起來像你的問題在那裏.. – Seabizkit

+0

exchange.Credentials = new WebCredentials(authEmailAddress,authEmailPassword,「DOMAINIFYOUHAVEONE」); – Seabizkit

+0

@Seabizkit工作。這就是我做的開始,這工作得很好,但我想使用OAuth – Detilium

回答

4

我而過去使用EWS已經得到這個錯誤「的訪問令牌獲取使用太弱的驗證方法,以允許該應用程序訪問。顯示身份驗證強度爲1,需要的是2」

你需要做的是用證書強制你的認證。

AuthenticationContext authContext = new AuthenticationContext(authority); 

exchangeService.Credentials = new OAuthCredentials(authContext.AcquireToken("https://outlook.office365.com", new ClientAssertionCertificate(ConfigurationManager.AppSettings["ida:ClientId"], certificate)).AccessToken); 

關鍵部分是在ClientAssertion中定義一個新的ClientAssertionCertificate。

您還必須修改Azure Active Directory應用程序的清單。

只看該參考(約的「配置X.509證書公衆對你的應用程序」的部分):https://msdn.microsoft.com/en-us/office/office365/howto/building-service-apps-in-office-365

+0

您可以編輯您的答案並解釋更多關於此證書的原因需要?是否因爲應用程序需要信任運行該服務的PC,因爲該服務的訪問範圍非常廣泛? – Detilium

+0

除了建立比僅具有應用祕密的信任更安全的信任以外,我不是其他原因。我認爲「竊取/知道」祕密(字符串)比à證書更容易。這是EWS讓你打電話的唯一方法。 – Trysior

+0

你得到我的+50代表,因爲這是唯一的答案,並且有些正確的答案,但我希望你能告訴我更多關於證書的信息。我設法將錯誤從'401:未經授權'更改爲'403:禁止',我相信這與AAD的許可有關。謝謝你的答案,並享受你的50個代表:) – Detilium

相關問題