简体   繁体   English

天蓝色。 Owin OpenId身份验证。 添加了自定义声明。 未调用AuthorizationCodeReceived

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

I've almost configured my OpenId owin authentication / authorization in Azure Active Directory . 我几乎配置了我OpenId owin authentication / authorizationAzure Active Directory My configuration is the following: 我的配置如下:

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

But I want to add one more application token (not user token) to claims list to be able to have ability to use this token in any place on my site. 但是我想在声明列表中添加一个应用程序令牌 (而不是用户令牌),以便能够在我网站的任何位置使用此令牌。 Also it's good point for me that I don't need to get this token from my external token provider more then one time per an authentication session. 对我来说,这也是一件好事,我不需要从外部令牌提供商那里获取此令牌,而每次身份验证会话只需一次。

But place, where I'm going to add my logic ( AuthorizationCodeReceived as well as other methods from OpenIdConnectAuthenticationNotifications ) is called only when I use my local IIS(run locally), when I try to use azure IIS, this method has not been called at all. 但是,仅当我使用本地IIS(在本地运行)时,才在我要添加我的逻辑( AuthorizationCodeReceived以及OpenIdConnectAuthenticationNotifications其他方法)的地方调用,而当我尝试使用Azure IIS时,未调用此方法。完全没有 In this case my User is authenticated anyway, but this method and the similar methods from OpenIdConnectAuthenticationNotifications (except RedirectToIdentityProvider ) are not fired. 在这种情况下,无论如何我的用户OpenIdConnectAuthenticationNotifications身份验证,但是不会触发此方法和OpenIdConnectAuthenticationNotifications的类似方法( RedirectToIdentityProvider除外)。

I've downloaded the git source code of Katana project and referenced this project to my instead of the official nuget packages to debug its and as I think currently, I've found the reason why it happens. 我已经下载了Katana项目的git源代码,并将该项目引用给了我而不是官方的nuget包,以对其进行调试。正如我目前所认为的,我已经找到了发生这种情况的原因。 The AuthorizationCodeReceived "event" method is called from OpenIdConnectAuthenticationHandler class in AuthenticateCoreAsync method. AuthenticateCoreAsync方法中,从OpenIdConnectAuthenticationHandler类调用AuthorizationCodeReceived “事件”方法。 But also, the calling of this method is required that the below checking must give the true result: 但是,还要求调用此方法,以便以下检查必须给出真实的结果:

 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
 } 

As we can see, this checking allows only POST requests and I see these POST requests when I run app in local IIS , but I cannot see these POST requests when I deploy my application in azure portal (I've debugged both of options : on local IIS and in azure portal ). 我们可以看到,该检查只允许POST请求,我看到这些POST请求当我运行在本地IIS应用程序,但我不能看到这些POST请求,当我部署我在蔚蓝的门户应用程序(我已经调试两个选项:上本地IISAzure门户中 )。 As summary from the above, this is the only one difference between these runnings. 综上所述,这是这些运行之间的唯一区别。 (Azure IIS doesn't send POST request at all by some reason).Any other methods in Katana project (which I checked) are called in the same way. (Azure的IIS不发送POST在所有的请求由于某种原因)的。任何其他方法Katana项目(这是我选中)被称为以同样的方式。

Could anybody help with it? 有人可以帮忙吗?

PS Note, I check any changes only after clearing of browser data (cache/history and so on). PS注意,仅在清除浏览器数据(缓存/历史记录等)之后,我才会检查所有更改。

The answer is the following: 答案如下:

验证配置

The authorization in azure portal should be configured as shown above. Azure门户中的授权应如上所示进行配置。 In case if you chose LogIn with Azure Active Directory , then app services auth takes place outside of your app, and the custom authorization is not triggered. 如果您选择LogIn with Azure Active Directory ,则应用程序服务身份验证将在应用程序外部进行,并且不会触发自定义授权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM