![](/img/trans.png)
[英]InvalidOperationException when signing in to IdentityServer4 via oidc-client
[英]Sign out from OIDC client not working with IdentityServer4
我目前正在使用 IdentityServer4 开发 .NET 5 应用程序。
我使用授权码 + PKCE 流程登录 -不幸的是,注销似乎无法在 localhost 上正常工作。
我的应用程序环境如下所示:
我在 IdentityServer4 中的客户端定义如下所示:
// Authorization Code + PKCE Flow
new Client
{
ClientId = "oidcClient",
ClientName = "Example App",
ClientSecrets = { new Secret("secret".Sha256()) },
RedirectUris = { "https://localhost:44301/signin-oidc" },
PostLogoutRedirectUris = { "https://localhost:44301/signout-callback-oidc" },
AllowedGrantTypes = GrantTypes.Code,
RequirePkce = true,
RequireClientSecret = true,
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
IdentityServerConstants.StandardScopes.OfflineAccess,
"roles",
},
AllowPlainTextPkce = false,
},
我在客户端应用程序上的 OIDC 连接如下所示:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.Authority = "https://localhost:5001";
options.RequireHttpsMetadata = true;
options.ClientId = "oidcClient";
options.ClientSecret = "secret";
options.ResponseType = "code";
options.UsePkce = true;
options.ResponseMode = "query";
options.Scope.Add("offline_access");
options.Scope.Add("roles");
options.SaveTokens = true;
});
我的 WebApp HomeController 中的注销方法如下所示:
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
return new SignOutResult(new[] { OpenIdConnectDefaults.AuthenticationScheme, CookieAuthenticationDefaults.AuthenticationScheme });
}
IdentityServer4 日志告诉我登录 => 登录成功和注销 => 注销成功。
这很奇怪——应用程序一直保持登录状态。
当我注销并返回 WebApp 主页索引页面时,我仍然登录 - 尽管我应该注销。
您知道如何在 IdentityServer4 OIDC 应用程序中正确配置注销吗?
你知道如何解决这个问题吗?
Logout 方法不应该返回任何东西。 因为如果这样做,您将覆盖 SignOut 方法在内部生成的重定向。
更好的方法是这样做:
public async Task DoLogout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.