<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>OOP - There It Is - Authentication</title>
    <link>http://www.vpsw.com/blogbaby/</link>
    <description>A Very Practical Blog</description>
    <language>en-us</language>
    <copyright>Dean Fiala</copyright>
    <lastBuildDate>Fri, 25 Aug 2006 18:42:57 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.1.8102.813</generator>
    <managingEditor>dfiala@vpsw.com</managingEditor>
    <webMaster>dfiala@vpsw.com</webMaster>
    <item>
      <trackback:ping>http://www.vpsw.com/blogbaby/Trackback.aspx?guid=0d5aed7a-6d9d-43e7-9ca0-14cfefef8bc6</trackback:ping>
      <pingback:server>http://www.vpsw.com/blogbaby/pingback.aspx</pingback:server>
      <pingback:target>http://www.vpsw.com/blogbaby/PermaLink,guid,0d5aed7a-6d9d-43e7-9ca0-14cfefef8bc6.aspx</pingback:target>
      <dc:creator>Dean</dc:creator>
      <wfw:comment>http://www.vpsw.com/blogbaby/CommentView,guid,0d5aed7a-6d9d-43e7-9ca0-14cfefef8bc6.aspx</wfw:comment>
      <wfw:commentRss>http://www.vpsw.com/blogbaby/SyndicationService.asmx/GetEntryCommentsRss?guid=0d5aed7a-6d9d-43e7-9ca0-14cfefef8bc6</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
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, <a href="http://www.vpsw.com/blogbaby/ct.ashx?id=7a9a7228-4b05-49e3-8ccb-dda8053783b3&amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.web.httpcontext.user.aspx">HttpContext.Current.User</a>,
seems to be misunderstood.<br /><br />
Microsoft has provided a <a href="http://www.vpsw.com/blogbaby/ct.ashx?id=7a9a7228-4b05-49e3-8ccb-dda8053783b3&amp;url=http%3a%2f%2fmsdn.microsoft.com%2flibrary%2fdefault.asp%3furl%3d%2flibrary%2fen-us%2fdnnetsec%2fhtml%2fSecNetAP05.asp">handy
matrix</a> 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.<br /><br />
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.<br /><br />
A freshly created web project in VS2003 (ASP.NET 1.1) has the following security settings
in web.config...<br /><br /></p>
        <blockquote>
          <font color="#800080" face="Courier New" size="2">&lt;authentication mode="Windows"
/&gt; </font>
          <br />
          <font color="#800080" face="Courier New" size="2">  </font>
          <br />
          <font color="#800080" face="Courier New" size="2">&lt;authorization&gt;</font>
          <br />
          <font color="#800080" face="Courier New" size="2">       
&lt;allow users="*" /&gt; &lt;!-- allow all users --&gt;</font>
          <br />
          <font color="#800080" face="Courier New" size="2">&lt;/authorization&gt;</font>
          <br />
        </blockquote>
        <br />
        <br />
The authorization element tells the app to let anyone in, making the authentication
element pointless.<br /><br />
In this situation, the matrix tells us that we won't have any identity to look at
since we'll have an anonymous user.  To test this, we build a web page that has
a label that we will fill with the current user's name, using code like so....<br /><br /><blockquote><font color="#000080" face="Courier New">CurrentUserIdentity.Text = HttpContext.Current.User.Identity.Name;</font><br /></blockquote><br />
Run the app and sure enough,<br />
HttpContext.Current.User.Identity.Name = ""<br /><br />
The user has no meaningful identity, so nothing is reported, we get the empty string.<br /><br />
Make a small change to the web.config file...<br /><br /><blockquote><font color="#800080" face="Courier New" size="2">&lt;authorization&gt;</font><br /><font color="#800080" face="Courier New" size="2">       
&lt;deny users="?" /&gt; &lt;!-- deny anonymous users --&gt;</font><br /><font color="#800080" face="Courier New" size="2">&lt;/authorization&gt;</font><br /></blockquote><br />
This tells the app to disallow ANONYMOUS users, it needs to authenitcate a user before
it serves up the goodies.  Since our authentication mode is set for "windows",
the user must have an account (domain or local) that can login to the server and has
access to the web site.  Run the app again and now the app requests user credentials
from the client's browser.  If the credentials are correct the page will be displayed,
otherwise a prompt dialog box for the user id and password will be displayed. 
Once correctly entered the page will be displayed, but this time it will show the
user's domain and user name...<br /><br />
HttpContext.Current.User.Identity.Name = "somedomain\someuser"<br /><br />
Windows authentication is obviously useful for intranet sites where all the users
are known to the server, but not so much for public sites. For public sites it
makes sense to use Forms authentication. Under Forms Authentication the
app will check a cookie (or a piece of the URL for a cookieless scheme) to see if
the user has been authenticated.  This lets the developer implement a custom
authentication scheme.  Putting the following in web.config tells the app to
use Forms Authentication.<br /><br /><blockquote> <font color="#000080" face="Courier New">  <font color="#800080"> &lt;authentication
mode="Forms"&gt;<br />
        &lt;forms name="FormsTicket"</font></font><font color="#800080"><br /></font><font color="#800080" face="Courier New">         
