HttpContext.Current.User.Identity.Name is always string.Empty
Asked Answered
N

7

27

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?

Nagy answered 29/6, 2009 at 3:46 Comment(5)
that code seems straightforward enough. you're sure that the user is authenticated? any funny business with changing the provider dynamically or some such?Brunet
no funny bizniz,, user is authenticatedNagy
Did you set the user name in the authentication cookie with FormsAuthentication.SetAuthCookie?Biquadratic
How are you dispatching your cookie? In what method are you calling the above code?Pretext
I have edit my answer, check now....Performing
E
36
FormsAuthentication.SetAuthCookie(tbUsername.Text, true);
bool x = User.Identity.IsAuthenticated; //true
string y = User.Identity.Name; //""

The problem you have is at this point you're only setting the authentication cookie, the IPrincipal that gets created inside the forms authentication module will not happen until there is a new request - so at that point the HttpContext.User is in a weird state. Once the redirect happens then, because it's a new request from the browser the cookie will get read before your page is reached and the correct user object created.

Cookies are only set on the browser after a request is completed.

As an aside RedirectFromLoginPage creates a forms auth cookie anyway, you don't need to do it manually

Erinn answered 29/6, 2009 at 6:0 Comment(2)
this is a good point. are you testing for the user name on the same page that sets the cookie/does the login? if so, check HttpContext.Current.User.Identity on a subsequent page.Brunet
very nice answer. But this doesn't help me much with my problem. I want to be able to redirect the user to a particular page based on the user's role. An admin directly to admin panel, a normal user to welcome page, an advanced user to a particular other page... I hoped to use User.IsInRole() to determine the ActionResult of the LogOn action method... but it's "in a weird state" as you put it :-)Jesuitism
R
24

Please try System.Web.HttpContext.Current.Request.LogonUserIdentity.Name instead of User.Identity.Name. It worked for me.

Rede answered 24/9, 2012 at 13:44 Comment(3)
This is something different. When I read this value on my website, I get my Windows account (AD) ...Erasmo
If User.Identity.Name is empty and you expect it not to be, but LogonUserIdentity.Name is not, then you may have a problem with your applicationhost.config if you are debugging out of Visual Studio. See my SO question for more info. #19687433Quibbling
Yea I agree with Jowen.. That is not the same Identity as what was asked for... but it is cool that is accessible I suppose.Fuller
P
9

The value of HttpContext.Current.User.Identity.Name is set by the call to RedirectFromLoginPage. You can get the current user id from HttpContext.Current.User.Identity.Name once you are redirected to a new page. I'm not sure why you would need to access the user name through the User property in this context, couldn't you just use the value contained in tbUsername.Text?

Pretext answered 29/6, 2009 at 5:41 Comment(0)
B
2

in VS Community 2015 version, if you create a web forms application, it automatically add codes in web.config node to remove FormsAuthentication, try remove below section

<modules>
  <remove name="FormsAuthentication"/>
</modules>
Barre answered 31/3, 2016 at 0:2 Comment(0)
M
1

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 :)

Mohn answered 17/2, 2014 at 9:52 Comment(0)
W
0

If you're looking for the name of the user from the membership provider, try something like this ...

var user = Membership.GetUser( HttpContext.Current.User.Identity.Name );
Wiatt answered 29/6, 2009 at 3:57 Comment(1)
If I need the user rather the user name, then I could simply use Membership.GetUser(); :<|Nagy
U
0

If you're using URL rewrite or change your URL,it may be cause return Empty null value.You should try change path of your URL from .html to .aspx or none extendtion. this is issue for my case.You try.I hope this useful

Undressed answered 9/4, 2014 at 16:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.