Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        private ControllersValidator GetCtrlValidator()
        {
            var store           = new AuthorizationPolicyStore();
            var paramValidator  = new ParametersValidator();
            var actionValidator = new ActionsValidator(paramValidator, store);

            return(new ControllersValidator(actionValidator, store));
        }
Exemplo n.º 3
0
        /// <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);
        }