CurrentPrincipal.Identity.Name is blank
Asked Answered
W

2

5

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

Whipple answered 19/6, 2014 at 15:12 Comment(9)
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.Outsail
The "current principal" is settable. Is it possible that some other part of the program has set it to a blank value? See msdn.microsoft.com/en-us/library/…Outsail
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.Whipple
Just ask for HttpContext.Current.User.Identity.Name instead.Piecedyed
@WiktorZychla - That also returns a blank value.Whipple
For some reason you haven't turned off anonymous access.Piecedyed
@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.Whipple
If anonymous authentication is disable, I don't know how you can check what the method is doing without logging in.Awn
Have you configured authorization to deny unauthenticated requests?Piecedyed
U
9
Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());

Worked for me. I was having the same problem.

Uniseptate answered 2/3, 2017 at 16:15 Comment(2)
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 loginEnthusiastic
thanks solved my problem! I am needing to fix a very old app and could not figure out why the principal was null when I was debugging and fine on the server. Code just had var principal = Thread.CurrentPrincipal as WindowsPrincipal; btw this old app is a piece of junk! I hate my life...Spaetzle
W
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.

Whipple answered 19/6, 2014 at 16:41 Comment(2)
I disagree. It wasn't working, and for someone else, this could be a problem.Whipple
@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.Ramon

© 2022 - 2024 — McMap. All rights reserved.