从2台计算机同时登录时,HttpContext.Current.User.Identity.Name不正确

时间:2010-10-08 08:55:32

标签: asp.net forms-authentication

我有一个带有IIS 7的ASP.NET网络服务器。

我的身份验证代码(表单身份验证)在登录页面上如下:

            var isAuthenticated = Membership.ValidateUser(usernameTextBox.Text, passwordTextBox.Text);
            if (isAuthenticated)
            {
                FormsAuthentication.RedirectFromLoginPage(usernameTextBox.Text, true);
            }
            else
            {
                var customValidator = new CustomValidator();
                customValidator.IsValid = false;
                customValidator.ErrorMessage = GetLocalResourceObject("LoginFailed.ErrorMessage").ToString();
                customValidator.ValidationGroup = "AllValidators";
                Page.Validators.Add(customValidator);
            }

在另一个页面上,我显示了用户名:

    if (HttpContext.Current.User.Identity != null &&
        !string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name))
    {
        string authenticatedUsername = HttpContext.Current.User.Identity.Name;

        return "authenticatedUsername=" + authenticatedUsername;
    }
    else
    {
        return null;
    }

我的问题是,如果我和我的一位同事使用不同的登录名(和不同的帐户)同时登录,则帐户设置为OK(我们看到不同的项目),但其中一个名称设置为另一个登录用户。

因此,如果我使用用户名foo和我的同事用用户名bar登录,我们将使用我们各自的帐户登录,但我会看到用户名bar或他将在页面上看到我的用户名foo

我已经看到了一些关于ASP.NET身份验证的奇怪行为的其他帐户,他们声称它是通过禁用输出缓存功能来修复的。它对我不起作用。

任何帮助表示赞赏,我不知道如何跟踪问题。

1 个答案:

答案 0 :(得分:0)

最后问题出在我的代码上。我想禁用页面的缓存并使用以下

<%@ OutputCache Duration="1" NoStore="true" VaryByParam="none" %>

似乎工作,并且大多是正确的。 但它并不完整,完整版本是

<%@ OutputCache Duration="1" NoStore="true" Location="None" VaryByParam="none" %>

有效。

后来,它发生得相当明显。虽然客户端不会存储页面,但如果Location未设置为None,ASP将会暂停一秒钟。

MSDN上的文档真的让我感到困惑。

相关问题