开发者

ASP.NET MVC truly log off with Forms Authentication

开发者 https://www.devze.com 2022-12-29 17:57 出处:网络
I ha开发者_如何学Cve a logoff action on a controller as so: public ActionResult Logoff() { var x = Request.IsAuthenticated;

I ha开发者_如何学Cve a logoff action on a controller as so:

    public ActionResult Logoff()
    {
        var x = Request.IsAuthenticated;
        var y = User.Identity.IsAuthenticated;

        FormsAuthentication.SignOut();
        Session.Abandon();

        var a = Request.IsAuthenticated;
        var b = User.Identity.IsAuthenticated;

        return View();
    }

However, x, y, a, and b, are all true. So when my view renders, it still behaves as if the user is logged in. Can someone please provide a solution and/or explanation?


FormsAuthentication.SignOut() removes the authentication cookie, so you need to redirect after it instead of returning a view so that the client is notified:

public ActionResult Logoff()
{
    FormsAuthentication.SignOut();
    return RedirectToAction("Index");
}

Now in the Index action the user will no longer be authenticated.

0

精彩评论

暂无评论...
验证码 换一张
取 消