13

I am using HttpContext.Current.User.Identity.Name to get the user name when the web application is in use. During development I was using my local iis, with integrated windows authentication enabled and anonymous access enabled and disabled, and I was able to get the username.

Now, when I publish the web application, it comes back blank. The setup on the published server is the same, and I have tried using Page.User.Identity.Name, which also returned blank.

Does anyone know why this is and how to fix it?

Andy5
  • 2,319
  • 11
  • 45
  • 91

9 Answers9

18

You probably had Anonymous Authentication on as well as Windows Authentication. Turn off Anonymous off.

So,

<system.web>
    <authentication mode="Windows" />
</system.web>

In IIS config for the app, look in Authentication tab

Set **Anonymous Authentication** to **Disabled** and
Set **Windows Authentication** to **Enabled**

This should work and the Identity.UserName should now show up properly.

starvator
  • 989
  • 1
  • 11
  • 26
Thing
  • 190
  • 1
  • 5
  • I ran into the same problem recently, and this solution doesn't work. is there any other causes you can think of? – Thang Do Apr 15 '16 at 00:15
3

To solve the problem, you have to enable the Windows Authentication feature. Follow the below steps:

-Click Start, and then click Control Panel. Open the Programs group.
-Under Programs and -Features, click Turn Windows Features on or off.
-Expand the item labeled Internet Information Services.
-Expand the item labeled World Wide Web Services. -Expand the item Security ->
Make sure to select Windows Authentication

Also you need to disable Anonymous Authentication from the IIS as follows: -Click on your application in IIS -Double click Authentication under IIS group -Click on Anonymous Authentication -Click on Disable on the right side under Actions. Hope this helps

A Ghazal
  • 2,693
  • 1
  • 19
  • 12
3

HttpContext.Current.Request.LogonUserIdentity.Name always work for me in VS 2012 environment and IIS 7

Tony Dong
  • 3,213
  • 1
  • 29
  • 32
  • Depends entirely what you want to do. It can show the IIS account for unauthenticated request, or it can show the application pool identity, or it can show the remote user if the remote user was in fact authenticated using NTLM for the current request. If the problem is an issue with getting authentication to work properly, this value is not going to magically solve it. – Oskar Berggren Aug 01 '23 at 20:59
1

When working with WIF you should use Thread.CurrentPrincipal.Identity.Name instead of User.Identity.Name.

Read more here: http://msdn.microsoft.com/en-us/magazine/ff872350.aspx to learn more about Windows Identity Foundation

Similar question: User.Identity.Name is null after authenticate via WIF

Community
  • 1
  • 1
1

This will not solve the original post, but want to put this here anyways in case others stumble across this when searching for why user.identity is returning nothing...

In my case User.Identity started returning nothing after updating a users ad username (specifically the pre-windows 2000 username).

The LSA cache on IIS was the issue. Even after restarting the IIS server while troubleshooting the issue persisted. It was not until adding the registry setting outlined here the the issue was fixed:

https://support.microsoft.com/en-us/help/946358/the-lsalookupsids-function-may-return-the-old-user-name-instead-of-the

Mike
  • 11
  • 1
  • Please add the important details from the link into your post. Links to third-party resources should only used as a reference. If the linked page no longer exists it would render this answer useless. – Dmitry Gamolin Apr 16 '18 at 15:15
0

For a blank return, my solution ended up being the web.config. I'm using Visual Studio 2010, and the default web.config did not work. I replaced it with a nearly empty web.config and then success! Perhaps the default vs2010 web.config called too many references or configured the IIS incorrectly for the use of User.Identity.Name. My system is Windows7.

Default asp.net web site web.config from vs2010 was about 100-115 lines long. As you can see below the nearly empty web.config is about 20 lines long.


the web.config that i used:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <system.web>
   <authentication mode="Windows" />
   <authorization>
      <allow roles="Doman Name\Group Name" users="" />
      <deny users="*" />
   </authorization>

  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
        <security>
            <authorization>
                <add accessType="Allow" users="" roles="Doman Name\Group Name" />
            </authorization>
        </security>
  </system.webServer>

</configuration>
Liem
  • 1
0

set <authentication mode="Forms"> in web.config file & Your Problem Will solve.

Test your web-site by using below code

if (Page.User.Identity.Name != "" ) { Label1.Text = "Hello"; } else { Response.Redirect("login.aspx?url=Upload.aspx"); }

Pankil Agrawal
  • 101
  • 1
  • 1
  • 11
0
  1. In IIS: click on your Site.
  2. In Home Page: Authentication.
  3. In Action menu: Open Feature.
  4. Disable Anonymous Authentication.
  5. Restart Site.

steps 1,2,3

step 4

0

The correct way to get the name by WindowsPrincipal. I am giving you the demo code below

WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
string username = wp.Identity.Name;