Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- using System.Security.Claims;
- using Newtonsoft.Json;
- namespace Account
- {
- public static class ClaimsExtensions
- {
- public static string Get(this ClaimsIdentity identity, string type)
- {
- return identity.Claims.FirstOrDefault(p => p.Type == type)?.Value;
- }
- public static T Get<T>(this ClaimsIdentity identity, string type) where T : new()
- {
- var json = identity.Claims.FirstOrDefault(p => p.Type == type)?.Value;
- return json == null ? new T() : JsonConvert.DeserializeObject<T>(json);
- }
- public static void Set(this ClaimsIdentity identity, string type, string value)
- {
- identity.AddClaim(new Claim(type, value));
- }
- public static void Set<T>(this ClaimsIdentity identity, string type, T obj)
- {
- var json = JsonConvert.SerializeObject(obj);
- identity.AddClaim(new Claim(type, json));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement