ASP.NET SQL Membership Provider (How to get the GUID of current user?)

Here is an example showing how you can get the GUID of the current logged in user in a controller using C#.


Step 1:

Firstly in your controller you need to include the namespace System.Web.Security.

Example:
using System.Web.Security;

You need this namespace because we will be using the Membership class to get the Membership information of the current logged in user. The Membership class resides within the System.Web.Security namespace.


Step 2:

This step shows a simple way, how you can retrieve the GUID of the current logged in user.

In your controller you can simply declare a variable to store the MembershipUser (shown in example below) by calling the Membership.GetUser(string method) method. We need to use User.Identity.Name property to get the username of the the current user and then input this username into the GetUser method.

Example:  GetUser(User.Identity.Name). The User.Identity.Name is under  System.Security.Principal
namespace.

Step 3:

So, now you will have something like var membershipUser = Membership.GetUser(User.Identity.Name).
This will get you the MembershipUser class object. This basically lets you access and update the user's membership information stored in the Membership data storage. You can then access the ProviderUserKey property, which will give you an object of GUID (this GUID is stored in SQL database as uniqueidentifier).

Example: var userGuid = membershipUser.ProviderUserKey;


Final step:

Finally you need to cast the object to GUID in order to be able to use the user's GUID.

Example: (Guid)userGuid.

If you do casting like this:  var userGuid = (Guid)membershipUser.ProviderUserKey; then it is risky because if the membershipUser is null or not present in the database, then a null reference error will occur. So you need to cast GUID when you are actually using the variable.


Working example:

This code shows a basic example. In this example you can cast the MembershipUser.ProviderUserKey to GUID straightaway, because we are using conditional statements to check that the membership user and the provider key is not null


    public ActionResult Index() {
        var membershipUser = Membership.GetUser(User.Identity.Name); //Get the  membership info
        if (membershipUser != null)//Check if user information is not null
        {
            if (membershipUser.ProviderUserKey != null)//Check if provider key is available
            {
                var userGuid = (Guid)membershipUser.ProviderUserKey; //Get key and cast to GUID
            }
        }
        return View();
    }

A more advance example. 

This example shows how you can check if the current logged in user has an existing profile in your website, and return the view containing the profile model.

Assuming that you have a Profile table in your website database and the primary key of the Profile table is UserGuid as uniqueidentifier. Also, assuming that you have a query called GetProfile(GUID userGuid) implemented in the ProfileRepository class, that returns the profile of the user with the particular GUID.

    [Authorize]
    public ActionResult Index() {
        var membershipUser = Membership.GetUser(User.Identity.Name);
        if (membershipUser != null) {
            if (membershipUser.ProviderUserKey != null) {
                var profile = _profile.GetProfile((Guid)membershipUser.ProviderUserKey);
                return View(profile);
            }
        }
        return View("Error");
    }
    


Hope this helps :) our fellow beginners to grasp some basic understanding of using ASP.NET Membership.