I am trying to build an application on our company's intranet using ASP.NET and VB.NET.
Neither of these functions return anything once my application is published to IIS. They work fine in development (ie: pressing F5 I get my regular network username), but once published they return '' (an empty string).
HttpContext.Current.User.Identity.Name
Page.User.Identity.Name
I'm looking for something -- anything -- that will grab the current users login name. Please note that I CANNOT change these settings in my web.config, because of other functionality requirements.
<authentication mode="Windows"/>
<roleManager enabled="true" cacheRolesInCookie="true" defaultProvider="AspNetWindowsTokenRoleProvider" cookieName=".ASPXROLES" cookiePath="/" cookieTimeout="480" cookieRequireSSL="false" cookieSlidingExpiration="true" createPersistentCookie="false" cookieProtection="All" />
Nor can I change any IIS settings, to include the 'Enable Anonymous User' setting. Specs are cast in stone and I'd have to chop off my own leg (or head) to get them changed.
I would think there's got to be a way to get the current logged in user's name with my current configuration.
Any ideas?
Thanks,
Jason
Disable Anonymous Authentication in IIS.
User.Identity.Name
might be empty if Anonymous Authentication is enabled in IIS.
Set in web.config
<configuration>
<system.web>
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
Use User.Identity.Name
to get the logon user.
Environment.UserName
is the running thread identity. If you have enabled Impersonation as Mark said, you can find out the returning result will be different. However this requires ASP.NET Impersionation. If you don't need ASP.NET Impersonation and dealing with the thread identity, you can ignore Environment.UserName
if and just use User.Identity.Name.
Also check before perform any action.
if (User.Identity.IsAuthenticated)
{
Page.Title = "Home page for " + User.Identity.Name;
}
else
{
Page.Title = "Home page for guest user.";
}
Here is a good example