Beispiel #1
0
        /// <summary>
        /// Builds a new <see cref="AuthorizationPolicy"/> from the requirements in this instance.
        /// </summary>
        /// <returns>A new <see cref="AuthorizationPolicy"/> built from the requirements in this instance.</returns>
        public AuthorizationPolicy Build()
        {
            var partialRequirements = new List <IAuthorizationRequirement>(_requirements);

            if (_roles != null)
            {
                partialRequirements.Add(new Infrastructure.RolesAuthorizationRequirement(_roles));
            }

            foreach (var(claimType, acceptValues) in _claims)
            {
                partialRequirements.Add(new Infrastructure.ClaimsAuthorizationRequirement(claimType, acceptValues));
            }

            if (partialRequirements.Count == 1)
            {
                _builder.AddRequirements(partialRequirements[0]);
            }
            else if (partialRequirements.Count > 1)
            {
                _builder.AddRequirements(new Infrastructure.AcceptanceRequirement(partialRequirements));
            }

            return(_builder.Build());
        }
Beispiel #2
0
        /// <summary>
        /// Adds a requirement that requires a scope claim containing at least one scope who's value matches at least one item in <paramref name="scopes"/>
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="scopes">The scopes.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">builder</exception>
        public static AuthorizationPolicyBuilder AddScopeRequirement(this AuthorizationPolicyBuilder builder, params string[] scopes)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddRequirements(new ScopeAuthorizationRequirement(scopes)));
        }
Beispiel #3
0
 public static AuthorizationPolicyBuilder RequireMinAge(this AuthorizationPolicyBuilder builder, int minAge)
 {
     return(builder.AddRequirements(new MinimumAgeRequirement(minAge)));
 }