0

我正在针对 azure b2c 对 asp.net mvc 应用程序进行身份验证,遵循 startup.cs 文件代码详细信息:

public void ConfigureAuth(IAppBuilder app)
        {
            IdentityModelEventSource.ShowPII = true;
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            app.UseCookieAuthentication(new CookieAuthenticationOptions());

            app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    // Generate the metadata address using the tenant and policy information
                    MetadataAddress = String.Format(Globals.WellKnownMetadata, Globals.Tenant, Globals.DefaultPolicy),

                    // These are standard OpenID Connect parameters, with values pulled from web.config
                    ClientId = Globals.ClientId,
                    RedirectUri = Globals.RedirectUri,
                    PostLogoutRedirectUri = Globals.RedirectUri,

                    // Specify the callbacks for each type of notifications
                    Notifications = new OpenIdConnectAuthenticationNotifications
                    {
                        RedirectToIdentityProvider = OnRedirectToIdentityProvider,
                        AuthorizationCodeReceived = OnAuthorizationCodeReceived,
                        AuthenticationFailed = OnAuthenticationFailed,
                    },

                    // Specify the claim type that specifies the Name property.
                    TokenValidationParameters = new TokenValidationParameters
                    {
                        NameClaimType = "name",
                        ValidateIssuer = false

                    },

                    // Specify the scope by appending all of the scopes requested into one string (separated by a blank space)
                    Scope = $"openid profile offline_access {Globals.ReadTasksScope} {Globals.WriteTasksScope}"
                });
}

使用自定义策略时出现以下错误:IDX10501:签名验证失败。无法匹配键:孩子:'gL****************'。捕获的异常:''。令牌:typ":"JWT","alg":"RS256","kid":"gL******************"}. {"exp":1599625561," nbf":1599621961,"ver":"1.0","iss":.......}

我已验证此密钥和令牌与从https://jwt.ms获得的完全相同。当我使用自定义策略时,它唯一会抛出错误,如果我使用内置策略,它会按预期工作。

任何帮助这里缺少什么?

谢谢。

4

2 回答 2

0

经确认,您的自定义策略中的签名密钥和加密密钥存在问题。创建两者都正确解决了这个问题。

创建签名密钥

  1. 选择策略密钥,然后选择添加。
  2. 对于选项,选择生成。
  3. 在名称中,输入 TokenSigningKeyContainer。前缀 B2C_1A_ 可能会自动添加。
  4. 对于密钥类型,选择 RSA。
  5. 对于密钥用法,选择签名。
  6. 选择创建。

创建加密密钥

  1. 选择策略密钥,然后选择添加。
  2. 对于选项,选择生成。
  3. 在名称中,输入 TokenEncryptionKeyContainer。前缀 B2C_1A_ 可能会自动添加。
  4. 对于密钥类型,选择 RSA。
  5. 对于密钥使用,选择加密。
  6. 选择创建。
于 2020-09-13T12:43:03.350 回答
0

我不得不重新创建我的签名密钥,由于某种原因,现有的密钥不适用于新策略(它仍在使用现有策略)。

于 2021-11-19T03:18:46.020 回答