title: Custom Authentication & Update Claims in ASP.NET CORE
body: <p>I am developing web site with ASP.NET CORE that uses Claims for User Authentication and User Id and other infos keeps in Claims, is it security ?  </p>
  
  <pre><code>ClaimsIdentity identity = new ClaimsIdentity(
                  new[]
                  {
                      new Claim(ClaimTypes.Name, userInfo.Name),
                      new Claim(ClaimTypes.Surname, userInfo.Surname),
                      new Claim("Image", userInfo.Image),
                      new Claim(ClaimTypes.NameIdentifier,result.Id.ToString()),
                      new Claim(ClaimTypes.IsPersistent, loginViewModel.RememberMe.ToString())
                  },
                  CookieName.User);
              HttpContext.SignOutAsync(CookieName.User).Wait();
              HttpContext.SignInAsync(CookieName.User, new ClaimsPrincipal(identity),
                  new AuthenticationProperties
                  {
                      IsPersistent = loginViewModel.RememberMe,
                      AllowRefresh = true
                  }).Wait();
  </code></pre>
  
  <p>Sometime i need change user infos and it uses this. is it secure way ?</p>
  
  <pre><code>//Get 
  int id = int.Parse(new ClaimsCookie(HttpContext).GetValue(CookieName.User, KeyName.Id));
  
  //Set Update
  new ClaimsCookie(HttpContext).SetValue(CookieName.User, new[] { KeyName.Name, KeyName.Surname }, new[] { model.Name, model.Surname });
  </code></pre>
  
  <p>Class :</p>
  
  <pre><code>namespace ...
  {
      public class ClaimsCookie
      {
          private readonly HttpContext _httpContext;
          public ClaimsCookie(HttpContext httpContext)
          {
              _httpContext = httpContext;
          }
  
          public string GetValue(string cookieName, string keyName)
          {
              var principal = _httpContext.User;
              var cp = principal.Identities.First(i =&gt; i.AuthenticationType == cookieName.ToString());
              return cp.FindFirst(keyName).Value;
          }
          public async void SetValue(string cookieName, string[] keyName, string[] value)
          {
              if (keyName.Length != value.Length)
              {
                  return;
              }
              if (_httpContext == null)
                  return;
              var principal = _httpContext.User;
              var cp = principal.Identities.First(i =&gt; i.AuthenticationType == cookieName.ToString());
              for (int i = 0; i &lt; keyName.Length; i++)
              {
                  if (cp.FindFirst(keyName[i]) != null)
                  {
                      cp.RemoveClaim(cp.FindFirst(keyName[i]));
                      cp.AddClaim(new Claim(keyName[i], value[i]));
                  }
  
              }
              await _httpContext.SignOutAsync(cookieName);
              await _httpContext.SignInAsync(cookieName, new ClaimsPrincipal(cp),
                  new AuthenticationProperties
                  {
                      IsPersistent = bool.Parse(cp.FindFirst(KeyName.IsPersistent).Value),
                      AllowRefresh = true
                  });
          }
          public async void SetValue(string cookieName, string keyName, string value)
          {
              var principal = _httpContext.User;
              var cp = principal.Identities.First(i =&gt; i.AuthenticationType == cookieName.ToString());
  
              if (cp.FindFirst(keyName) != null)
              {
                  cp.RemoveClaim(cp.FindFirst(keyName));
                  cp.AddClaim(new Claim(keyName, value));
              }
              await _httpContext.SignOutAsync(cookieName);
              await _httpContext.SignInAsync(cookieName, new ClaimsPrincipal(cp),
                  new AuthenticationProperties
                  {
                      IsPersistent = bool.Parse(cp.FindFirst(KeyName.IsPersistent).Value),
                      AllowRefresh = true
                  });
          }
      }
      public static class CookieName
      {
          public static string Company =&gt; "CompanyUserProfilCookie";
          public static string User =&gt; "UserProfilCookie";
          public static string Admin =&gt; "AdminPanelCookie";
      }
  
      public static class KeyName
      {
          public static string Id =&gt; ClaimTypes.NameIdentifier;
          public static string Name =&gt; ClaimTypes.Name;
          public static string Surname =&gt; ClaimTypes.Surname;
          public static string IsPersistent =&gt; ClaimTypes.IsPersistent;
          public static string Image =&gt; "Image";
      }
  }
  </code></pre>
  
  <p>I am setting HttpContext to this class from any Controller. Have any way static HttpContext, i dont want set from Controller ?</p>