【问题标题】:does the SecurityTokenValidated callback automatically validate the token using Owin MiddlewareSecurityTokenValidated 回调是否使用 Owin 中间件自动验证令牌
【发布时间】:2017-01-13 11:19:19
【问题描述】:

我正在使用 Azure Ad 并已按如下方式设置了我的 Startup.Auth.cs 文件 我能够连接并使用 Azure、Google、MS 和 Linked in 来成功进行身份验证,我收到了 id_token 回复,但我希望能够验证从 Azure 收到的这个令牌,但我不确定如何.引发的SecurityTokenValidated 事件是否意味着令牌已经针对我定义的TokenValidationParameters 进行了验证,并且我不需要验证令牌?如果是这种情况,我应该在TokenValidationParameters 中输入什么?

我收到的 id_token 不包含要验证的加密签名

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    SlidingExpiration = true,
                    LoginPath = new PathString("/"),
                    CookieSecure = CookieSecureOption.Always,


                });

            var options = new OpenIdConnectAuthenticationOptions
            {

                Authority = "https://login.windows.net/common",
                ClientId = clientId,
                RedirectUri = redirectUri,
                PostLogoutRedirectUri = postLogoutRedirectUri,
                Notifications = new OpenIdConnectAuthenticationNotifications
                {

                    AuthenticationFailed = AuthenticationFailed,
                    RedirectToIdentityProvider = OnRedirectToIdentityProvider,
                    SecurityTokenReceived = OnSecurityTokenReceived,
                    AuthorizationCodeReceived = OnAuthorizationCodeReceived,
                    SecurityTokenValidated = OnSecurityTokenValidated,
                    MessageReceived = OnMessageReceived
                },
                Scope = "openid",
                ResponseType = "id_token",
                Description = new AuthenticationDescription
                {

                    AuthenticationType = "OpenIdConnect",
                                        },

                ConfigurationManager = new PolicyConfigurationManager(
                    string.Format(CultureInfo.InvariantCulture, aadInstance, tenant, "/v2.0", OidcMetadataSuffix),
                    new[] { SisuGoogle, SisuLinkedIn, SisuMicrosoft, SisuLocal, ResetPasswordLocalPolicyId }),


                TokenValidationParameters = new TokenValidationParameters
                {
                    ValidAudiences = new string[]
                    {
                     "http://localhost:44330/",


                    },
                    IssuerSigningKey = GetSecurityKey(),
                    // If you don't add this, you get IDX10205
                    //ValidateIssuer = false,
                },
            };

            app.UseOpenIdConnectAuthentication(options);



 private SecurityKey GetSecurityKey()
        {
            var securityKey = "secure key";
            var signingKey = new InMemorySymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey));
            var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.RsaSha256Signature,SecurityAlgorithms.Sha256Digest);
            return signingCredentials.SigningKey;
        }


    private Task OnSecurityTokenValidated(SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> arg)
            {


//do I need to validate the token here or has it already been validated??

//if I have to validate it then how do I? I've tried the following but does not work
                var tokenValidationParameters = new TokenValidationParameters
                {
                    IssuerSigningKey = GetSecurityKey()
                };

                SecurityToken validatedToken;
                var jwtHandler = new JwtSecurityTokenHandler();


    //crashes at this point
                jwtHandler.ValidateToken(arg.ProtocolMessage.IdToken, tokenValidationParameters, out validatedToken);



                return Task.FromResult(0);
            }

【问题讨论】:

  • 我也有同样的问题。你最后做了什么?
  • @Mukus 我最终使用密码学构建了自己的验证。从那以后我就离开了那个项目。但是,我相信只有在成功验证令牌时才会引发回调,这意味着是的,在引发此回调时,令牌已经得到验证。但是,当时我找不到任何文件来支持这一点。文档现在可能已更改

标签: c# azure owin openid-connect azure-ad-b2c


【解决方案1】:

您可以关注此示例:https://github.com/Azure/azure-content/blob/master/articles/active-directory-b2c/active-directory-b2c-devquickstarts-api-dotnet.md

或者看看这个类似的问题: https://social.msdn.microsoft.com/Forums/en-US/893a6142-1508-4aa2-9da3-dab3b1f1a6b9/b2c-jwt-token-signature-validation?forum=WindowsAzureAD

如果您在示例中使用类似的配置,则 OWIN 将使用从元数据端点获取的密钥来处理令牌验证。

【讨论】:

    【解决方案2】:

    有点晚了,但可以在此处找到各种事件的文档:

    OpenIdConnectAuthenticationNotifications

    您可以与通知挂钩的事件是:

    • 身份验证失败
      如果在请求处理期间抛出异常,则调用。除非被抑制,否则将在此事件之后重新抛出异常。

    • AuthorizationCodeReceived 如果协议消息中存在授权代码,则在安全令牌验证后调用。

    • 收到消息
      在第一次收到协议消息时调用。

    • RedirectToIdentityProvider
      调用以操纵对 SignIn、SignOut 或 Challenge 的身份提供者的重定向。

    • SecurityTokenReceived 使用从协议消息中提取的安全令牌调用。

    • SecurityTokenValidated
      在安全令牌通过验证并生成 ClaimsIdentity 后调用。

    • TokenResponseReceived 在令牌端点兑换“授权码”后调用。

    可以在此处找到使用其中一些的更新示例:Azure AD B2C: Call an ASP.NET Web API from an ASP.NET Web App

    正如Tutorial: Add sign-in to Microsoft to an ASP.NET web app 的高级选项中所述,您可以通过多种方式进一步限制谁可以在通过身份验证后访问您的应用程序。

    【讨论】:

      猜你喜欢
      • 2014-09-25
      • 2018-11-25
      • 1970-01-01
      • 2020-03-29
      • 2016-08-06
      • 2017-08-25
      • 2017-10-01
      • 2019-01-30
      • 2020-07-25
      相关资源
      最近更新 更多