How Does Httpcontext.Current.User.Identity.Name Know Which Usernames Exist

How does HttpContext.Current.User.Identity.Name know which usernames exist?

The HttpContext.Current.User.Identity.Name returns null

This depends on whether the authentication mode is set to Forms or Windows in your web.config file.

For example, if I write the authentication like this:

<authentication mode="Forms"/>

Then because the authentication mode="Forms", I will get null for the username. But if I change the authentication mode to Windows like this:

<authentication mode="Windows"/>

I can run the application again and check for the username, and I will get the username successfully.

For more information, see System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET.

Where does Web.HttpContext.Current.User.Identity.Name come from?

The username is just a property of the IPrinciple user object and that object is set in one of the standard ASP.NET HTTPModules, in your case probably System.Web.Security.FormsAuthenticationModule as part of the OnAuthenticate method.

If what you want to know is how to change this information, such as setting a different username or identity, you will want to look at creating a global.asax or a custom HTTPModule which overrides the Application_AuthenticateRequest. Here is an example:

Public Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cookieName As String = FormsAuthentication.FormsCookieName
Dim authCookie As HttpCookie = HttpContext.Current.Request.Cookies(FormsAuthentication.FormsCookieName)

If Not IsNothing(authCookie) Then
Dim authTicket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(authCookie.Value)
If IsNothing(authTicket) OrElse authTicket.Expired Then
HttpContext.Current.Response.Redirect(FormsAuthentication.LoginUrl)
Else
Dim id As New FormsIdentity(authTicket)

Dim newUser As New YourCustomUserType(id.Name)
HttpContext.Current.User = newUser
End If
End If
End Sub

System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET

System.Environment.UserName returns the identity under which the app pool that hosts your web app is running. If you're using Windows authentication and impersonation then it will be the actual user's name, however in all cases you're better off using the information provided by the HTTP context. There is no performance hit either way.

Where does HttpContext.Current.User.Identity.Name get stored / accessed?

I found the solution: the page was in the "Intranet" zone. I opened IE, and went into internet options. I went to the Internet Options, went to security tab, selected Intranet, and then "Custom Level". Went down to user authentication and set it to "Prompt for user name and password". Closed the browser, reopened that page and then it prompted me. Changed the credentials accordingly.

HttpContext.Current.User.Identity.Name is always string.Empty

FormsAuthentication.SetAuthCookie(tbUsername.Text, true);
bool x = User.Identity.IsAuthenticated; //true
string y = User.Identity.Name; //""

The problem you have is at this point you're only setting the authentication cookie, the IPrincipal that gets created inside the forms authentication module will not happen until there is a new request - so at that point the HttpContext.User is in a weird state. Once the redirect happens then, because it's a new request from the browser the cookie will get read before your page is reached and the correct user object created.

Cookies are only set on the browser after a request is completed.

As an aside RedirectFromLoginPage creates a forms auth cookie anyway, you don't need to do it manually

Unable to get the current user identity value

IIS settings should be like this:

+------------------------------------+
| Authentication |
+------------------------------------+
Name Status
------------------------ --------
Anonymous Authentication Disabled ---+
ASP.NET Impersonation Enabled |
Basic Authentication Enabled |__ Both are important
Digest Authentication Disabled |
Forms Authentication Disabled |
Windows Authentication Enabled ---+

Use User.Identity.Name to get the logon user and test like this:

protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
{
Page.Title = "Home page for " + User.Identity.Name;
}
else
{
Page.Title = "Home page for guest user.";
}
}

@HttpContext.Current.User.Identity.Name not showing backslash

Based on your comments you are using the following code to show the user name:

alert('@HttpContext.Current.User.Identity.Name');

@HttpContext.Current.User.Identity.Nameis a string that can contain "\" backslash character. This character is considered as a escape character in javascript as it is in C# as well.
You need to escape the "\" character in the string before passing it to Javascript like that:

alert('@HttpContext.Current.User.Identity.Name.Replace("\\", "\\\\")')


Related Topics



Leave a reply



Submit