[英]Azure AD Graph API and WsFederation Authentication
我正在嘗試在Azure上托管的MVC Web應用程序中實現Azure AD Graph API。 我正確地設置了Azure AD,因為我能夠在去年/今年下半年的某個時刻對其進行更新之前,在去年的舊版本中使用Graph API。
我正在按照https://github.com/AzureADSamples/WebApp-GraphAPI-DotNet上的說明進行操作,並使用更新的代碼。 這兩個項目之間的區別是我使用的是WsFed,而不是OpenID,因此某些部分有所不同,即Startup.Auth.cs。 這是此示例項目中的相關代碼(請參見此處 ):
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(clientId, appKey);
string userObjectID = context.AuthenticationTicket.Identity.FindFirst(
"http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectID));
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
AuthenticationHelper.token = result.AccessToken;
return Task.FromResult(0);
}
}
但是,由於我的網站設置為必須通過WS-Fed登錄才能訪問該網站上的所有內容,因此我嘗試在Startup.Auth.cs中獲得一個令牌。 這樣我以后可以簡單地使用AcquireTokenSilent。 我在這里使用該項目https://github.com/AzureADSamples/WebApp-WSFederation-DotNet來設置WS-Fed。
Startup.Auth.cs中的問題是我無權訪問AuthorizationCodeReceived選項,只有SecurityTokenReceived和SecurityTokenValidated。 這些都不是訪問代碼或以后可在我的應用程序中查詢Graph API的任何方法的好選擇。 我該怎么做呢? 任何指導將不勝感激。
不幸的是,WS-Federation協議沒有客戶端和訪問令牌的任何概念-唯一被交易的令牌是發送給您的Web登錄令牌,並且沒有生成授權代碼。 如果您需要調用Graph API,我強烈建議您切換到OpenId Connect(使用上面報告的邏輯來處理訪問令牌的獲取)。 如果您絕對不能從ws-fed中退出,則需要手動執行OAuth2流。 實際上,這意味着從https://github.com/AzureADSamples/WebApp-WebAPI-OAuth2-AppIdentity-DotNet或https://github.com/AzureADSamples/WebApp-WebAPI-OAuth2-UserIdentity-DotNet中獲取代碼並粘貼它放在您應用的頂部。 那不是一個很明確的任務,這就是為什么我堅持我的建議以利用OpenId Connect提供的集成流程的原因。 HTHV。
我使用以下方法設法獲得了Microsoft Graph訪問令牌:使用以下參數對應用程序的oauth2 / token端點https://login.microsoftonline.com/{tenantId}/oauth2/token
執行服務器端POST:
grant_type=client_credentials
&client_id=<clientId>
&client_secret=<clientSecret>
&resource=https://graph.microsoft.com
在上面, <clientSecret>
是通過Azure管理門戶生成的有效應用程序密鑰。
此處描述的方法: https : //graph.microsoft.io/en-us/docs/authorization/app_only
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.