ASP.NET Membership – Get UserID from Username

ASP.NET Comments Off on ASP.NET Membership – Get UserID from Username

To Get the UserID or UserKey of the logged in User from the ASP.NET membership tables the following snippet will work.

First add the Label Control in the .aspx page

<asp:Label ID="Label_UserInfo" runat="server" Text=""></asp:Label>

Now the snippet in the code behind. In the snippet

User.Identity.Name gets the current loggedin user name of the asp.net membership provider.


MembershipUser CurrentUser = Membership.GetUser(User.Identity.Name);

string UserID = CurrentUser.ProviderUserKey.ToString();

Label_UserInfo.Text = User.Identity.Name + " : " + UserID;

UserID provides the current logged in userid.

To use the MembershipUser you should have System.Web.Security Namespace


using System.Web.Security;



Author

Search

Back to Top