loginUrl="login.aspx" </font><font color="#800080"><br /></font><font color="#800080" face="Courier New">         
protection="All"</font><font color="#800080"><br /></font><font color="#800080" face="Courier New">         
timeout="20" path="/" </font><font color="#800080"><br /></font><font color="#800080" face="Courier New">         
requireSSL="false"</font><font color="#800080"><br /></font><font color="#800080" face="Courier New">         
slidingExpiration="true" /&gt;</font><font color="#800080"><br /></font><font color="#800080" face="Courier New">    &lt;/authentication&gt;</font><br /></blockquote><br />
There are several ways user authentication can be implemented, the most common
being looking up the user and a (hashed) password in a database.  But the key
is <b>setting the authentication cookie</b> so the app knows that the user has been
authenticated.  There are three methods in the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebsecurityformsauthenticationclasstopic.asp">FormsAuthentication</a> class
that will do this.  This one creates the cookie with the User's ID and redirects
the user to the page they initially requested....<br /><br /><blockquote><font color="#000080" face="Courier New">if(SomeMethodToAuthenticateUser(UserID,
Password) == true)<br />
{<br /></font><blockquote><font color="#000080" face="Courier New">FormsAuthentication.RedirectFromLoginPage(UserID,
false);<br /></font></blockquote><font color="#000080" face="Courier New">}</font><br /></blockquote><br />
This one sets the cookie and supplies the redirect URL, useful when redrection requires
some more logic...<br /><font color="#000080" face="Courier New"><br /></font><blockquote><font color="#000080" face="Courier New">RedirectURL = FormsAuthentication.GetRedirectUrl(UserID,
false);</font><br /></blockquote><br />
This one just sets the cookie...<br /><br /><blockquote><font color="#000080" face="Courier New">FormsAuthentication.SetAuthCookie(UserID,
false);</font><br /></blockquote><br />
All that matters is setting the cookie.  Without the cookie, the app thinks the
user is anonymous and won't show itself.<br /><br />
So what shows up on our page?<br /><br />
HttpContext.Current.User.Identity.Name = UserID<br /><br />
Whatever the UserID set into the cookie was is what shows up as the User's name. 
This is exactly what the matrix tells us it should be. Woo hoo.<br /><br />
So, there is one step to get the current user's identity no matter what authentication
mode is being used.<br /><br />
1) Force authentication by denying access to anonymous users.<br /><br />
OK, three steps for Forms authentication:<br />
2) Authenticate the user<br />
3) <b>Set the authentication cookie</b><br /><br />
So, if the user is not authenticated, the value of the HttpContext.Current.User.Identity.Name
="". For Authenticated users it just depends on the Authentication mode:<br /><ul><li>
Windows =  domain\user (created and maintained by the app's server or its domain)<br /></li><li>
Forms = UserID (created and maintained by the app -- in DB, config file, etc. -- set
in the Forms Authentication cookie) 
</li><li><strike>Paspport = good question, does anybody outside of Redmond use this?<br /></strike></li></ul>
Now, another source of confusion is that each web app user has more than 1 identity,
in fact, <b>each user has 3 identities</b>.  But even this isn't too horrid after
a little investigation.<br /><br />
We've discussed the HttpContext Identity, which holds the user's identity as far as
the application is concerned. There are also the<br /><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecurityprincipalwindowsidentityclasstopic.asp">Windows
Identity </a><br />
and<br />
Thread Identity<br /><br />
Odds are the Thread Identity is something that most developers will not have to concern
themselves with. This is simply the identity tied to the currently executing .NET
thread. The Windows Identity of the current user however, is something that every
web developer has or will encounter at some point, because the Windows Identity is
the actual Windows account that the current user is running under.  This is what
gives the user the PERMISSIONS to access the file system and other resources on the
server.  This is commonly referred to as the user's Security Context.<br /><br />
Let's go back to our previous examples and see what Windows accounts are being used
in each situation: 
<p></p><table border="0" cellpadding="5" cellspacing="5"><tbody><tr><td><font face="Arial" size="2"><b>HttpContext User</b></font></td><td><font face="Arial" size="2"><b>Windows Account (XP, 2000)</b></font></td><td><font face="Arial" size="2"><b>Windows Account (2003)</b></font></td></tr><tr><td><font face="Arial" size="2">Anonymous</font></td><td><font face="Arial" size="2">MACHINE\ASPNET</font></td><td><font face="Arial" size="2">NT Authority\Network Service</font></td></tr><tr><td><font face="Arial" size="2">Windows Authenticated</font></td><td><font face="Arial" size="2">MACHINE\ASPNET</font></td><td><font face="Arial" size="2">NT Authority\Network Service</font></td></tr><tr><td><font face="Arial" size="2">Forms Authenticated</font></td><td><font face="Arial" size="2">MACHINE\ASPNET</font></td><td><font face="Arial" size="2">NT Authority\Network Service</font></td></tr></tbody></table><p>
Duh, that doesn't look complicated, all that matters is the OS, all the users use
the same account! How can that cause confusion?<br /></p>
Well, like many mystery stories, we've left a character out until the end...<br /><br />
IMPERSONATE<br />
 <br />
ASP.NET applications have the ability to "impersonate" a windows account.  The
above examples assumed the default of no impersonation.  Turn on impersonation,
and it gets a little more complicated.<br /><p><a href="http://www.vpsw.com/blogbaby/ct.ashx?id=7a9a7228-4b05-49e3-8ccb-dda8053783b3&amp;url=http%3a%2f%2fmsdn.microsoft.com%2flibrary%2fdefault.asp%3furl%3d%2flibrary%2fen-us%2fcpgenref%2fhtml%2fgngrfidentitysection.asp">Impersonation</a> is
turned on like so...
</p><font color="#800080" face="Courier New" size="2"></font><blockquote><font color="#800080" face="Courier New" size="2">&lt;identity
impersonate="true"&gt;&lt;!-- default --&gt;<br />
OR<br /></font><br /><font color="#800080" face="Courier New" size="2">&lt;identity impersonate="true"
userName="SomeDomain\SomeUser" password="apassword"/&gt;&lt;!-- specific account --&gt;<br /></font></blockquote><p>
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.<br /></p><br /><table border="0" cellpadding="5" cellspacing="5"><tbody><tr><td><font face="Arial" size="2"><b>HttpContext User</b></font></td><td><font face="Arial" size="2"><b>Windows Account (XP, 2000)</b></font></td><td><font face="Arial" size="2"><b>Windows Account (2003)</b></font></td></tr><tr><td><font face="Arial" size="2">Anonymous</font></td><td><font face="Arial" size="2">MACHINE\IUSR_MACHINE</font></td><td><font face="Arial" size="2">MACHINE\IUSR_MACHINE</font></td></tr><tr><td><font face="Arial" size="2">Windows Authenticated</font></td><td><font face="Arial" size="2">SomeDomain\LoggedInUser</font></td><td><font face="Arial" size="2">SomeDomain\LoggedInUser</font></td></tr><tr><td><font face="Arial" size="2">Forms Authenticated</font></td><td><font face="Arial" size="2">SomeDomain\SpecifiedUser</font></td><td><font face="Arial" size="2">SomeDomain\SpecifiedUser</font></td></tr></tbody></table><br /><p>
 Let's put the info for our two identies of concern -- HttpContext and Windows
-- into one table for easy reference.<br /></p><p><font size="2"><br /></font></p><table border="0" cellpadding="6" cellspacing="1"><tbody><tr><td><font size="1"><br /></font></td><td colspan="3" align="center" bgcolor="#a0ffc0"><font face="Arial" size="1"><b>Impersonation = False</b></font></td><td colspan="2" align="center" bgcolor="#ffffc0"><font face="Arial" size="1"><b>Impersonation = True</b></font></td></tr><tr><td><font face="Arial" size="1"><b>HttpContext User</b></font></td><td bgcolor="#a0ffc0"><font face="Arial" size="1"><b>Name</b></font></td><td bgcolor="#a0ffc0"><font face="Arial" size="1"><b>Win Acct (XP, 2000)</b></font></td><td bgcolor="#a0ffc0"><font face="Arial" size="1"><b>Win Acct (2003)</b></font></td><td bgcolor="#ffffc0"><font face="Arial" size="1"><b>Name</b></font></td><td bgcolor="#ffffc0"><font face="Arial" size="1"><b>Win Acct</b></font></td></tr><tr><td><font face="Arial" size="1">Anonymous</font></td><td bgcolor="#a0ffc0"><font size="1"> </font></td><td bgcolor="#a0ffc0"><font face="Arial" size="1">MACHINE\ASPNET</font></td><td bgcolor="#a0ffc0"><font face="Arial" size="1">Network Service</font></td><td bgcolor="#ffffc0"><font size="1"> </font></td><td bgcolor="#ffffc0"><font face="Arial" size="1">MACHINE\IUSR_MACHINE</font></td></tr><tr><td><font face="Arial" size="1">Windows Authenticated</font></td><td bgcolor="#a0ffc0"><font face="Arial" size="1">Domain\LoggedInUser</font></td><td bgcolor="#a0ffc0"><font face="Arial" size="1">MACHINE\ASPNET</font></td><td bgcolor="#a0ffc0"><font face="Arial" size="1">Network Service</font></td><td bgcolor="#ffffc0"><font face="Arial" size="1">Domain\LoggedInUser</font></td><td bgcolor="#ffffc0"><font face="Arial" size="1">Domain\LoggedInUser</font></td></tr><tr><td><font face="Arial" size="1">Forms Authenticated</font></td><td bgcolor="#a0ffc0"><font size="1"><font face="Arial">UserID</font><br /></font></td><td bgcolor="#a0ffc0"><font face="Arial" size="1">MACHINE\ASPNET</font></td><td bgcolor="#a0ffc0"><font face="Arial" size="1">Network Service</font></td><td bgcolor="#ffffc0"><font face="Arial" size="1">UserID</font></td><td bgcolor="#ffffc0"><font face="Arial" size="1">Domain\SpecifiedUser</font></td></tr></tbody></table><br /><br />
As the table makes clear, the HttpContext Identity is only filled for authenticated
users and the value it holds depends only on the authentication mode.  The name
of the HttpContext user is controlled by Windows under Windows Authentication, the
web application under Forms Authentication.  The HttpContext Identity is typically
used by the application for personalization and application security. 
<br /><br />
The Windows Identity depends on the authentication mode and the impersonation settings.
The Windows Identity the Windows Account the user is operating under, which 
determines what files and resources the user can access on the SERVER. Without impersonation,
a user will have access to the server resources via a very restricted account. 
Impersonation can allow the user to have much broader access and should be used with
care.<br /><br />
Hope this summary makes finding the current user's identities! easier. In the next
post we'll look at what authentication wrinkles and new goodies there are in ASP.NET
2.0.  In the meantime, there's a new <a href="http://groups.yahoo.com/group/AspNetMembershipAuthenticate/">Yahoo
Group</a> to help decipher the more puzzling aspects of ASP.NET Security and Authentication.<br /><img width="0" height="0" src="http://www.vpsw.com/blogbaby/aggbug.ashx?id=0d5aed7a-6d9d-43e7-9ca0-14cfefef8bc6" /></body>
      <title>Identity and Authentication in ASP.NET 1.1</title>
      <guid isPermaLink="false">http://www.vpsw.com/blogbaby/PermaLink,guid,0d5aed7a-6d9d-43e7-9ca0-14cfefef8bc6.aspx</guid>
      <link>http://www.vpsw.com/blogbaby/PermaLink,guid,0d5aed7a-6d9d-43e7-9ca0-14cfefef8bc6.aspx</link>
      <pubDate>Fri, 25 Aug 2006 18:42:57 GMT</pubDate>
      <description>&lt;p&gt;
Finding the Identity of the current user is one of those ASP.NET issues that causes
confusion and consternation, especially&amp;nbsp;given the&amp;nbsp;different authentication
schemes and application configurations available in ASP.NET.&amp;nbsp; In particular, &lt;a href="http://www.vpsw.com/blogbaby/ct.ashx?id=7a9a7228-4b05-49e3-8ccb-dda8053783b3&amp;amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.web.httpcontext.user.aspx"&gt;HttpContext.Current.User&lt;/a&gt;,
seems to be misunderstood.&lt;br&gt;
&lt;br&gt;
Microsoft has provided a &lt;a href="http://www.vpsw.com/blogbaby/ct.ashx?id=7a9a7228-4b05-49e3-8ccb-dda8053783b3&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2flibrary%2fdefault.asp%3furl%3d%2flibrary%2fen-us%2fdnnetsec%2fhtml%2fSecNetAP05.asp"&gt;handy
matrix&lt;/a&gt; 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.&amp;nbsp;
In any case, either this matrix is hard to find, or too confusing, so let's review
the basics.&lt;br&gt;
&lt;br&gt;
This post will cover ASP.NET 1.1.&amp;nbsp; Though the basics are the same in 2.0, there
are some other things to discuss, which we'll tackle in a future post.&lt;br&gt;
&lt;br&gt;
A freshly created web project in VS2003 (ASP.NET 1.1) has the following security settings
in web.config...&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;blockquote&gt;&lt;font color="#800080" face="Courier New" size="2"&gt;&amp;lt;authentication mode="Windows"
/&amp;gt; &lt;/font&gt;
&lt;br&gt;
&lt;font color="#800080" face="Courier New" size="2"&gt;&amp;nbsp; &lt;/font&gt;
&lt;br&gt;
&lt;font color="#800080" face="Courier New" size="2"&gt;&amp;lt;authorization&amp;gt;&lt;/font&gt;
&lt;br&gt;
&lt;font color="#800080" face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;allow users="*" /&amp;gt; &amp;lt;!-- allow all users --&amp;gt;&lt;/font&gt;
&lt;br&gt;
&lt;font color="#800080" face="Courier New" size="2"&gt;&amp;lt;/authorization&amp;gt;&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
&lt;br&gt;
The authorization element tells the app to let anyone in, making the authentication
element pointless.&lt;br&gt;
&lt;br&gt;
In this situation, the matrix tells us that we won't have any identity to look at
since we'll have an anonymous user.&amp;nbsp; To test this, we build a web page that has
a label that we will fill with the current user's name, using code like so....&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#000080" face="Courier New"&gt;CurrentUserIdentity.Text = HttpContext.Current.User.Identity.Name;&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
Run the app and sure enough,&lt;br&gt;
HttpContext.Current.User.Identity.Name = ""&lt;br&gt;
&lt;br&gt;
The user has no meaningful identity, so nothing is reported, we get the empty string.&lt;br&gt;
&lt;br&gt;
Make a small change to the web.config file...&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#800080" face="Courier New" size="2"&gt;&amp;lt;authorization&amp;gt;&lt;/font&gt;
&lt;br&gt;
&lt;font color="#800080" face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;deny users="?" /&amp;gt; &amp;lt;!-- deny anonymous users --&amp;gt;&lt;/font&gt;
&lt;br&gt;
&lt;font color="#800080" face="Courier New" size="2"&gt;&amp;lt;/authorization&amp;gt;&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
This tells the app to disallow ANONYMOUS users, it needs to authenitcate a user before
it serves up the goodies.&amp;nbsp; Since our authentication mode is set for "windows",
the user must have an account (domain or local) that can login to the server and has
access to the web site.&amp;nbsp; Run the app again and now the app requests user credentials
from the client's browser.&amp;nbsp; If the credentials are correct the page will be displayed,
otherwise a prompt dialog box for the user id and password will be displayed.&amp;nbsp;
Once correctly entered the page will be displayed, but this time it will show the
user's domain and user name...&lt;br&gt;
&lt;br&gt;
HttpContext.Current.User.Identity.Name = "somedomain\someuser"&lt;br&gt;
&lt;br&gt;
Windows authentication is obviously useful for intranet sites where all the users
are known to the server, but not so much for public sites. For public sites&amp;nbsp;it
makes sense to use Forms authentication.&amp;nbsp;Under Forms Authentication&amp;nbsp;the
app will check a cookie (or a piece of the URL for a cookieless scheme) to see if
the user has been authenticated.&amp;nbsp; This lets the developer implement a custom
authentication scheme.&amp;nbsp; Putting the following in web.config tells the app to
use Forms Authentication.&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&amp;nbsp;&lt;font color="#000080" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&lt;font color="#800080"&gt; &amp;lt;authentication
mode="Forms"&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;forms name="FormsTicket"&lt;/font&gt;&lt;/font&gt;&lt;font color="#800080"&gt;
&lt;br&gt;
&lt;/font&gt;&lt;font color="#800080" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
loginUrl="login.aspx" &lt;/font&gt;&lt;font color="#800080"&gt;
&lt;br&gt;
&lt;/font&gt;&lt;font color="#800080" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
protection="All"&lt;/font&gt;&lt;font color="#800080"&gt;
&lt;br&gt;
&lt;/font&gt;&lt;font color="#800080" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
timeout="20" path="/" &lt;/font&gt;&lt;font color="#800080"&gt;
&lt;br&gt;
&lt;/font&gt;&lt;font color="#800080" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
requireSSL="false"&lt;/font&gt;&lt;font color="#800080"&gt;
&lt;br&gt;
&lt;/font&gt;&lt;font color="#800080" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
slidingExpiration="true" /&amp;gt;&lt;/font&gt;&lt;font color="#800080"&gt;
&lt;br&gt;
&lt;/font&gt;&lt;font color="#800080" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/authentication&amp;gt;&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
There are several ways&amp;nbsp;user authentication can be implemented, the most common
being looking up the user and a (hashed) password in a database.&amp;nbsp; But the key
is &lt;b&gt;setting the authentication cookie&lt;/b&gt; so the app knows that the user has been
authenticated.&amp;nbsp; There are three methods in the &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebsecurityformsauthenticationclasstopic.asp"&gt;FormsAuthentication&lt;/a&gt; class
that will do this.&amp;nbsp; This one creates the cookie with the User's ID and redirects
the user to the page they initially requested....&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#000080" face="Courier New"&gt;if(SomeMethodToAuthenticateUser(UserID,
Password) == true)&lt;br&gt;
{&lt;br&gt;
&lt;/font&gt;&lt;blockquote&gt;&lt;font color="#000080" face="Courier New"&gt;FormsAuthentication.RedirectFromLoginPage(UserID,
false);&lt;br&gt;
&lt;/font&gt;&lt;/blockquote&gt;&lt;font color="#000080" face="Courier New"&gt;}&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
This one sets the cookie and supplies the redirect URL, useful when redrection requires
some more logic...&lt;br&gt;
&lt;font color="#000080" face="Courier New"&gt;
&lt;br&gt;
&lt;/font&gt; &lt;blockquote&gt;&lt;font color="#000080" face="Courier New"&gt;RedirectURL = FormsAuthentication.GetRedirectUrl(UserID,
false);&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
This one just sets the cookie...&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#000080" face="Courier New"&gt;FormsAuthentication.SetAuthCookie(UserID,
false);&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
All that matters is setting the cookie.&amp;nbsp; Without the cookie, the app thinks the
user is anonymous and won't show itself.&lt;br&gt;
&lt;br&gt;
So what shows up on our page?&lt;br&gt;
&lt;br&gt;
HttpContext.Current.User.Identity.Name = UserID&lt;br&gt;
&lt;br&gt;
Whatever the UserID set into the cookie was is what shows up as the User's name.&amp;nbsp;
This is exactly what the matrix tells us it should be. Woo hoo.&lt;br&gt;
&lt;br&gt;
So, there is one step to get the current user's identity no matter what authentication
mode is being used.&lt;br&gt;
&lt;br&gt;
1) Force authentication by denying access to anonymous users.&lt;br&gt;
&lt;br&gt;
OK, three steps for Forms authentication:&lt;br&gt;
2) Authenticate the user&lt;br&gt;
3) &lt;b&gt;Set the authentication cookie&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
So, if the user is not authenticated, the value of the HttpContext.Current.User.Identity.Name
="". For Authenticated users it just depends on the Authentication mode:&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;
Windows =&amp;nbsp; domain\user (created and maintained by the app's server or its domain)&lt;br&gt;
&lt;/li&gt;
&lt;li&gt;
Forms = UserID (created and maintained by the app -- in DB, config file, etc. -- set
in the Forms Authentication cookie) 
&lt;/li&gt;
&lt;li&gt;
&lt;strike&gt;Paspport = good question, does anybody outside of Redmond use this?&lt;br&gt;
&lt;/strike&gt;
&lt;/li&gt;
&lt;/ul&gt;
Now, another source of confusion is that each web app user has more than 1 identity,
in fact, &lt;b&gt;each user has 3 identities&lt;/b&gt;.&amp;nbsp; But even this isn't too horrid after
a little investigation.&lt;br&gt;
&lt;br&gt;
We've discussed the HttpContext Identity, which holds the user's identity as far as
the application is concerned. There are also the&lt;br&gt;
&lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecurityprincipalwindowsidentityclasstopic.asp"&gt;Windows
Identity &lt;/a&gt;
&lt;br&gt;
and&lt;br&gt;
Thread Identity&lt;br&gt;
&lt;br&gt;
Odds are the Thread Identity is something that most developers will not have to concern
themselves with. This is simply the identity tied to the currently executing .NET
thread. The Windows Identity of the current user however, is something that every
web developer has or will encounter at some point, because the Windows Identity is
the actual Windows account that the current user is running under.&amp;nbsp; This is what
gives the user the PERMISSIONS to access the file system and other resources on the
server.&amp;nbsp; This is commonly referred to as the user's Security Context.&lt;br&gt;
&lt;br&gt;
Let's go back to our previous examples and see what Windows accounts are being used
in each situation: 
&lt;p&gt;
&lt;/p&gt;
&lt;table border="0" cellpadding="5" cellspacing="5"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;&lt;b&gt;HttpContext User&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;&lt;b&gt;Windows Account (XP, 2000)&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;&lt;b&gt;Windows Account (2003)&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;Anonymous&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;MACHINE\ASPNET&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;NT Authority\Network Service&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;Windows Authenticated&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;MACHINE\ASPNET&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;NT Authority\Network Service&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;Forms Authenticated&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;MACHINE\ASPNET&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;NT Authority\Network Service&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
Duh, that doesn't look complicated, all that matters is the OS, all the users use
the same account! How can that cause confusion?&lt;br&gt;
&lt;/p&gt;
Well, like many mystery stories, we've left a character out until the end...&lt;br&gt;
&lt;br&gt;
IMPERSONATE&lt;br&gt;
&amp;nbsp;&lt;br&gt;
ASP.NET applications have the ability to "impersonate" a windows account.&amp;nbsp; The
above examples assumed the default of no impersonation.&amp;nbsp; Turn on impersonation,
and it gets a little more complicated.&lt;br&gt;
&lt;p&gt;
&lt;a href="http://www.vpsw.com/blogbaby/ct.ashx?id=7a9a7228-4b05-49e3-8ccb-dda8053783b3&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2flibrary%2fdefault.asp%3furl%3d%2flibrary%2fen-us%2fcpgenref%2fhtml%2fgngrfidentitysection.asp"&gt;Impersonation&lt;/a&gt; is
turned on like so...
&lt;/p&gt;
&lt;font color="#800080" face="Courier New" size="2"&gt;&lt;/font&gt; &lt;blockquote&gt;&lt;font color="#800080" face="Courier New" size="2"&gt;&amp;lt;identity
impersonate="true"&amp;gt;&amp;lt;!-- default --&amp;gt;&lt;br&gt;
OR&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
&lt;font color="#800080" face="Courier New" size="2"&gt;&amp;lt;identity impersonate="true"
userName="SomeDomain\SomeUser" password="apassword"/&amp;gt;&amp;lt;!-- specific account --&amp;gt;&lt;br&gt;
&lt;/font&gt;&lt;/blockquote&gt; 
&lt;p&gt;
Note that Impersonation can be set at any configuration level (machine, application,
subdirectory).&amp;nbsp; 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.&lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;table border="0" cellpadding="5" cellspacing="5"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;&lt;b&gt;HttpContext User&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;&lt;b&gt;Windows Account (XP, 2000)&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;&lt;b&gt;Windows Account (2003)&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;Anonymous&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;MACHINE\IUSR_MACHINE&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;MACHINE\IUSR_MACHINE&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;Windows Authenticated&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;SomeDomain\LoggedInUser&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;SomeDomain\LoggedInUser&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;Forms Authenticated&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;SomeDomain\SpecifiedUser&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font face="Arial" size="2"&gt;SomeDomain\SpecifiedUser&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;p&gt;
&amp;nbsp;Let's put the info for our two identies of concern -- HttpContext and Windows
-- into one table for easy reference.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size="2"&gt;
&lt;br&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;table border="0" cellpadding="6" cellspacing="1"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size="1"&gt;
&lt;br&gt;
&lt;/font&gt;&lt;/td&gt;
&lt;td colspan="3" align="center" bgcolor="#a0ffc0"&gt;
&lt;font face="Arial" size="1"&gt;&lt;b&gt;Impersonation = False&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td colspan="2" align="center" bgcolor="#ffffc0"&gt;
&lt;font face="Arial" size="1"&gt;&lt;b&gt;Impersonation = True&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font face="Arial" size="1"&gt;&lt;b&gt;HttpContext User&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#a0ffc0"&gt;
&lt;font face="Arial" size="1"&gt;&lt;b&gt;Name&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#a0ffc0"&gt;
&lt;font face="Arial" size="1"&gt;&lt;b&gt;Win Acct (XP, 2000)&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#a0ffc0"&gt;
&lt;font face="Arial" size="1"&gt;&lt;b&gt;Win Acct (2003)&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#ffffc0"&gt;
&lt;font face="Arial" size="1"&gt;&lt;b&gt;Name&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#ffffc0"&gt;
&lt;font face="Arial" size="1"&gt;&lt;b&gt;Win Acct&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font face="Arial" size="1"&gt;Anonymous&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#a0ffc0"&gt;
&lt;font size="1"&gt;&amp;nbsp;&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#a0ffc0"&gt;
&lt;font face="Arial" size="1"&gt;MACHINE\ASPNET&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#a0ffc0"&gt;
&lt;font face="Arial" size="1"&gt;Network Service&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#ffffc0"&gt;
&lt;font size="1"&gt;&amp;nbsp;&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#ffffc0"&gt;
&lt;font face="Arial" size="1"&gt;MACHINE\IUSR_MACHINE&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font face="Arial" size="1"&gt;Windows Authenticated&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#a0ffc0"&gt;
&lt;font face="Arial" size="1"&gt;Domain\LoggedInUser&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#a0ffc0"&gt;
&lt;font face="Arial" size="1"&gt;MACHINE\ASPNET&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#a0ffc0"&gt;
&lt;font face="Arial" size="1"&gt;Network Service&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#ffffc0"&gt;
&lt;font face="Arial" size="1"&gt;Domain\LoggedInUser&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#ffffc0"&gt;
&lt;font face="Arial" size="1"&gt;Domain\LoggedInUser&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font face="Arial" size="1"&gt;Forms Authenticated&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#a0ffc0"&gt;
&lt;font size="1"&gt;&lt;font face="Arial"&gt;UserID&lt;/font&gt;
&lt;br&gt;
&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#a0ffc0"&gt;
&lt;font face="Arial" size="1"&gt;MACHINE\ASPNET&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#a0ffc0"&gt;
&lt;font face="Arial" size="1"&gt;Network Service&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#ffffc0"&gt;
&lt;font face="Arial" size="1"&gt;UserID&lt;/font&gt;&lt;/td&gt;
&lt;td bgcolor="#ffffc0"&gt;
&lt;font face="Arial" size="1"&gt;Domain\SpecifiedUser&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;br&gt;
As the table makes clear, the HttpContext Identity is only filled for authenticated
users and the value it holds depends only on the authentication mode.&amp;nbsp; The name
of the HttpContext user is controlled by Windows under Windows Authentication, the
web application under Forms Authentication.&amp;nbsp; The HttpContext Identity is typically
used by the application for personalization and application security. 
&lt;br&gt;
&lt;br&gt;
The Windows Identity depends on the authentication mode and the impersonation settings.
The Windows Identity the Windows Account the user is operating under, which&amp;nbsp;
determines what files and resources the user can access on the SERVER. Without impersonation,
a user will have access to the server resources via a very restricted account.&amp;nbsp;
Impersonation can allow the user to have much broader access and should be used with
care.&lt;br&gt;
&lt;br&gt;
Hope this summary makes finding the current user's identities! easier. In the next
post we'll look at what authentication wrinkles and new goodies there are in ASP.NET
2.0.&amp;nbsp; In the meantime, there's a new &lt;a href="http://groups.yahoo.com/group/AspNetMembershipAuthenticate/"&gt;Yahoo
Group&lt;/a&gt; to help decipher the more puzzling aspects of ASP.NET Security and Authentication.&lt;br&gt;
&lt;img width="0" height="0" src="http://www.vpsw.com/blogbaby/aggbug.ashx?id=0d5aed7a-6d9d-43e7-9ca0-14cfefef8bc6" /&gt;</description>
      <comments>http://www.vpsw.com/blogbaby/CommentView,guid,0d5aed7a-6d9d-43e7-9ca0-14cfefef8bc6.aspx</comments>
      <category>Authentication</category>
      <category>web.config</category>
    </item>
  </channel>
</rss>