views:

383

answers:

2

Hey Everyone,

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.

Thanks

+4  A: 

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
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..
Jason Heine
Exactly. How can you know who is logged in before they log in?
Greg
Okay, thanks for the info. I am pretty sure this is it. Appreciate it.
Jason Heine
A: 

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