如何使会话无效?
摄制:
反正有没有使之前复制的cookie无效?
我正在使用标准的MVC5注销功能.
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Index", "Home");
}
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
Run Code Online (Sandbox Code Playgroud)
还尝试只签出cookie.
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
Run Code Online (Sandbox Code Playgroud)
想改变SecurityStamp也会有效,但由于索赔没有改变,邮票也没有.
UserManager.UpdateSecurityStampAsync(user.UserName);
Run Code Online (Sandbox Code Playgroud)
我也尝试过这个函数,文档说这应该使会话无效. http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.abandon(v=vs.110).aspx
Session.Abandon();
Run Code Online (Sandbox Code Playgroud)
Ahm*_*him -1
我不知道如何使 cookie 本身“无效”,但如果您需要的是使重新使用 cookie 的请求无效,那么您可以跟踪会话的状态,并在请求身份验证后检查此状态。
用于跟踪会话:
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
...
await SignInAsync(user, model.RememberMe);
Session["IsValid"] = true; // Tells that the session is valid
...
}
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
Session["IsValid"] = false; // The session is no longer valid
...
}
Run Code Online (Sandbox Code Playgroud)
在 Global.asax 中
protected void Session_End(Object sender, EventArgs e)
{
Session["IsValid"] = false; // Invalidate the session here too
}
protected void Application_AcquireRequestState(Object sender, EventArgs e)
{
if (Request.IsAuthenticated && // The cookie tells that the request is authenticated...
!(bool) HttpContext.Current.Session["IsValid"]) // but the session status is NOT valid
{
// Handle requests that re-use the auth cookie
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1859 次 |
| 最近记录: |