title: Getting userId of logged in user returns null
body: <p>I am designing an ASP.net MVC4 application and I need to get the current user ID so that I'll save it in a table in my database as an owner id for a group.</p>
  
  <p>This is the code I am using:</p>
  
  <pre><code>public ActionResult CreateGroup(string groupName, string description)
  {
         if (Request.IsAuthenticated)
         {
             Group group = new Group();
             Random random = new Random();
             int groupId = random.Next(1, 2147483647);
  
             using (CodeShareEntities conn = new CodeShareEntities())
             {
                 try
                 {
                          int ownerId = (int) Membership.GetUser(System.Web.HttpContext.Current.User.Identity.Name).ProviderUserKey;
  
                          group.GroupId = groupId;
                          group.GroupName = groupName;
                          group.Description = description;
                          group.OwnerId = ownerId;
  
                          conn.Group.Add(group);
                          conn.SaveChanges();
                      }
                      catch (Exception e)
                      {
                          ModelState.AddModelError("", e.Message);
                      }
                  }
  
                  return View("ThisGroup");
            }
            else 
            {
            return View("../Shared/NotLoggedIn");
    }
  </code></pre>
  
  <p>I am getting a NullReferenceException on this line:</p>
  
  <pre><code>int ownerId = (int) Membership.GetUser(System.Web.HttpContext.Current.User.Identity.Name).ProviderUserKey;
  </code></pre>
  
  <p>although the username is returning correctly. What can be the problem? I am still new to web applications and I really can't figure this out!</p>
  
  <p>Thanks </p>