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

标签 azure azure-active-directory owin openid katana

我几乎已经配置好我的 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
                 );
           }
            },
        });

但我想在声明列表中再添加一个应用程序 token (不是用户 token ),以便能够在我网站上的任何位置使用此 token 。另外,对于我来说,我不需要在每次身份验证 session 中多次从外部 token 提供程序获取此 token ,这对我来说是件好事。

但是,只有当我使用本地 IIS(本地运行)、尝试使用 Azure IIS 时,才会调用我要添加逻辑的位置( AuthorizationCodeReceived 以及 OpenIdConnectAuthenticationNotifications 中的其他方法) ,这个方法根本就没有被调用过。在这种情况下,我的用户无论如何都经过身份验证,但此方法和 OpenIdConnectAuthenticationNotifications 中的类似方法(除 RedirectToIdentityProvider )不会被解雇。

我已经下载了Katana的git源码项目并将该项目引用到我的项目而不是官方 nuget 包来调试它,正如我目前认为的那样,我已经找到了发生这种情况的原因。 AuthorizationCodeReceivedOpenIdConnectAuthenticationHandler调用“事件”方法类(class) AuthenticateCoreAsync方法。而且,调用此方法需要以下检查必须给出 true 结果:

 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当我在azure门户中部署应用程序时发出请求(我已经调试了两个选项:在本地IISazure门户中)。 综上所述,这是这些运行之间的唯一区别。 (由于某种原因,Azure IIS 根本不发送 POST 请求)。 Katana 中的任何其他方法项目(我检查过)以相同的方式调用。

有人可以帮忙吗?

PS注意,我只有在清除浏览器数据(缓存/历史记录等)后才会检查任何更改。

最佳答案

答案如下:

Auth configuration

应按如上所示配置 azure 门户中的授权。如果您选择使用 Azure Active Directory 登录,则应用服务身份验证将在您的应用外部进行,并且不会触发自定义授权。

关于 azure 。 Owin OpenId 身份验证。添加了自定义声明。未调用 AuthorizationCodeReceived,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49759692/

相关文章:

asp.net-core - kestrel 中自托管的信号器中的最大并发请求数

azure - 如何获取 Azure 订阅经典管理员的 AD 用户?

Azure DevOps checkout 仅触发存储库

Azure 示例 - WebApp-WSFederation-DotNet

c# - Azure 事件目录 |基于角色的授权

c# - Microsoft Owin Self Host 与 Google Chrome 崩溃

asp.net-mvc - MVC .NET cookie 身份验证系统通过 token 身份验证访问 Web Api

azure - 如何删除数据工厂中的链接服务?

c# - 如何从 Windows 10 获取当前 Azure Active Directory (AAD) 用户电子邮件

.net - Azure AD 异常 Microsoft.Identity.Client.MsalServiceException : The provided value for the input parameter 'scope' is not valid