繁体   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