Finding the Identity of the current user is one of those ASP.NET issues that causes confusion and consternation, especially given the different authentication schemes and application configurations available in ASP.NET. In particular, HttpContext.Current.User, seems to be misunderstood.Microsoft has provided a handy matrix outlining what can be expected under the different scenarios. Note that the last 3 tables are redundant -- they work exactly the same way no matter what form of Windows identification (Basic, Digest or Integrated) IIS is configured to use. In any case, either this matrix is hard to find, or too confusing, so let's review the basics.This post will cover ASP.NET 1.1. Though the basics are the same in 2.0, there are some other things to discuss, which we'll tackle in a future post.A freshly created web project in VS2003 (ASP.NET 1.1) has the following security settings in web.config...
<authentication mode="Windows" /> <authorization> <allow users="*" /> <!-- allow all users --></authorization>
CurrentUserIdentity.Text = HttpContext.Current.User.Identity.Name;
<authorization> <deny users="?" /> <!-- deny anonymous users --></authorization>
<authentication mode="Forms"> <forms name="FormsTicket" loginUrl="login.aspx" protection="All" timeout="20" path="/" requireSSL="false" slidingExpiration="true" /> </authentication>
if(SomeMethodToAuthenticateUser(UserID, Password) == true){FormsAuthentication.RedirectFromLoginPage(UserID, false);}
FormsAuthentication.RedirectFromLoginPage(UserID, false);
RedirectURL = FormsAuthentication.GetRedirectUrl(UserID, false);
FormsAuthentication.SetAuthCookie(UserID, false);
Duh, that doesn't look complicated, all that matters is the OS, all the users use the same account! How can that cause confusion?
Impersonation is turned on like so...
<identity impersonate="true"><!-- default -->OR<identity impersonate="true" userName="SomeDomain\SomeUser" password="apassword"/><!-- specific account -->
Note that Impersonation can be set at any configuration level (machine, application, subdirectory). If this is set in machine.config, it will affect all the web apps on a machine. Also note that putting a password and account in clear text isn't advised and there are ways to put encrypted versions in the registry. Finally note that allowing anonymous users to impersonate a real user account is a silly idea. In general, impersonation should only be used when necessary.
Let's put the info for our two identies of concern -- HttpContext and Windows -- into one table for easy reference.
Remember Me
Theme design by Dean Fiala
Pick a theme: BlogXP calmBlue Candid Blue dasBlog Discreet Blog Blue Elegante essence Just Html Mono Movable Radio Blue Movable Radio Heat orangeCream Portal Project84 Slate Sound Waves Tricoleur windmill
Powered by: newtelligence dasBlog 2.1.8102.813
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Dean Fiala
E-mail