4

I'm trying to use System.Threading.Thread.CurrentPrincipal.Identity.Name to get the login of who is using an ASP.NET application. I'm not getting any build errors, but it returns a blank value. I'm using IIS 6 and here are my authentication settings:

  • Anonymous Authentication: Disabled
  • ASP.NET Impersonation: Disabled
  • Basic Authentication: Enabled
  • Windows Authentication: Enabled

I have no authorization settings in my Web.config file either. Here is the method I am using to try to get the login:

    public void SetUser()
    {
       string login = System.Threading.Thread.CurrentPrincipal.Identity.Name;
    }

I have a breakpoint to check the value and it says login = "".

How can I get the login?

EDIT

Here is my authentication picture:enter image description here

SharpC
  • 6,974
  • 4
  • 45
  • 40
user3062114
  • 129
  • 1
  • 3
  • 12
  • Can you clarify what is going on in terms of set/get/void? You call it "SetUser", but it retrieves a value and does nothing with it. What about the machine this runs on? Is it a normal Windows server, or a VM, or in the cloud, etc? Did you try to run a simple program with just this on your machine? I am asking because it works for me, so I am trying to figure out what is different for you. – radumanolescu Jun 19 '14 at 15:24
  • The "current principal" is settable. Is it possible that some other part of the program has set it to a blank value? See http://msdn.microsoft.com/en-us/library/system.threading.thread.currentprincipal(v=vs.110).aspx – radumanolescu Jun 19 '14 at 15:29
  • The SetUser() will retrieve the value for login. I will use login to pull records from a database to see if they are authorized. I don't have the logic for that written out, I'm just trying to get the value first. I'm running this on a normal Windows 7 OS locally. – user3062114 Jun 19 '14 at 15:30
  • 1
    Just ask for `HttpContext.Current.User.Identity.Name` instead. – Wiktor Zychla Jun 19 '14 at 15:47
  • @WiktorZychla - That also returns a blank value. – user3062114 Jun 19 '14 at 15:49
  • 1
    For some reason you haven't turned off anonymous access. – Wiktor Zychla Jun 19 '14 at 15:50
  • @WiktorZychla My IIS authentication says I have, I just edited my question with a picture of my IIS Authentication settings. I've restarted IIS a couple times too. – user3062114 Jun 19 '14 at 15:54
  • If anonymous authentication is disable, I don't know how you can check what the method is doing without logging in. – Erik Philips Jun 19 '14 at 16:20
  • Have you configured authorization to deny unauthenticated requests? – Wiktor Zychla Jun 19 '14 at 16:37

2 Answers2

8
Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());

Worked for me. I was having the same problem.

SharpC
  • 6,974
  • 4
  • 45
  • 40
  • Noted that even the project is not put in IIS, this method does not return null. So... I guess using this method will technically remove the relationship between the project and IIS login – Near Jul 02 '19 at 08:35
2

I was able to fix it. In my project properties, I was using IIS express so my application wasn't hitting my actual IIS. I unchecked that, created a virtual directory and disabled all authentication except Windows Authentication and it works now.

user3062114
  • 129
  • 1
  • 3
  • 12
  • I disagree. It wasn't working, and for someone else, this could be a problem. – user3062114 Jun 20 '14 at 20:15
  • 1
    @Amol if it solved your problem then you can mark this post as accepted answer by clicking on grey check mark below voting buttons. This helps the community to focus on unsolved questions. – RBT Jan 04 '18 at 07:59