English 中文(简体)
从2台计算机同时登录时,HttpContext.Current.User.Identity.Name不正确
原标题:HttpContext.Current.User.Identity.Name not right when simultaneous login from 2 machines

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

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

            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,我们都将使用各自的帐户登录,但要么我会在页面上看到用户名barfoo

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

感谢您的帮助,我不知道如何追踪这个问题。

最佳回答

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

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

which seemed to work and was mostly correct. It wasn t complete though and the full version is

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

这是有效的。

事后看来,发生的事情是相当明显的。尽管客户端不会存储页面,但如果Location未设置为None,ASP会存储一秒钟。

MSDN上的文档真的让我对这个主题感到困惑。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!