Authenticate Windows ASP.NET MVC as Another User

I searched all day and I cannot find a solution to this.

I am using Windows authentication for an intranet application. I want to be able to click the link and log in as another user, similar to the options in SharePoint.

I have seen many proposed .net based SO solutions, but have no specific MVC.

I came across HttpUnauthorizedResult

return new HttpUnauthorizedResult(); 

which forces the user to log in, but I do not believe that you can redirect after logging in.

I also tried

 Response.StatusCode = 401; Response.StatusDescription = "Unauthorized"; Response.SuppressContent = true; HttpContext.Current.ApplicationInstance.CompleteRequest(); 

After three attempts, I get HTTP error 401.0 - Unauthorized

This article is the most detailed that I found, however it is not specifically for mvc, and my attempts to change it failed.

http://www.roelvanlisdonk.nl/?p=825

Someone got this to work, or you can point me in the right direction.

I'm interested in a cross-browser solution, as some of our users are on iPads, etc.

+4
source share
2 answers

My solution to this problem is to create a public method (action) "SignInAsDifferentUser" and create a session variable to indicate whether I have visited this method before or not. The first visit will set the session variable (IsEntered) to true and return HttpUnauthorizedResult (). After entering the new credentials by the user, you will return to this method. But the session is already set to true. This way you will be redirected to the Index method. Hope this helps.

 public ActionResult SignInAsDifferentUser() { if ((bool)(Session["IsEntered"] ?? false)) { // already entered... Session["IsEntered"] = false; return RedirectToAction("Index", "Home"); } else { Session["IsEntered"] = true; return new HttpUnauthorizedResult(); } } 
+3
source

I needed to do this in classic ASP.net using web forms. I would suggest that the same would work in MVC. I had to create an Active Directory library for logging in as an “Interactive” user with a website set to impersonate a person. To do this, you need to access the Win32 lib calls. If you're still interested, I can create a sample in Codeplex or something like that.

0
source

Source: https://habr.com/ru/post/1501893/


All Articles