How to logout user in OWIN ASP.NET MVC5

前端 未结 6 2037
不思量自难忘°
不思量自难忘° 2020-12-31 11:30

I have got a standard AccountController class of ASP.NET MVC5 project. When I try to log out user I am facing an error coz HttpContext

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 12:15

    This worked for me

    `public void SignOut()
        {
            IOwinContext context = _context.Request.GetOwinContext();
            IAuthenticationManager authenticationManager = context.Authentication;
            authenticationManager.SignOut(AuthenticationType);
        }
    `
    

    The only problem I have is there's no redirect to Login so I get a view not found error because the view I logged out from is under an [Authorize] attribute. I thought the automatic redirect was built in when a user is not authorized by this code block...

    `app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "ApplicationCookie",
            LoginPath = new PathString("/Account/Login"),
            ExpireTimeSpan = TimeSpan.FromHours(1),
        });
    `
    

提交回复
热议问题