title: How to pass a claim in a cookie in ASP.NET Identity?
body: <p>I am trying to save a <code>Guid</code> value in a cookie. The way I want to save it is by setting a <code>Claim</code> for the user and getting it back in every controller for validation purposes.</p>
  
  <p><strong>Before going on the subject I have seen this question <a href="https://stackoverflow.com/questions/20383955/how-to-add-claims-in-asp-net-identity">here</a> but it did not solve my problem!</strong></p>
  
  <p>Here is my code in login controller:</p>
  
  <pre><code> public async Task&lt;ActionResult&gt; Login_Post(LoginViewModel loginViewModel)
      {
          if (ModelState.IsValid)
          {
              var user = IdentityManager.UserManager.FindByName(loginViewModel.Username);
              if (user != null)
              {
                  IdentityManager.SignInManager.AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                  var status = SignInStatus.Failure;
                  if (!user.IsApproved)
                  {
                      status = SignInStatus.Failure;
                  }
                  if (!await IdentityManager.UserManager.IsEmailConfirmedAsync(user.Id))
                  {
                      status = SignInStatus.EmailNotConfirmed;
                  }
                  status = (SignInStatus)await IdentityManager.SignInManager
                      .PasswordSignInAsync(loginViewModel.Username, loginViewModel.Password, loginViewModel.RememberMe, true);
                  if (status == SignInStatus.Failure)
                  {
                      await IdentityManager.UserManager.AccessFailedAsync(user.Id);
                      if (await IdentityManager.UserManager.IsLockedOutAsync(user.Id))
                      {
                          status = SignInStatus.LockedOut;
                      }
                  }
                  switch (status)
                  {
                      case SignInStatus.Success:
                          var identity = await IdentityManager.UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
                          var permissions = IdentityManager.ApplicationDbContext.UserVesselPermissions(Guid.Parse(identity.GetUserId()), DateTime.Now).ToList();
                          var userVesselPermissionsResult = permissions.FirstOrDefault();
                          identity.AddClaim(userVesselPermissionsResult != null
                              ? new Claim(SealogicalClaimUtility.VesselId, userVesselPermissionsResult.VesselId.ToString())
                              : new Claim(SealogicalClaimUtility.VesselId, ""));
                          IdentityManager.SignInManager.AuthenticationManager.SignIn(identity);
                          return RedirectToAction("Index", "HomeDashboard", new {area = "Dashboard"});
                      case SignInStatus.Failure:
                          ModelState.AddModelError("", "Login failed, username or password is incorrect.");
                          break;
                      case SignInStatus.RequiresVerification:
                          ModelState.AddModelError("", "Your account needs verification");
                          break;
                      case SignInStatus.LockedOut:
                          ModelState.AddModelError("",
                              "Login failed, this account has been locked. Contact administrator for further information.");
                          break;
                      case SignInStatus.EmailNotConfirmed:
                          ModelState.AddModelError("",
                              "Login failed, activate this account by clicking the email sent to your email account.");
                          break;
                      default:
                          ModelState.AddModelError("",
                              "Login failed, unkwon error. Contact administrator for further information");
                          break;
                  }
              }
          }
          return View("Login", loginViewModel);
      }
  </code></pre>
  
  <p>I am using <code>PasswordSignInAsync()</code> method for login validation because I need to get back that <code>SignInStatus</code> in order show a proper message to the user.</p>
  
  <p>After that I read this question in <a href="https://forums.asp.net/t/2104011.aspx?How%20to%20add%20multiples%20claims%20during%20login%20in%20asp%20net%20mvc%205" rel="nofollow noreferrer">here</a> and they say to create a <code>ClaimsIdentity</code> and then use again the <code>AuthenticationManager</code> to sign in with that <code>ClaimsIdentity</code> object as I did here:</p>
  
  <pre><code>case SignInStatus.Success:
       var identity = await IdentityManager.UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
       var permissions = IdentityManager.ApplicationDbContext.UserVesselPermissions(Guid.Parse(identity.GetUserId()), DateTime.Now).ToList();
       var userVesselPermissionsResult = permissions.FirstOrDefault();
       identity.AddClaim(userVesselPermissionsResult != null
       ? new Claim(SealogicalClaimUtility.VesselId, userVesselPermissionsResult.VesselId.ToString())
       : new Claim(SealogicalClaimUtility.VesselId, ""));
       IdentityManager.SignInManager.AuthenticationManager.SignIn(identity);
       return RedirectToAction("Index", "HomeDashboard", new {area = "Dashboard"});
  </code></pre>
  
  <p><code>SealogicalClaimUtility</code> as a static class as below:</p>
  
  <pre><code>public static class SealogicalClaimUtility 
  {
       public const string VesselId = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/vesselid";
  }
  </code></pre>
  
  <p>Until now everything is working fine but when I try to get back that <code>Claim</code> it does not exist at all.</p>
  
  <p>To get back that clam I use a custom property as below:</p>
  
  <pre><code>    public static Guid? UserSelectedVesselId
      {
          get
          {
              var hasVesselClaim = ((ClaimsIdentity) HttpContext.Current.User.Identity)
                  .HasClaim(claim =&gt; claim.Type == SealogicalClaimUtility.VesselId);
              if (hasVesselClaim)
              {
                  var vesselClaim = ((ClaimsIdentity) HttpContext.Current.User.Identity).Claims.FirstOrDefault(
                      claim =&gt; claim.Type == SealogicalClaimUtility.VesselId);
                  if (vesselClaim != null)
                  {
                      Guid vesselId;
                      if (Guid.TryParse(vesselClaim.Value, out vesselId))
                      {
                          return vesselId;
                      }
                  }
                  return null;
              }
              var currentUserPermission = UserVesselPermissions.FirstOrDefault();
              return currentUserPermission?.VesselId;
          }
      }
  </code></pre>
  
  <p>Any idea what I am doing wrong in here ?</p>
  
  <p>Thanks in advance!</p>
  
  <p><em>I am using ASP.NET Identity version 2</em></p>