コード例 #1
0
 // TODO: Add unit tests
 public static AuthorizationPolicy Combine([NotNull] IEnumerable<AuthorizationPolicy> policies)
 {
     var builder = new AuthorizationPolicyBuilder();
     foreach (var policy in policies)
     {
         builder.Combine(policy);
     }
     return builder.Build();
 }
コード例 #2
0
        public static AuthorizationPolicy Combine(AuthorizationOptions options, IEnumerable <IAuthorizeData> attributes)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (attributes == null)
            {
                throw new ArgumentNullException(nameof(attributes));
            }

            var policyBuilder = new AuthorizationPolicyBuilder();
            var any           = false;

            foreach (var authorizeAttribute in attributes.OfType <AuthorizeAttribute>())
            {
                any = true;
                var useDefaultPolicy = true;
                if (!string.IsNullOrWhiteSpace(authorizeAttribute.Policy))
                {
                    var policy = options.GetPolicy(authorizeAttribute.Policy);
                    if (policy == null)
                    {
                        throw new InvalidOperationException(Resources.FormatException_AuthorizationPolicyNotFound(authorizeAttribute.Policy));
                    }
                    policyBuilder.Combine(policy);
                    useDefaultPolicy = false;
                }
                var rolesSplit = authorizeAttribute.Roles?.Split(',');
                if (rolesSplit != null && rolesSplit.Any())
                {
                    policyBuilder.RequireRole(rolesSplit);
                    useDefaultPolicy = false;
                }
                var authTypesSplit = authorizeAttribute.ActiveAuthenticationSchemes?.Split(',');
                if (authTypesSplit != null && authTypesSplit.Any())
                {
                    foreach (var authType in authTypesSplit)
                    {
                        policyBuilder.AuthenticationSchemes.Add(authType);
                    }
                }
                if (useDefaultPolicy)
                {
                    policyBuilder.Combine(options.DefaultPolicy);
                }
            }
            return(any ? policyBuilder.Build() : null);
        }
コード例 #3
0
        public static AuthorizationPolicy Combine(IEnumerable<AuthorizationPolicy> policies)
        {
            if (policies == null)
            {
                throw new ArgumentNullException(nameof(policies));
            }

            var builder = new AuthorizationPolicyBuilder();
            foreach (var policy in policies)
            {
                builder.Combine(policy);
            }
            return builder.Build();
        }
コード例 #4
0
        public static AuthorizationPolicy Combine(IEnumerable <AuthorizationPolicy> policies)
        {
            if (policies == null)
            {
                throw new ArgumentNullException(nameof(policies));
            }

            var builder = new AuthorizationPolicyBuilder();

            foreach (var policy in policies)
            {
                builder.Combine(policy);
            }
            return(builder.Build());
        }
コード例 #5
0
        public void AddPolicy(string name, Action <AuthorizationPolicyBuilder> configurePolicy)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (configurePolicy == null)
            {
                throw new ArgumentNullException(nameof(configurePolicy));
            }

            var policyBuilder = new AuthorizationPolicyBuilder();

            configurePolicy(policyBuilder);
            PolicyMap[name] = policyBuilder.Build();
        }
コード例 #6
0
        public static AuthorizationPolicy Combine([NotNull] AuthorizationOptions options, [NotNull] IEnumerable <AuthorizeAttribute> attributes)
        {
            var  policyBuilder = new AuthorizationPolicyBuilder();
            bool any           = false;

            foreach (var authorizeAttribute in attributes.OfType <AuthorizeAttribute>())
            {
                any = true;
                var requireAnyAuthenticated = true;
                if (!string.IsNullOrWhiteSpace(authorizeAttribute.Policy))
                {
                    var policy = options.GetPolicy(authorizeAttribute.Policy);
                    if (policy == null)
                    {
                        throw new InvalidOperationException(Resources.FormatException_AuthorizationPolicyNotFound(authorizeAttribute.Policy));
                    }
                    policyBuilder.Combine(policy);
                    requireAnyAuthenticated = false;
                }
                var rolesSplit = authorizeAttribute.Roles?.Split(',');
                if (rolesSplit != null && rolesSplit.Any())
                {
                    policyBuilder.RequireRole(rolesSplit);
                    requireAnyAuthenticated = false;
                }
                string[] authTypesSplit = authorizeAttribute.ActiveAuthenticationSchemes?.Split(',');
                if (authTypesSplit != null && authTypesSplit.Any())
                {
                    foreach (var authType in authTypesSplit)
                    {
                        policyBuilder.ActiveAuthenticationSchemes.Add(authType);
                    }
                }
                if (requireAnyAuthenticated)
                {
                    policyBuilder.RequireAuthenticatedUser();
                }
            }
            return(any ? policyBuilder.Build() : null);
        }
