Exemplo n.º 1
0
 private void ConfigureAuthorization(IEndpointConventionBuilder builder)
 {
     bool.TryParse(_config["enableCertAuth"], out var enableCertAuth);
     if (enableCertAuth)
     {
         builder.RequireAuthorization();
     }
 }
        /// <summary>
        /// Adds the default authorization policy to the endpoint(s).
        /// </summary>
        /// <param name="builder">The endpoint convention builder.</param>
        /// <returns>The original convention builder parameter.</returns>
        public static IEndpointConventionBuilder RequireAuthorization(this IEndpointConventionBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.RequireAuthorization(new AuthorizeAttribute()));
        }
Exemplo n.º 3
0
        private void ConfigureAuthorization(IEndpointConventionBuilder builder)
        {
#if NET5_0_OR_GREATER
            bool.TryParse(_config["enableCertAuth"], out var enableCertAuth);
            if (enableCertAuth)
            {
                builder.RequireAuthorization();
            }
#endif
        }
Exemplo n.º 4
0
    private static void ApplyAuthRolesAndPolicies(IEndpointConventionBuilder builder, bool auth, string roles,
                                                  params string[] policies)
    {
        if (policies is not null && policies.Any())
        {
            builder.RequireAuthorization(policies);
            return;
        }

        var hasRoles  = !string.IsNullOrWhiteSpace(roles);
        var authorize = new AuthorizeAttribute();

        if (hasRoles)
        {
            authorize.Roles = roles;
        }

        if (auth || hasRoles)
        {
            builder.RequireAuthorization(authorize);
        }
    }
        public static IEndpointConventionBuilder RequireAuthorization(this IEndpointConventionBuilder builder, params string[] policyNames)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

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

            return(builder.RequireAuthorization(policyNames.Select(n => new AuthorizeAttribute(n)).ToArray()));
        }
Exemplo n.º 6
0
 public static IEndpointConventionBuilder RequireRoles(this IEndpointConventionBuilder builder, string roles)
 {
     return(builder.RequireAuthorization(new AuthorizeAttribute {
         Roles = roles
     }));
 }