Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sign in as different user when using Integrated Windows Authentication

I have restricted access to a site by using Integrated Windows Authentication and turning off anonymous access. This way I can then show them their real name (from looking up on Active Directory and using the server variable LOGON_USER) and do other related Active Directory tasks.

How can I then prompt again for their user credentials, through a 'sign in as other user' link , showing the browser prompt (like you would get on a browser like Chrome or Firefox, or if the site was not in the 'Intranet' zone in IE) rather than a Web Form?

Since SharePoint offers this functionality, I assume there is a way to do this through code, but I don't know what code can do this (using C#). I can send a 401 header which makes the prompt appear, but how do you then confirm if they are logged in?

like image 767
SamWM Avatar asked May 04 '10 15:05

SamWM


2 Answers

Maybe this can help you out.

ASP .NET – C# – How to “Sign in as Different User” like in Microsoft SharePoint with Windows Authentication

like image 150
Mark Coleman Avatar answered Nov 15 '22 19:11

Mark Coleman


Try this approach. It is based on disassembled code of the method Microsoft.SharePoint.ApplicationPages.AccessDeniedPage.LogInAsAnotherUser()

First of all, I'm accessing the AccessDeniedPage page using javascript because Sharepoint does something similar:

function GoToSignAs() {
    window.location.replace("./SignAs.aspx?signAs=true&returnUrl=" + window.location.toString());
}

<a onclick="GoToSignAs(); return false;" href="javascript:;">SignAs</a>

Then, in your page AccessDeniedPage you use this:

public partial class SignAs : Page
{
    private const string LoginAttempts = "LoginAttempts";

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        HttpContext current = HttpContext.Current;
        if (current == null)
        {
            throw new InvalidOperationException();
        }
        if (GetUrlParameter<bool>("signAs"))
        {
            HandleSignAs(current, GetUrlParameter<string>("returnUrl"));
        }
    }

    // ...

    private static void HandleSignAs(HttpContext context, string returnUrl)
    {
        int attempts = 0;
        HttpCookie attemptsCookie = context.Request.Cookies[LoginAttempts];
        if (attemptsCookie == null || string.IsNullOrEmpty(attemptsCookie.Value))
        {
            attemptsCookie = new HttpCookie(LoginAttempts);
        }
        else
        {
            attempts = int.Parse(attemptsCookie.Value, CultureInfo.InvariantCulture);
        }

        if (!string.IsNullOrEmpty(context.Request.Headers["Authorization"]))
        {
            // Attempts are counted only if an authorization token is informed.
            attempts++;
        }

        if (attempts>1)
        {
            attemptsCookie.Value = string.Empty;
            context.Response.Cookies.Add(attemptsCookie);
            context.Response.Redirect(returnUrl, true);
        }
        else
        {
            attemptsCookie.Value = attempts.ToString(CultureInfo.InvariantCulture);
            context.Response.Cookies.Add(attemptsCookie);
            SendEndResponse(context, 401, "401 Unauthorized");
        }
    }

    private static void SendEndResponse(HttpContext context, int code, string description)
    {
        HttpResponse response = context.Response;
        context.Items["ResponseEnded"] = true;
        context.ClearError();

        response.StatusCode = code;
        response.Clear();
        response.StatusDescription = description;

        response.AppendHeader("Connection", "close");
        response.AddHeader("WWW-Authenticate", "Negotiate");
        response.AddHeader("WWW-Authenticate", "NTLM");

        response.End();
    }
}

FIX: you must use the IIS to work properly

like image 42
Jose M. Avatar answered Nov 15 '22 18:11

Jose M.