コード例 #7
0
 public static AuthorizationPolicy Combine([NotNull] AuthorizationOptions options, [NotNull] IEnumerable<AuthorizeAttribute> attributes)
 {
     var policyBuilder = new AuthorizationPolicyBuilder();
     bool any = false;
     foreach (var authorizeAttribute in attributes.OfType<AuthorizeAttribute>())
     {
         any = true;
         var requireAnyAuthenticated = true;
         if (!string.IsNullOrWhiteSpace(authorizeAttribute.Policy))
         {
             var policy = options.GetPolicy(authorizeAttribute.Policy);
             if (policy == null)
             {
                 throw new InvalidOperationException(Resources.FormatException_AuthorizationPolicyNotFound(authorizeAttribute.Policy));
             }
             policyBuilder.Combine(policy);
             requireAnyAuthenticated = false;
         }
         var rolesSplit = authorizeAttribute.Roles?.Split(',');
         if (rolesSplit != null && rolesSplit.Any())
         {
             policyBuilder.RequireRole(rolesSplit);
             requireAnyAuthenticated = false;
         }
         string[] authTypesSplit = authorizeAttribute.ActiveAuthenticationSchemes?.Split(',');
         if (authTypesSplit != null && authTypesSplit.Any())
         {
             foreach (var authType in authTypesSplit)
             {
                 policyBuilder.ActiveAuthenticationSchemes.Add(authType);
             }
         }
         if (requireAnyAuthenticated)
         {
             policyBuilder.RequireAuthenticatedUser();
         }
     }
     return any ? policyBuilder.Build() : null;
 }
コード例 #8
0
 public void AddPolicy([NotNull] string name, [NotNull] Action<AuthorizationPolicyBuilder> configurePolicy)
 {
     var policyBuilder = new AuthorizationPolicyBuilder();
     configurePolicy(policyBuilder);
     PolicyMap[name] = policyBuilder.Build();
 }
コード例 #9
0
        public static AuthorizationPolicy Combine(AuthorizationOptions options, IEnumerable<IAuthorizeData> attributes)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (attributes == null)
            {
                throw new ArgumentNullException(nameof(attributes));
            }

            var policyBuilder = new AuthorizationPolicyBuilder();
            var any = false;
            foreach (var authorizeAttribute in attributes.OfType<AuthorizeAttribute>())
            {
                any = true;
                var useDefaultPolicy = true;
                if (!string.IsNullOrWhiteSpace(authorizeAttribute.Policy))
                {
                    var policy = options.GetPolicy(authorizeAttribute.Policy);
                    if (policy == null)
                    {
                        throw new InvalidOperationException(Resources.FormatException_AuthorizationPolicyNotFound(authorizeAttribute.Policy));
                    }
                    policyBuilder.Combine(policy);
                    useDefaultPolicy = false;
                }
                var rolesSplit = authorizeAttribute.Roles?.Split(',');
                if (rolesSplit != null && rolesSplit.Any())
                {
                    policyBuilder.RequireRole(rolesSplit);
                    useDefaultPolicy = false;
                }
                var authTypesSplit = authorizeAttribute.ActiveAuthenticationSchemes?.Split(',');
                if (authTypesSplit != null && authTypesSplit.Any())
                {
                    foreach (var authType in authTypesSplit)
                    {
                        policyBuilder.AuthenticationSchemes.Add(authType);
                    }
                }
                if (useDefaultPolicy)
                {
                    policyBuilder.Combine(options.DefaultPolicy);
                }
            }
            return any ? policyBuilder.Build() : null;
        }