public static IAuthorizationPolicyStore GetPolicyStore() { IAuthorizationPolicyStore store = new AuthorizationPolicyStore(); store.SetPolicy("Over16", user => { var claim = user.Claims.FirstOrDefault(x => x.Type == "age"); if (claim != null) { int val; if (int.TryParse(claim.Value, out val)) { return(val >= 16); } } return(false); }); store.SetPolicy("Over18", user => { var claim = user.Claims.FirstOrDefault(x => x.Type == "age"); if (claim != null) { int val; if (int.TryParse(claim.Value, out val)) { return(val >= 18); } } return(false); }); return(store); }
/// <summary> /// Adds the authorization policy. /// </summary> /// <param name="name">The name.</param> /// <param name="policy">The policy</param> /// <returns>This instance</returns> public LiteApiOptions AddAuthorizationPolicy(string name, Func <ClaimsPrincipal, bool> policy) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("name cannot be null or empty or whitespace"); } if (policy == null) { throw new ArgumentNullException(nameof(policy)); } AuthorizationPolicyStore.SetPolicy(name, policy); return(this); }