Reputation: 2054
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?
Upvotes: 3
Views: 22992
Reputation: 338
If you are using FormsAuthentication.SetAuthCookie then you need to add
<authentication mode="Forms" />
to
<System.Web>
in Web.config file. Solution from here
Upvotes: 0
Reputation: 314
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.
Upvotes: 2
Reputation: 10329
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");
}
}
Upvotes: 3
Reputation: 43077
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.
Upvotes: 4