mar*_*ler 6 c# asp.net cookies asp.net-mvc asp.net-identity
我有一个带有自定义角色和用户存储的Asp.Net MVC应用程序(版本6.0.0-rc1-final).经过一番努力,我终于可以创建一个有效的登录机制.但是我现在有麻烦来创建一个干净的注销.我在控制器中的注销代码目前是什么样的:
public async Task<ActionResult> Logout()
{
if (User.Identity.IsAuthenticated)
{
await SignInManager.SignOutAsync();
}
return RedirectToAction("Index", "App");
}
Run Code Online (Sandbox Code Playgroud)
此代码的问题是,没有删除一个cookie:.AspNet.Microsoft.AspNet.Identity.Application
只要我不手动删除cookie,应用程序就处于脏状态并抛出空指针异常,因为User.Identity为null.
我在stackoverflow上找到了一个描述类似场景的问题.但是那里的解决方案并不适合我,因为我正在使用MVC 6,它不再具有System.Web.
我也有一个样本解决方案,只是工作正常.在此解决方案中,永远不会创建所提到的cookie.也许正确的解决方案不是在注销后删除cookie,而是为了防止以某种方式创建cookie.
问题是您RedirectToAction
覆盖了发出的 Identity Server 结束会话 URL 的重定向SignOutAsync
。
(微软的Haok在这里对同样的问题给出了同样的解释。)
编辑:解决方案是在对象中发送重定向 URL,AuthenticationProperties
最后SignOutAsync
:
// in some controller/handler, notice the "bare" Task return value
public async Task LogoutAction()
{
// SomeOtherPage is where we redirect to after signout
await MyCustomSignOut("/SomeOtherPage");
}
// probably in some utility service
public async Task MyCustomSignOut(string redirectUri)
{
// inject IHttpContextAccessor to get "context"
await context.SignOutAsync("Cookies");
var prop = new AuthenticationProperties()
{
RedirectUri = redirectUri
});
// after signout this will redirect to your provided target
await context.SignOutAsync("oidc", prop);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4587 次 |
最近记录: |