SignOutAsync 删除 cookie 但在返回客户端应用程序时重新生成?
SignOutAsync deletes cookie but is regenerated when returning to client application?
这是在 api:
中完成注销的方式
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Logout(LogoutRequest logoutContext)
{
if (User?.Identity.IsAuthenticated == true)
{
var prop = new AuthenticationProperties
{
RedirectUri = logoutContext.PostLogoutRedirectUri
};
await HttpContext.SignOutAsync(IdentityConstants.ApplicationScheme, prop);
await _events.RaiseAsync(new UserLogoutSuccessEvent(User.GetSubjectId(), User.GetDisplayName()));
}
}
在客户端应用程序中,我有以下配置:
services.AddAuthentication(options =>
{
options.DefaultScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = "oidc";
})
.AddCookie(IdentityConstants.ApplicationScheme, options => { options.AccessDeniedPath = "/Home/AccessDenied"; })
.AddOpenIdConnect("oidc", options =>
{
options.Authority = model.Authority;
options.ClientId = model.ClientId;
options.SignInScheme = IdentityConstants.ApplicationScheme;
options.ResponseType = "id_token token";
options.Scope.Add("openid profile");
});
services.AddAuthorization(options =>
{
options.AddPolicy("User", p => p.RequireAuthenticatedUser().RequireRole("User"));
});
当我注销时,cookie 被删除,一旦我导航回客户端,系统不会提示我重新登录并且 cookie 已返回。我做错了什么吗?
您没有删除在您的身份提供商中生成的单点登录 cookie。您的客户端正在重定向到您的身份提供者,并使用新的身份验证再次重定向到您的客户端,因为您的身份提供者仍然维护着 Cookie。在 Fiddler 中捕获请求以查看自动重定向。
您也应该退出 oidc 以删除此 Cookie:
await HttpContext.SignOutAsync("oidc");
如果您想自动注销(没有身份提供者的注销视图),您可以在 AccountOptions.cs 文件中设置 false ShowLogoutPrompt
并设置 true AutomaticRedirectAfterSignOut
。
这是在 api:
中完成注销的方式 [HttpPost]
[ValidateAntiForgeryToken]
public async Task Logout(LogoutRequest logoutContext)
{
if (User?.Identity.IsAuthenticated == true)
{
var prop = new AuthenticationProperties
{
RedirectUri = logoutContext.PostLogoutRedirectUri
};
await HttpContext.SignOutAsync(IdentityConstants.ApplicationScheme, prop);
await _events.RaiseAsync(new UserLogoutSuccessEvent(User.GetSubjectId(), User.GetDisplayName()));
}
}
在客户端应用程序中,我有以下配置:
services.AddAuthentication(options =>
{
options.DefaultScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = "oidc";
})
.AddCookie(IdentityConstants.ApplicationScheme, options => { options.AccessDeniedPath = "/Home/AccessDenied"; })
.AddOpenIdConnect("oidc", options =>
{
options.Authority = model.Authority;
options.ClientId = model.ClientId;
options.SignInScheme = IdentityConstants.ApplicationScheme;
options.ResponseType = "id_token token";
options.Scope.Add("openid profile");
});
services.AddAuthorization(options =>
{
options.AddPolicy("User", p => p.RequireAuthenticatedUser().RequireRole("User"));
});
当我注销时,cookie 被删除,一旦我导航回客户端,系统不会提示我重新登录并且 cookie 已返回。我做错了什么吗?
您没有删除在您的身份提供商中生成的单点登录 cookie。您的客户端正在重定向到您的身份提供者,并使用新的身份验证再次重定向到您的客户端,因为您的身份提供者仍然维护着 Cookie。在 Fiddler 中捕获请求以查看自动重定向。
您也应该退出 oidc 以删除此 Cookie:
await HttpContext.SignOutAsync("oidc");
如果您想自动注销(没有身份提供者的注销视图),您可以在 AccountOptions.cs 文件中设置 false ShowLogoutPrompt
并设置 true AutomaticRedirectAfterSignOut
。