That's strange, but maybe the upgrade made changes in the website or application configuration? Check the authentication config in web.config and IIS and ensure both has forms authentication and anonymous access enabled.
.NET 4.0 -> 4.5 Context.User.Identity.Name is no longer set after Forms Authenication (always returns Windows username)
-
10-06-2023 - |
Pregunta
I've been using simple Forms Authenication on my website like this:
protected void btnLogin_Click(object sender, EventArgs e)
{
txtUserName.Text = txtUserName.Text.Trim().ToUpper();
if (txtUserName.Text != string.Empty)
{
if (/* UserName found in SQL database */)
{
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, true);
}
else
/* Display error message */
}
}
Global.asax
:
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
System.Security.Principal.IIdentity id = HttpContext.Current.User.Identity;
string[] roles = { Security.IsAdmin(id.Name) ? "Admin" : "User" };
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
}
}
}
}
Then I used Context.User.Identity.Name
throughout my pages as the identifier of in currently logged. This was working both on server and my local machine. But when the server received upgrade to .NET 4.5 this is no longer working: Context.User.Identity.Name
ALWAYS returns current Windows identity of user accessing the website.
What's wrong? Why can't I set Context.User.Identity.Name
anymore?
Solución
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow