Advertisement
Guest User

Untitled

a guest
May 24th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. using System.Linq;
  2. using System.Security.Claims;
  3. using Newtonsoft.Json;
  4.  
  5. namespace Account
  6. {
  7. public static class ClaimsExtensions
  8. {
  9. public static string Get(this ClaimsIdentity identity, string type)
  10. {
  11. return identity.Claims.FirstOrDefault(p => p.Type == type)?.Value;
  12. }
  13.  
  14. public static T Get<T>(this ClaimsIdentity identity, string type) where T : new()
  15. {
  16. var json = identity.Claims.FirstOrDefault(p => p.Type == type)?.Value;
  17. return json == null ? new T() : JsonConvert.DeserializeObject<T>(json);
  18. }
  19.  
  20. public static void Set(this ClaimsIdentity identity, string type, string value)
  21. {
  22. identity.AddClaim(new Claim(type, value));
  23. }
  24.  
  25. public static void Set<T>(this ClaimsIdentity identity, string type, T obj)
  26. {
  27. var json = JsonConvert.SerializeObject(obj);
  28. identity.AddClaim(new Claim(type, json));
  29. }
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement