Srb1313711 Srb1313711 - 4 months ago 44
C# Question

HttpContext.Current.User.Identity.Name returns blank?

This is not necessarily an issue i am just curious as to how it works. I have a method:

public static bool UserIsAuthenticated()
{
bool isAuthed = false;
try
{
if (HttpContext.Current.User.Identity.Name != null)
{
if (HttpContext.Current.User.Identity.Name.Length != 0)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
isAuthed = true;
string MyUserData = ticket.UserData;
}
}
}
catch { } // not authed
return isAuthed;
}


The HttpContext.Current.User.Identity.Name returns nulll if the user does not exist, but how does it know which usernames exist or do not exist? Generally any information that might give me more of an understanding of this would be helpful, thanks

Answer Source

As par my understanding of your Que. I am trying to give answer of your Que. The HttpContext.Current.User.Identity.Name returns null , This is depend on authentication mode is Form or Windows in web config.

I write the authentication like this

<authentication mode="Forms"/>

Because of authentication mode="Forms" I got username is null that’s why I change th e authentication mode to Windows like this

<authentication mode="Windows"/>

Again I run the application and checked for username now I got the username successfully.

For more Information, See these link :

System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.Net/C#