我正在尝试设置 IdTokenHint
发送注销请求时。上一篇 Microsoft.Owin.Security.OpenIdConnect
中间件我可以设置 id_token
作为 SecurityTokenValidated
中的 claim 使用 SecurityTokenValidated
的方法通过执行以下操作来通知:
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
...
Notifications = new OpenIdConnectAuthenticationNotifications
{
//Perform claims transformation
SecurityTokenValidated = async notification =>
{
...
notification.AuthenticationTicket.Identity.AddClaim(new Claim("id_token", notification.ProtocolMessage.IdToken));
},
RedirectToIdentityProvider = async n =>
{
if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
{
var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token").Value;
n.ProtocolMessage.IdTokenHint = idTokenHint;
}
}
}
}
使用新的中间件
Microsoft.AspNetCore.Authentication.OpenIdConnect
(在 ASP.NET Core RC2 中)我在尝试完成同样的事情时遇到了麻烦。我假设我应该使用 Events
像这样。app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
...
Events = new OpenIdConnectEvents()
{
OnTokenValidated = context =>
{
...
context.SecurityToken.Payload.AddClaim(new Claim("id_token", context.ProtocolMessage.IdToken));
},
OnRedirectToIdentityProviderForSignOut = context =>
{
var idTokenHint = context.HttpContext.User.FindFirst("id_token").Value;
context.ProtocolMessage.IdTokenHint = idTokenHint;
}
}
}
我看到的问题是声明没有保留在
SecurityToken
上。并且不要在 HttpContext.User
上设置.我错过了什么?
最佳答案
关于上面的代码,至少在 ASP.NET Core 2.1 版中,可以通过 context.Properties.GetTokenValue(...)
访问 ID token 。 (而不是作为用户声明)。
然而,正如 Brock Allen 在 comment to your question 中所说的那样, OpenIdConnectHandler
将自动包含 idTokenHint
登出时。然而,今天这让我咬了几个小时,除非 OpenIdConnectOptions.SaveTokens
,否则它不会保存 token 以备后用。设置为 true
.默认为 false
.
所以,如果 SaveTokens
是 true
,处理程序将自动包含 idTokenHint
,您可以手动访问 id token
通过 context.Properties.GetTokenValue(...)
.
关于claims-based-identity - 添加 id_token 作为声明 AspNetCore OpenIdConnect 中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37576666/