i have looked around and still didn't get an answer to this.
I have a ASP.NET application and it uses windows authentication. Web.config is properly configured:
<authentication mode="Windows"/>
<authorization>
<deny users="?"/>
</authorization>
I am getting the userid in method: WindowsAuthentication_Authenticate(object sender, WindowsAuthenticationEventArgs e)
The code that is getting the userid:
var userId = String.Empty;
//Dispalys the UserName from AD.
System.Security.Principal.IPrincipal User;
User = System.Web.HttpContext.Current.User;
if (User != null)
{
string username = User.Identity.Name;
username = username.Substring(username.IndexOf("\\") + 1);
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
userId = (textInfo.ToTitleCase(username));
}
System.Web.HttpContext.Current.User is always null in this case.
What am i doing wrong here?
One of the arguments of WindowsAuthentication_Authenticate
method is of WindowsAuthenticationEventArgs
type. One of its properties is Identity
, so You should probably be able to access user name by writing e.Identity.Name
.
Reference links: