![](/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.