× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

'Login as another user' MVC 4 Windows Authentication

Leniel Macaferi
Feb 02, 2015
<p>People <a href="http://www.morgantechspace.com/2013/08/how-to-force-sign-in-as-different-user.html">reverse engineered\decompiled</a> some code from Sharepoint that happens to have this feature.</p> <p>I tested it in an <code>ASP.NET MVC 5</code> app and it's working as expected.</p> <blockquote> <p>The code is based on decompiling the Microsoft.TeamFoundation.WebAccess which has the "Sign in as a different User" function.</p> </blockquote> <pre><code>public ActionResult LogOut() { HttpCookie cookie = Request.Cookies["TSWA-Last-User"]; if(User.Identity.IsAuthenticated == false || cookie == null || StringComparer.OrdinalIgnoreCase.Equals(User.Identity.Name, cookie.Value)) { string name = string.Empty; if(Request.IsAuthenticated) { name = User.Identity.Name; } cookie = new HttpCookie("TSWA-Last-User", name); Response.Cookies.Set(cookie); Response.AppendHeader("Connection", "close"); Response.StatusCode = 0x191; Response.Clear(); //should probably do a redirect here to the unauthorized/failed login page //if you know how to do this, please tap it on the comments below Response.Write("Unauthorized. Reload the page to try again..."); Response.End(); return RedirectToAction("Index"); } cookie = new HttpCookie("TSWA-Last-User", string.Empty) { Expires = DateTime.Now.AddYears(-5) }; Response.Cookies.Set(cookie); return RedirectToAction("Index"); } </code></pre> <hr> <p>Source:</p> <p><a href="http://www.morgantechspace.com/2013/08/how-to-force-sign-in-as-different-user.html">Force Sign in as a different user while using Windows Authentication in asp.net</a></p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/17871816/'Login%20as%20another%20user'%20MVC%204%20Windows%20Authentication/25672345">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus