簡體   English   中英

EWS:“遠程服務器返回錯誤(401)未經授權”

[英]EWS: “The remote server returned error (401) Unauthorized”

我正在嘗試從當前上下文中的所有項目中查找單個項目,但是我似乎不斷收到以下錯誤消息:

請求失敗。 遠程服務器返回錯誤:(401)未經授權。

首先,我設置了一切以訪問交換服務:

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));

正如我通過調試代碼所看到的。

在此處輸入圖片說明

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

編輯

我只是設法調試了所有進入exchange.ResponseHeaders的地方:

使用太弱而無法允許對此應用程序訪問的身份驗證方法來獲取訪問令牌。 顯示的身份驗證強度為1,要求為2

解碼了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]

然后去哪兒?

我過去在使用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/zh-cn/office/office365/howto/building-service-apps-in- office-365

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM