Search code examples
javascriptasp.net-membershipmembership-provider

JavaScript equivalent to Membership.GetUser()


I am using ASP.NET Membership Provider JavaScript library to authenticate users using the following:

Sys.Services.AuthenticationService.login(...)

And to check for their login status I use:

Sys.Services.AuthenticationService.get_isLoggedIn()

But is there a way to get the User object through JavaScript? I can easily get it on server-side using:

MembershipUser u = Membership.GetUser();

But I am looking for a way to do the above in JavaScript (if possible).

Thanks.


Solution

  • You have to serialize the data to a Javascript variable.

    (Warning: Typed on-the-fly. Didn't feel like logging into work to run this through the editor.)

    // Copy the important user fields over.
    
    var UserInfo = new {
      field1 = "field1"
    };
    
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    <script>
      var userInfo = <%= serializer.Serialize(UserInfo ) %>
    </script>