Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormsAuthentication Membership.GetUser() Null

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.

like image 891
CodeLikeBeaker Avatar asked Jan 29 '10 15:01

CodeLikeBeaker


1 Answers

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;
like image 148
David Avatar answered Oct 16 '22 15:10

David