'Login as another user' MVC 4 Windows Authentication

后端 未结 3 1949
误落风尘
误落风尘 2020-12-08 05:38

I have an intranet project written in MVC 4 which uses Windows Authentication to authorise and authenticate users.

I need to add a \'Login as another user\' function

3条回答
  •  执笔经年
    2020-12-08 06:04

    This method will always log the user out and redirect to the home page. I also added [AllowAnonymous] to make sure everybody can access this method.

        [AllowAnonymous]
        public ActionResult LogOut()
        {
            HttpCookie cookie = Request.Cookies["TSWA-Last-User"];
    
            cookie = new HttpCookie("TSWA-Last-User", string.Empty)
            {
                Expires = DateTime.Now.AddYears(-5)
            };
            Response.Cookies.Set(cookie);
    
            Response.AppendHeader("Connection", "close");
            Response.StatusCode = 401; // Unauthorized;
            Response.Clear();
    
            // redirect to home
            Response.Write("");
            Response.End();           
    
            return RedirectToAction("Index");
    
        }
    

提交回复
热议问题