jQuery and ASP.NET

May 18, 2009

Get UserID of the user using ASP.NET Membership




Retreiving the UserId for a user while using the ASP.NET Login controls is very simple, yet it still is one of the most frequently asked questions on the forums. Here's how to do so:

C#


    protected void Page_Load(object sender, EventArgs e)


    {


        System.Web.Security.MembershipUser mu = System.Web.Security.Membership.GetUser();


        string strUsrId = mu.ProviderUserKey.ToString();       


    }




VB.NET


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)


        Dim mu As System.Web.Security.MembershipUser = System.Web.Security.Membership.GetUser()


        Dim strUsrId As String = mu.ProviderUserKey.ToString()


    End Sub




'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

2 Responses to "Get UserID of the user using ASP.NET Membership"
  1. JD said...
    July 22, 2009 1:00 PM

    When I run the C# code, it gives an error:


    Object reference not set to an instance of an object.


    Which doesnt make sense..

    Its not triggering till after logging in to the site. Any ideas?

  2. alkhabr said...
    August 6, 2009 1:11 AM

    i think it should be
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    Dim mu As System.Web.Security.MembershipUser = System.Web.Security.Membership.GetUser("user name")

    Dim strUsrId As String = mu.ProviderUserKey.ToString()

    End Sub

 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions