9

I am using Forms Authentication and have the basic logon page and default page.

When I am on the logon page, and call the SignOn this works just great. However, when I am still on the Logon page the Membership.GetUser() returns null. When I redirect to my default page, the Membership.GetUser() returns my user information.

Is there any way I get get this method to return my user while still on the logon page. I have read all over google that other have similar issues where it will only work once you redirect.

Let me know if you need further information.

Here is a simple code snippet of what I am using to verify that the information is being set:

bool authenticated = User.Identity.IsAuthenticated;
            string username = User.Identity.Name;

            MembershipUser user = Membership.GetUser();

I put this code on both the logon page and the default page in the code behind and only the default page has values and shows that it is authenticated after the authentication process executes.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108

6 Answers6

13

Something else to try is the following code:

 MembershipUser user = Membership.GetUser(username);
 GenericIdentity identity = new GenericIdentity(user.UserName);
 RolePrincipal principal = new RolePrincipal(identity);
 System.Threading.Thread.CurrentPrincipal = principal;
 HttpContext.Current.User = principal;
David
  • 131
  • 1
  • 2
  • Brilliant! This works great for making the user you just validated in LogOn the current user. I put this right after `if (Membership.ValidateUser(model.UserName, model.Password)...`. Thanks! – tig Nov 30 '11 at 06:42
11

This might be because you allow anonymous users on your login page. Therefore the browser doesn't bother sending any more information to this page than is necessary.

Chris Simpson
  • 7,821
  • 10
  • 48
  • 68
  • This makes sense actually. Since the logon page is the only page allowed for anonymous users, the system can't pull the membership information until it is on a page that requires the authenticated user... very interesting.. – CodeLikeBeaker Jan 29 '10 at 16:09
  • 3
    Exactly. How can you know who is logged in before they log in? – Greg Jan 29 '10 at 16:18
  • Okay, thanks for the info. I am pretty sure this is it. Appreciate it. – CodeLikeBeaker Jan 29 '10 at 16:19
2

I was in this same situation, here is what worked for me on MVC 4 .NET 4.5.

Membership.GetUser(HttpContext.Current.User.Identity.Name)
Clay Smith
  • 1,051
  • 14
  • 27
2

I had a similar issue and the problem turned out to be that I was missing authentication method = form in the web config .

<system.web>
    <authentication mode="Forms"/>
    ....

Don't forget that one (I was migrating an old legacy site to aspnet)

Etienne
  • 1,058
  • 11
  • 22
0

I had this issue and found it was due to having multiple membership providers, so instead of

Membership.GetUser()

you can try

Membership.Providers["MyMembershipProvider"].GetUser()

or more specifically

Membership.Providers["MyMembershipProvider"].GetUser(LoginCtrl.UserName, false)
Simon Hawes
  • 101
  • 1
  • 3
0

I had a similar problem (it was all pages on the site) and it was caused by an error in the AspNetSqlMembershipProvider connection string, which SHOULD have been different in different environments but wasn't, so it worked locally but not when deployed to the server.

jmoreno
  • 12,752
  • 4
  • 60
  • 91