【发布时间】:2020-12-28 09:35:01
【问题描述】:
我正在针对 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 获得的完全相同。 当我使用自定义策略时,它唯一会抛出错误,如果我使用内置策略,它会按预期工作。
任何帮助这里缺少什么?
谢谢。
【问题讨论】:
-
MetadataAddress = String.Format(Globals.WellKnownMetadata, Globals.Tenant, Globals.DefaultPolicy)- 您是否在切换时将Globals.DefaultPolicy更新为自定义策略名称? -
是的,我已经更新了正确的策略名称,它也执行了策略。它提供了登录屏幕,但是一旦我登录用户凭据并重定向回我的 mvc 应用程序,它就会引发签名无效错误。令人惊讶的是,孩子和其他令牌详细信息是正确的(我已通过从门户运行自定义策略并在 jwt.ms 验证令牌详细信息进行验证)。
-
好的,谢谢。看起来签名密钥有些不匹配。所以,我仍然会关注你正在设置的知名
MetadataAddress。您可以发布它在string.Format之后形成的实际网址(当然是在屏蔽实际名称之后)吗?看起来像https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_sign_in/v2.0/.well-known/openid-configuration? -
是的元数据地址创建如下:tenantname.b2clogin.com/tfp/tenantname.onmicrosoft.com/…。我也尝试从 url 中删除 {tfp} 并像你一样构建,但仍然遇到同样的错误。
-
我已经重新创建了 B2C_1A_TokenEncryptionKeyContainer 和 B2C_1A_TokenSigningKeyContainer,正如提到的 [(docs.microsoft.com/en-us/azure/active-directory-b2c/…) ,并且解决了这个问题。 {tfp} 也是元数据端点的一部分,否则即使在收到有效代码后也会引发错误。
标签: c# asp.net-mvc owin azure-ad-b2c azure-ad-b2c-custom-policy