使用id_token注销OpenIDConnect AspNetCore

问题描述:

主要问题是我找不到从identityServer4注销的正确方法。使用id_token注销OpenIDConnect AspNetCore

详细说明:

客户端Web应用程序startup.cs包含以下代码

app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationScheme = "Cookies", 
      AutomaticAuthenticate = true 
     }); 
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions 
     { 
      AuthenticationScheme = "oidc", 
      SignInScheme = "Cookies", 
      Authority = "http://localhost:1941/",//local identityServer4 
      ClientId = "testsoft", 
      ClientSecret = "secret", 
      ResponseType = "code id_token token", 
      GetClaimsFromUserInfoEndpoint = true, 
      RequireHttpsMetadata = false, 
      Scope = { "openid", "profile", "email" }, 
      TokenValidationParameters = new TokenValidationParameters() 
      { 
       NameClaimType = "name", 
       RoleClaimType = "role" 
      }, 
      AutomaticAuthenticate = false, 
      AutomaticChallenge = true 
    }); 

IdentityServer4本地运行已在客户端添加如下

new Client 
      { 
       ClientId = "testsoft", 
       ClientName = "testsoft", 
       ClientSecrets = new List<Secret> 
       { 
        new Secret("secret".Sha256()) 
       }, 
       ClientUri = "http://localhost:55383/",//clientside web application url 
       AllowedGrantTypes = GrantTypes.Hybrid, 
       AllowAccessTokensViaBrowser = true, 
       RedirectUris = new List<string> 
       { 
        "http://localhost:55383/signin-oidc" 
       }, 
       RequireConsent = false, 
       AllowedScopes = new List<string> 
       { 
        StandardScopes.OpenId.Name, 
        StandardScopes.Profile.Name, 
        StandardScopes.Email.Name, 
        StandardScopes.Roles.Name, 
        StandardScopes.OfflineAccess.Name, 

        "api1", "api2", 
       }, 
      }, 

我能够登录并像这样在MVC中的Controller-View上显示声明

[Authorize] 
    public IActionResult About() 
    { 
     return View((User as ClaimsPrincipal).Claims); 
    } 

而显示的视图是这样的。需要注意的是,没有id_token

And the view displayed was like this. Note that there is no id_token

,我能使用cookie来注销下面

public async Task<IActionResult> LogOut() 
    { 

     await HttpContext.Authentication.SignOutAsync("Cookies"); 
     return Redirect("~/"); 
    } 

给出但问题是,我不能找到一种方法,从IdentityServer注销。我来的越接近使用 /connect/endsession?id_token_hint=...&post_logout_redirect_uri=https://myapp.com

但我找不到在代码中获取原始id_token的方法。在上面给出的About()方法中,我只获得了声明(我认为它是id_token的解密内容),并且在这些声明列表中没有可见的id_token。但不知何故设法从这个url http://localhost:55383/signin-oidc的提琴手获得id_token,然后在身份服务器的注销触发(借助上面给出的url)。

我有以下问题:

  1. 如何获得id_token的代码? (而不是从提琴手手动复制)
  2. 有没有更好的注销方式?或者是否有一个注销的AspnetCore/Oidc框架方法(反过来用正确的参数调用正确的服务器API)?
  3. 我能够注销并登录多次,但在提琴手上看到id_token相同。例如:Bob用户,Alice用户都有相同的id_token。 Cookie被清除,并且每次在视图上显示不同的用户时,id_token仍然是相同的。每个登录/用户的id_token不应该不同?
  4. 即使我给了一个随机字符串作为id_token,注销网址也能正常工作。这是否意味着IdentityServer4注销功能不能基于id_token工作?
+1

您是否发现回答您的问题?我面临同样的问题。 –

对于注销,你有没有试戴

HttpContext.Authentication.SignOutAsync("oidc"); 

在客户的退出行动?

+0

其实这是正确答案。您需要注销您登录的所有上下文。除了客户端配置中的登录重定向uri之外,您还可以指定注销重定向uri。有关更多详细信息,请参阅IdentityServer4文档 – Flawless