1

我正在使用OWIN OpenID Connect中間件連接到Azure AD。我能夠成功驗證用戶並重定向到回叫端點。我在這裏有點困惑,因爲我只收到id_token & codeOpenIDConnect連接到Azure AD

app.UseOpenIdConnectAuthentication(
     new OpenIdConnectAuthenticationOptions 
      { 
       AuthenticationType = "Azure AD - TEST", 
       Caption = "azure AD", 
       SignInAsAuthenticationType = signInAsType, 
       ClientId = "some guid", 
       Authority = "https://sts.windows.net/idp", 
       ResponseType = OpenIdConnectResponseTypes.CodeIdToken, 
       RedirectUri = "https://localhost:44392/ExternalLogins/Callback/", 
       AuthenticationMode = AuthenticationMode.Active, 
      }); 

回調方法:

[HttpPost] 
[Route("ExternalLogins/Callback")] 
[AllowAnonymous] 
public async Task<IHttpActionResult> ExternalLoginCallback() 
{ 
    var content = await Request.Content.ReadAsStringAsync(); 
    // I could see the content is a string with id_token, code , state etc. 

    //id_token is a JWT, so i can decode it and see the user claims and use them later 
} 

我的問題是:

  1. 僅用於驗證用戶Azure的廣告?關於授權呢?
  2. 如果我想在認證後撥打其他API,我該怎麼做,因爲我沒有access_token
  3. 我想我可以用access_token交換code,但不知道我需要撥打哪個Azure端點以獲得access_token
  4. AuthenticationMode.ActiveAuthenticationMode.Passive有什麼區別?

回答

0
  1. Azure的AD絕對可以授權用戶,讓你的訪問/刷新標記。它支持所有oAuth 2.0和OIDC流。
  2. 你需要獲得一個access token來調用api。假設您想要在MS Graph上調用一個/ get端點,您會將access token填入http請求的正文中,其前面的關鍵字爲Bearer ey...。 此外,您需要進入Azure門戶並配置您想要訪問的委派權限。
  3. 驗證碼用於交換access_token。我建議查看this protocol doc,它會告訴你如何使用所有的端點。簡短的答案是你POST到/ token端點。
  4. 有源與無源之間的差異對於SO回答有點複雜,我建議閱讀this blog post瞭解差異。

我只補充一點,如果你想看到使用Azure的AD一些示例代碼,你可以去Azure AD Dev GuideAzure AD code samples on Github