3

I am trying to figure out why HttpContext.User.Identity.Name is returning blank.

Code

public ActionResult Test()
{
    string username = HttpContext.User.Identity.Name;
    return Content(username);         
}

Am I using this in the wrong context? I am trying to get the user's username.

Web.Config

<authentication mode="Windows" />

IIS

I have enabled Anonymous and nothing else is checked. I am running IIS 6.0.

Is there any type of information I need to add to assist with figuring this out? I am pretty stuck. I checked this question but do I need to set a Cookie to make this work?

PTD
  • 1,028
  • 1
  • 16
  • 23
xivo
  • 2,054
  • 3
  • 22
  • 37
  • 1
    Is HttpContext.User.Identity.Name blank? Or the returned string from GetFileNameWithoutExtension is blank? What's your Authentication config look like? – Erix Dec 18 '12 at 18:25
  • 1
    What authentication mechanism are you using? Forms? Windows? Have you checked that Request.IsAuthenticated returns true? – xdumaine Dec 18 '12 at 18:26
  • I updated the question. I pasted old code that was used before. It "did" work. IsAuthenticated returns false. – xivo Dec 18 '12 at 18:31
  • Uncheck anonymous authentication in IIS and only enable Windows Authentication. That will fix it. – Caleb Keith Dec 18 '12 at 18:34
  • Didn't work for some reason. – xivo Dec 18 '12 at 18:40

4 Answers4

4

I have enabled Anonymous and nothing else is checked. I am running IIS 6.0.

This means that you won't be prompted to login, so User.Identity.IsAuthenticated will be false and User.Identity.Name will be blank.

Uncheck Anonymous Authentication and check Windows Authentication.

jrummell
  • 42,637
  • 17
  • 112
  • 171
3

IsAuthenticated returns false, and thus Identity.Name returns empty string because you haven't required authentication for that action. You have to enable Windows Authentication and require authentication for the action. Try requiring that the user be authorized for the action by decorating it with the [Authorize] attribute - which will initiate the authentication negotiation.

[Authorize]
public ActionResult Test()
{
    if(Request.IsAuthenticated)
    {
        string username = HttpContext.User.Identity.Name;
        return Content(username);         
    }
    else 
    {
        return Content("User is not authenticated");
    }
}
xdumaine
  • 10,096
  • 6
  • 62
  • 103
  • Please elaborate. What doesn't work? Did you Enabled Windows Authentication? Are you prompted for credentials before reaching the action? If not, then you have something configured incorrectly. – xdumaine Dec 18 '12 at 18:58
  • You solution doesn't answer my question. It doesn't return anything at all. It's blank. The page is blank nothing happens. I'm pretty frustrated. – xivo Dec 18 '12 at 19:15
  • @xivo I'm willing to try to help, but you're not providing enough information. Returning blank is what it *should* do. The user is not authenticated, so the Name returned is an empty string, and all you're returning is that empty string. I just updated my code that will give you more information. If you get "User is not Authenticated" then that tells me for sure that your windows authentication is not configured correctly. – xdumaine Dec 18 '12 at 19:23
  • I feel like this is not an "issue", but of what I attempted to do. I don't think it will work out even if this question is answered. I am willing to discard the question, but thank you for the help. I don't think I am clear enough for people today so I want to make sure this is deleted so it does not confuse others as some questions did for me before asking this. Thanks again. – xivo Dec 18 '12 at 19:36
2

If anyone else is experiencing this issue, verify "Load User Profile" option is set to true. Go to IIS application pools. Select your app pool from the list. Click on Advanced Settings and scroll down to Process Model section.

This solved the problem in my case.

Dan
  • 304
  • 1
  • 4
0

If you are using FormsAuthentication.SetAuthCookie then you need to add

<authentication mode="Forms" />

to

<System.Web>

in Web.config file. Solution from here

Community
  • 1
  • 1
Artur Alexeev
  • 318
  • 3
  • 7