Question

I am trying to get the currently logged in user's UserId as mentioned in this post http://msdn.microsoft.com/en-us/library/aa478949.aspx as part of the aspnet_Membership table.

Here's where I am looking:

    MembershipUser user = null;

    if (User != null && User.Identity != null && !string.IsNullOrEmpty(User.Identity.Name))
    {
        user = Membership.GetUser(User.Identity.Name);
    }

    if (user != null)
    {
        //int id = user.UserId;
    }

MembershipUser does not have a UserId. I am trying to user the UserId as a foreign key for other tables I will create. I can use the UserName, but I figured it may be better to use the UserId.

Was it helpful?

Solution

Try and see if the MembershipUser.ProvideUserKey Property works for you

From above link:

The ProviderUserKey property exposes the identifier from the membership data source typed as object. The type of the identifier depends on the MembershipProvider or the MembershipUser. In the case of the SqlMembershipProvider, the ProviderUserKey can be cast as a Guid, since the SqlMembershipProvider stores the user identifier as a UniqueIdentifier.

OTHER TIPS

Membership.GetUser().ProviderUserKey

GetUser() will get the current user, and ProviderUserKey will be the Id, it will come as an object, and you will want to cast it to what it really is, int, guid, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top