我試圖找到一個單一的項目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));
,我可以通過調試代碼中看到。
沒有刷新令牌存在,雖然,我不知道這是否有話要說?
編輯
我勉強我一路調試到在那裏我看到這個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]
從哪裏出發?
我建議強制設置「exchange.Credentials」,直到你已經工作了認證... 。因爲它看起來像你的問題在那裏.. – Seabizkit
exchange.Credentials = new WebCredentials(authEmailAddress,authEmailPassword,「DOMAINIFYOUHAVEONE」); – Seabizkit
@Seabizkit工作。這就是我做的開始,這工作得很好,但我想使用OAuth – Detilium