views:

122

answers:

3

Hi,

I'm coming across a peculiar request: I have a website that uses Forms Authentication, but it now needs to grab the WindowsPrincipal (Windows Authentication) at some point in order to reuse it.

My first instinct was to create a new page, and to disable Anonymous Access on IIS for that precise page. When I'm on that page, Request.ServerVariables["LOGON_USER"] gives me the current Windows login name as expected.

Unfortunately, Context.User still gives me a GenericPrincipal.

Any idea on how I could get the current WindowsPrincipal in a FormsAuthentication Application? (recreating it by asking the user for his password is not an option)

A: 

What does this return?

WindowsIdentity wi = WindowsIdentity.GetCurrent();
Jan Jongboom
In fact, that will return ASP.Net's windows user (but you were on the right track)
Luk
A: 

See this similar question.

Preet Sangha
A: 

Found it: Context.Request.LogonUserIdentity is what should be used in this scenario.

It will return the windows user that made the request if Anonymous Access is disabled on IIS (otherwise it'll return the IIS anonymous user).

For those interested on how to reuse it:

lblUserName.Text = WindowsIdentity.GetCurrent().Name; 
    // will return the ASP.Net user (that's useless to us)

WindowsIdentity id = Context.Request.LogonUserIdentity;
WindowsImpersonationContext ctx = id.Impersonate();
try
{
    lblUserName.Text = WindowsIdentity.GetCurrent().Name;
        // will return the correct user

    // (...) do your stuff
}
finally
{
    ctx.Undo();
}
Luk