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()) { var trimmedRolesSplit = rolesSplit.Where(r => !string.IsNullOrWhiteSpace(r)).Select(r => r.Trim()); policyBuilder.RequireRole(trimmedRolesSplit); useDefaultPolicy = false; } var authTypesSplit = authorizeAttribute.ActiveAuthenticationSchemes?.Split(','); if (authTypesSplit != null && authTypesSplit.Any()) { foreach (var authType in authTypesSplit) { if (!string.IsNullOrWhiteSpace(authType)) { policyBuilder.AuthenticationSchemes.Add(authType.Trim()); } } } if (useDefaultPolicy) { policyBuilder.Combine(options.DefaultPolicy); } } return any ? policyBuilder.Build() : null; }
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(); }
/// <summary> /// Combines the specified <see cref="AuthorizationPolicy"/> into a single policy. /// </summary> /// <param name="policies">The authorization policies to combine.</param> /// <returns> /// A new <see cref="AuthorizationPolicy"/> which represents the combination of the /// specified <paramref name="policies"/>. /// </returns> 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()); }
public void Apply(AuthorizationOptions options) { foreach (var item in _policy1) { var builder = new AuthorizationPolicyBuilder(); item.Value.Invoke(builder); options.AddPolicy(item.Key, builder.Build()); } foreach (var item in _policy2) { var builder = new AcceptancePolicyBuilder(); item.Value.ForEach(a => a.Invoke(builder)); options.AddPolicy(item.Key, builder.Build()); } }
/// <summary> /// Add a policy that is built from a delegate with the provided name. /// </summary> /// <param name="name">The name of the policy.</param> /// <param name="configurePolicy">The delegate that will be used to build the policy.</param> 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(); }
/// <summary> /// Add a policy that is built from a delegate with the provided name. /// </summary> /// <param name="name">The name of the policy.</param> /// <param name="configurePolicy">The delegate that will be used to build the policy.</param> 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(); }