HttpContext.Current.User.Identity.Name is always s

2019-01-08 17:13发布

Hi I use a custom MembershipProvider.

I want to know the current username during an application scenario, but when I try accessing HttpContext.Current.User.Identity.Name it always returns string.Empty.

if (Membership.ValidateUser(tbUsername.Text, tbPassword.Text))
{
    FormsAuthentication.SetAuthCookie(tbUsername.Text, true);
    bool x = User.Identity.IsAuthenticated; //true
    string y = User.Identity.Name; //""
    FormsAuthentication.RedirectFromLoginPage(tbUsername.Text, cbRememberMe.Checked);
}

Am I missing something?

7条回答
疯言疯语
2楼-- · 2019-01-08 18:08

As already suggested FormsAuthentication.RedirectFromLoginPage() method, sets the Authentication Cookie automatically.

However in my case, i had nested web applications where i had cleared <httpModules> tag in child application (so that it does not inherit httpModules from its parent application) in the web.config file. Removing the unwanted parent httpModules made everything work again.

its better to check this tag before complicating things :)

查看更多
登录 后发表回答