【发布时间】:2015-11-25 16:30:19
【问题描述】:
如何获取隐式令牌的 id_token 以传递 id_token 提示以注销隐式流或有其他方法?我有端点/connect/endsession? id_token_hint=
不确定如何从隐含流中获取 id_token,我得到的只是 access_token 和过期时间。 IdSvr 中有设置吗?
【问题讨论】:
如何获取隐式令牌的 id_token 以传递 id_token 提示以注销隐式流或有其他方法?我有端点/connect/endsession? id_token_hint=
不确定如何从隐含流中获取 id_token,我得到的只是 access_token 和过期时间。 IdSvr 中有设置吗?
【问题讨论】:
这包含三个组成部分。
当您在 Startup.cs 中配置 OIDC 身份验证时,首先确保您从 Identity Server 请求 id_token(如上面的 @leastprivilege 所述):
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "https://localhost:44301/",
...
ResponseType = "id_token token", //(Here's where we request id_token!)
其次,使用 OIDC 通知并在验证安全令牌后,将 id_token 添加到用户的声明中:
Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = async n =>
{
var nid = new ClaimsIdentity(
n.AuthenticationTicket.Identity.AuthenticationType,
Constants.ClaimTypes.GivenName,
Constants.ClaimTypes.Role);
// get userinfo data
var userInfoClient = new UserInfoClient(
new Uri(n.Options.Authority + "/" + Constants.RoutePaths.Oidc.UserInfo),
n.ProtocolMessage.AccessToken);
var userInfo = await userInfoClient.GetAsync();
userInfo.Claims.ToList().ForEach(ui => nid.AddClaim(new Claim(ui.Item1, ui.Item2)));
// keep the id_token for logout (**This bit**)
nid.AddClaim(new Claim(Constants.TokenTypes.IdentityToken, n.ProtocolMessage.IdToken));
n.AuthenticationTicket = new AuthenticationTicket(
nid,
n.AuthenticationTicket.Properties);
},
最后,在重定向注销(也是一个通知事件)时,您将 id_token 添加到协议消息中:
RedirectToIdentityProvider = n =>
{
if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
{
var idTokenHint = n.OwinContext.Authentication.User.FindFirst(Constants.TokenTypes.IdentityToken);
if (idTokenHint != null)
{
n.ProtocolMessage.IdTokenHint = idTokenHint.Value;
}
}
return Task.FromResult(0);
}
您还需要确保在 Identity Server 中的客户端上设置 PostLogoutRedirectUris:
new Client
{
Enabled = true,
ClientName = "(MVC) Web App",
ClientId = "mvc",
Flow = Flows.Implicit,
PostLogoutRedirectUris = new List<string>
{
"https://localhost:44300/" //(** The client's Url**)
}
}
这将确保您在用户注销时可以选择返回到授权客户端 :)
所有这些都与 https://identityserver.github.io/Documentation/docsv2/overview/mvcGettingStarted.html 的 MVC 示例非常相似
比你要求的要多一点,但希望这也能帮助其他想弄清楚的人:)
【讨论】:
要获得 id_token,您必须提出要求。使用response_type=id_token token
【讨论】:
【讨论】: