简体   繁体   English

在 .NET MVC 应用程序中验证 Azure AD B2C JWT

[英]Validating Azure AD B2C JWT in a .NET MVC application

I am having trouble fully understanding if my application login feature is performing as expected.我无法完全理解我的应用程序登录功能是否按预期执行。 The main problem I have is that I cannot verify if the token coming from Azure AD B2C is being validated.我遇到的主要问题是我无法验证来自 Azure AD B2C 的令牌是否经过验证。 I can log in just fine, and the application will show me the contents of the token through looping ClaimsPrincipal.Current.Claims, but I have not been able to figure out how to capture anything between logging in through my Azure AD B2C endpoint, and my MVC application accepting the token and logging me in to my MVC app.我可以正常登录,应用程序将通过循环 ClaimsPrincipal.Current.Claims 向我显示令牌的内容,但我无法弄清楚如何在通过 Azure AD B2C 端点登录之间捕获任何内容,并且我的 MVC 应用程序接受令牌并将我登录到我的 MVC 应用程序。

I started by going to Create New Project > ASP.NET Web Application (.NET) > .NET Framework 4.7.2 > Use Authentication.我首先要创建新项目 > ASP.NET Web 应用程序 (.NET) > .NET Framework 4.7.2 > 使用身份验证。 In the settings for authentication, used the following options:在身份验证设置中,使用了以下选项:

  • Work or School accounts工作或学校帐户
  • Cloud - Single Organization云 - 单一组织
  • Domain: My omnimicrosoft.com domain域:我的 omnimicrosoft.com 域

My Startup.Auth.cs has been modified a bit to look like this:我的 Startup.Auth.cs 已被修改为如下所示:

private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
    private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
    private static string wellKnownMetadata = ConfigurationManager.AppSettings["ida:WellKnownMetadataUrl"];
    private static string issuer = ConfigurationManager.AppSettings["ida:Issuer"];

    public void ConfigureAuth(IAppBuilder app)
    {         
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                MetadataAddress = wellKnownMetadata,
                ResponseType = OpenIdConnectResponseType.IdToken,
                ClientId = clientId,
                RedirectUri = postLogoutRedirectUri,
                PostLogoutRedirectUri = postLogoutRedirectUri,
                TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = "name",                        
                    ValidateLifetime = true,
                    ValidateTokenReplay = true,
                    RequireSignedTokens = true,
                    ValidIssuer = issuer,
                    ValidAudience = clientId,
                    ValidateIssuer = true,
                    ValidateAudience = true
                }                               
            });
    }

The Sign In button code is:登录按钮代码是:

 public void SignIn()
    {
        // Send an OpenID Connect sign-in request.
        if (!Request.IsAuthenticated)
        {
            HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" },
                OpenIdConnectAuthenticationDefaults.AuthenticationType);
        }
    }  

When I check the requests after logging in to my B2C endpoint, I see two entries.在登录到我的 B2C 端点后检查请求时,我看到两个条目。 One is a 302 POST to my post logout redirect URI with the id_token that I am trying to validate, and the other is a 200 response to my homepage courtesy of { RedirectUri = "/" }.一个是 302 POST 到我的 post logout 重定向 URI,带有我试图验证的 id_token,另一个是对我的主页的 200 响应,由 { RedirectUri = "/" } 提供。 The 302 response contains the token, but the 200 response is all I see in the browser. 302 响应包含令牌,但 200 响应是我在浏览器中看到的全部。 Somehow the application is capturing the token from the 302 response (I think) and redirects me to the homepage, and I never get to interact with the token.应用程序以某种方式从 302 响应中捕获令牌(我认为)并将我重定向到主页,而我从未与令牌进行交互。 I have changed the ValidIssuer value to something that is definitely not valid, but I never see any indication that the validation check failed (though it should in that case).我已将 ValidIssuer 值更改为绝对无效的值,但我从未看到任何迹象表明验证检查失败(尽管在这种情况下应该如此)。

The Notifications property for the OpenIdConnectAuthenticationOptions class of type OpenIdConnectAuthenticationNotifications enables you to handle events as the OpenID Connect messages are processed in the authentication middleware.对于通知物业OpenIdConnectAuthenticationOptions类类型的OpenIdConnectAuthenticationNotifications能够作为ID连接消息在认证中间件处理您来处理事件。

For example, the SecurityTokenReceived delegate for the OpenIdConnectAuthenticationNotifications class is invoked after the ID token is received from Azure AD B2C but before it is validated.例如, OpenIdConnectAuthenticationNotifications类的SecurityTokenReceived委托是在从 Azure AD B2C 收到 ID 令牌之后但在验证之前调用的。

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

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