IDX12709:CanReadToken() 返回 false.JWT 格式不正确 - 打开 ID 连接身份验证



我们正在使用开放 id connect 在 mvc 应用程序中 asp.net 实现身份验证。

app.UseKentorOwinCookieSaver();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies",
CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebChunkingCookieManager()
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
RedirectUri = RedirectUri,
ResponseType = OpenIdConnectResponseType.Code,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretkey)),
ValidateIssuer = true,
ValidIssuer = authority,
ValidateAudience = true
// ValidAudience = strAudience
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
// when an auth code is received...
AuthorizationCodeReceived = (context) => {
// get the OpenID Connect code passed from Azure AD on successful auth
string code = context.Code;
var handler = new JwtSecurityTokenHandler();
var token = handler.ReadJwtToken(code);
//// successful auth
return Task.FromResult(0);
},
SecurityTokenValidated = (ctx) =>
{
// We can remove claims that are not necessary in this context, mitigating the cookie size.
var identity = ctx.AuthenticationTicket.Identity;
return Task.FromResult(0); 
},
AuthenticationFailed = (context) => {
context.HandleResponse();
return Task.FromResult(0);
}
}
}); 

身份验证成功,我能够获取代码。

我在控制器中使用授权属性。

使用 msal,我们收到了身份验证令牌和id_token。我正在获得正确的令牌,但在获得令牌后,我正在获得无限循环。我也使用UseKentorOwinCookieSaver。但没有任何效果。

授权代码不是 JWT。 使用它从 Azure AD 的令牌终结点获取 JWT。

可以为此使用 MSAL(Microsoft身份验证库(,也可以自行进行调用。 https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow

最新更新