ASP.NET Get current username

I am trying to create an application on our corporate intranet using ASP.NET and VB.NET.

None of these functions return anything as soon as my application is published to IIS. They work great in development (that is: by pressing F5, I get my usual network username), but after publishing they return '' (empty string).

HttpContext.Current.User.Identity.Name Page.User.Identity.Name 

I'm looking for something - everything that captures the username of the current user. Please note: I cannot change these settings in my web.config due to other function requirements.

  <authentication mode="Windows"/> <roleManager enabled="true" cacheRolesInCookie="true" defaultProvider="AspNetWindowsTokenRoleProvider" cookieName=".ASPXROLES" cookiePath="/" cookieTimeout="480" cookieRequireSSL="false" cookieSlidingExpiration="true" createPersistentCookie="false" cookieProtection="All" /> 

I also cannot change any IIS settings to enable the Enable anonymous user option. Speculations are thrown into stone, and I will have to chop off my leg (or head) so that they are changed.

I would think that there should be a way to get the current username with my current configuration.

Any ideas?

Thanks,

Jason

+4
source share
8 answers

Here is what I found (somewhere) and ended up using it. Hope this helps someone else!

 Public Shared Function Check_If_Member_Of_AD_Group(ByVal username As String, _ ByVal grouptoCheck As String, _ ByVal domain As String, _ ByVal ADlogin As String, _ ByVal ADpassword As String) _ As Boolean Dim myDE As DirectoryEntry Dim EntryString As String Dim NumberOfGroups As Integer Dim tempString As String 'Checks to see if the specified user is a member of the specified group Try 'Setup the LDAP basic entry string. EntryString = "LDAP://" & domain 'Make the group to check all lowercase (for matching) grouptoCheck = grouptoCheck.ToLower() 'Use the correct overloaded function of DirectoryEntry If (ADlogin <> "" AndAlso ADpassword <> "") Then myDE = New DirectoryEntry(EntryString, ADlogin, ADpassword) Else myDE = New DirectoryEntry(EntryString) End If 'Filter the directory searcher and get the group names Dim myDirectorySearcher As New DirectorySearcher(myDE) myDirectorySearcher.Filter = "sAMAccountName=" & username myDirectorySearcher.PropertiesToLoad.Add("MemberOf") Dim myresult As SearchResult = myDirectorySearcher.FindOne() 'Get the number of groups, so they can be itereated NumberOfGroups = myresult.Properties("memberOf").Count() - 1 While (NumberOfGroups >= 0) 'Extract the group name from the result set of the index tempString = myresult.Properties("MemberOf").Item(NumberOfGroups) tempString = tempString.Substring(0, tempString.IndexOf(",", 0)) tempString = tempString.Replace("CN=", "") tempString = tempString.ToLower() tempString = tempString.Trim() If (grouptoCheck = tempString) Then 'We got a winner Return True End If NumberOfGroups = NumberOfGroups - 1 End While Return False 'User is not in the specified group Catch ex As Exception Check_If_Member_Of_AD_Group = False 'If all else fails, don't authenticate End Try End Function 
+1
source

Disable anonymous authentication in IIS. User.Identity.Name may be empty if anonymous authentication is enabled in IIS.

Install in web.config

 <configuration> <system.web> <authentication mode="Windows" /> <authorization> <deny users="?"/> </authorization> </system.web> </configuration> 

Use User.Identity.Name to get the user login.

Environment.UserName is the identifier of the current thread. If you turned on impersonation, as Mark said, you may find out that the result of the return will be different. However, this requires ASP.NET Impersionation. If you do not need ASP.NET impersonation and a problem with the stream identifier, you can ignore Environment.UserName if you just use User.Identity.Name.

Also check before taking 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.

+4
source

I am sure that the only way to make it work is to check "Integrated Windows Authentication" in IIS. If "allow anonymous access" is also checked, it will just use anonymous access, so you should disable it ...

+2
source

And another way to get the login username:

 Request.ServerVariables["LOGON_USER"] 
+2
source

The reason it works during the development process is because the VS test web server is not IIS and runs under your current user account.

If you want this to work in IIS, you must be able to configure IIS correctly - there is no other way to do this.

+1
source

Will this work for what you are trying to achieve? Environment.GetEnvironmentVariable("USERNAME").ToString();

0
source

I tried all of the above and none of them worked. I also could not enter my IIS to change the settings. I fought and fought and fought it. I also searched for a long time without finding an answer. One of the things is that I do not have access to IIS, which is blocked, so I could not change any server settings. I had to go with what I could do in the code. When I explored this, many of the answers said: "configure IIS like this.",. Well, that’s great when you have access to IIS, but I didn’t do it - I had to work with what I could do in the code. So, I ended up working with this as follows:

In my web configuration file, I added the following lines of code in the section:

 <system.webServer> <security> <authentication> <anonymousAuthentication enabled="false" /> <windowsAuthentication enabled="true" /> </authentication> </security> </system.webServer> 

Then he returned an error on my local computer, and I had to log in and fix it. I went to the applicationhost.config file located on the following path on my machine (yours may be different):

C: \ users \ "your username" \ My Documents \ "yourIISInstallation" \ config \ applicationhost.config

and I changed the following settings to "allow", which was set to "deny":

 <section name="anonymousAuthentication" overrideModeDefault="Deny" /> 

changed to

 <section name="anonymousAuthentication" overrideModeDefault="Allow" /> 

and

 <section name="windowsAuthentication" overrideModeDefault="Deny" /> 

changed to:

 <section name="windowsAuthentication" overrideModeDefault="Allow" /> 

in

 <sectionGroup name="authentication"> 

section. Before I found out about this, I pulled my hair out because of this. I hope this helps someone. As soon as I entered the above code into the webconfig file, it worked on the intranet, it just returned errors in my local one, but as soon as I added this to my local applicationhost.config file, it started working on my local one as well. Then I called the following variable to return the name of the registered user in the windows:

 HttpContext.Current.User.Identity.Name.ToString().Substring((HttpContext.Current.User.Identity.Name.ToString().IndexOf("\\")) + 1); 

Hooray!

0
source

If the domain and username specified something like "DOMAIN\username" in AD

HttpContext.Current.User.Identity.Name.Split('\\')[0] returns the domain

and

HttpContext.Current.User.Identity.Name.Split('\\')[1] returns the username

0
source

All Articles