简体   繁体   English

.NET 4.0-> 4.5 Context.User.Identity.Name在窗体身份验证后不再设置(总是返回Windows用户名)

[英].NET 4.0 -> 4.5 Context.User.Identity.Name is no longer set after Forms Authenication (always returns Windows username)

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 : 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. 然后,我在整个页面中都使用了Context.User.Identity.Name作为当前登录的标识符。 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. 但是,当服务器收到升级到.NET 4.5的升级时, Context.User.Identity.Name不再起作用: Context.User.Identity.Name ALWAYS返回访问网站的用户的当前Windows身份。

What's wrong? 怎么了? Why can't I set Context.User.Identity.Name anymore? 为什么我不能再设置Context.User.Identity.Name

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. 检查web.config和IIS中的身份验证配置,并确保两者都启用了表单身份验证和匿名访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM