簡體   English   中英

天藍色。 Owin OpenId身份驗證。 添加了自定義聲明。 未調用AuthorizationCodeReceived

[英]Azure. Owin OpenId authentication. Added custom claims. AuthorizationCodeReceived is not called

我幾乎配置了我OpenId owin authentication / authorizationAzure Active Directory 我的配置如下:

 app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
 app.UseCookieAuthentication(new CookieAuthenticationOptions()
 {
     CookieName = "AppServiceAuthSession"
 });

 app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
 {

     ClientId = ClientId,
     Authority = _authority,
     PostLogoutRedirectUri = PostLogoutRedirectUri,
     RedirectUri = PostLogoutRedirectUri,
     Notifications = new OpenIdConnectAuthenticationNotifications
     {
           AuthenticationFailed = context =>
           {
                 context.HandleResponse();
                 context.Response.Redirect("/Error?message=" + context.Exception.Message);
                 return Task.FromResult(0);
           },                   
           AuthorizationCodeReceived = async context =>
           {
                 var id = new ClaimsIdentity(context.AuthenticationTicket.Identity.AuthenticationType);
                 id.AddClaims(context.AuthenticationTicket.Identity.Claims);
                 var appToken = "MyToken";
                 id.AddClaim(new Claim("MyTokenKey", appToken));

                 context.AuthenticationTicket = new AuthenticationTicket
                 (
                      new ClaimsIdentity(id.Claims, context.AuthenticationTicket.Identity.AuthenticationType),
                        context.AuthenticationTicket.Properties
                 );
           }
            },
        });

但是我想在聲明列表中添加一個應用程序令牌 (而不是用戶令牌),以便能夠在我網站的任何位置使用此令牌。 對我來說,這也是一件好事,我不需要從外部令牌提供商那里獲取此令牌,而每次身份驗證會話只需一次。

但是,僅當我使用本地IIS(在本地運行)時,才在我要添加我的邏輯( AuthorizationCodeReceived以及OpenIdConnectAuthenticationNotifications其他方法)的地方調用,而當我嘗試使用Azure IIS時,未調用此方法。完全沒有 在這種情況下,無論如何我的用戶OpenIdConnectAuthenticationNotifications身份驗證,但是不會觸發此方法和OpenIdConnectAuthenticationNotifications的類似方法( RedirectToIdentityProvider除外)。

我已經下載了Katana項目的git源代碼,並將該項目引用給了我而不是官方的nuget包,以對其進行調試。正如我目前所認為的,我已經找到了發生這種情況的原因。 AuthenticateCoreAsync方法中,從OpenIdConnectAuthenticationHandler類調用AuthorizationCodeReceived “事件”方法。 但是,還要求調用此方法,以便以下檢查必須給出真實的結果:

 if (string.Equals(Request.Method, "POST", StringComparison.OrdinalIgnoreCase)
 && !string.IsNullOrWhiteSpace(Request.ContentType) // May have media/type; charset=utf-8, allow partial match.
 && Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase)
 && Request.Body.CanRead)
 {
 //some necessary preparation to call `AuthorizationCodeReceived` event method
 } 

我們可以看到,該檢查只允許POST請求,我看到這些POST請求當我運行在本地IIS應用程序,但我不能看到這些POST請求,當我部署我在蔚藍的門戶應用程序(我已經調試兩個選項:上本地IISAzure門戶中 )。 綜上所述,這是這些運行之間的唯一區別。 (Azure的IIS不發送POST在所有的請求由於某種原因)的。任何其他方法Katana項目(這是我選中)被稱為以同樣的方式。

有人可以幫忙嗎?

PS注意,僅在清除瀏覽器數據(緩存/歷史記錄等)之后,我才會檢查所有更改。

答案如下:

驗證配置

Azure門戶中的授權應如上所示進行配置。 如果您選擇LogIn with Azure Active Directory ,則應用程序服務身份驗證將在應用程序外部進行,並且不會觸發自定義授權。

暫無
暫無

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

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