Exemple #1
0
        /// <summary>
        /// Adds singleton authorization handler into <see cref="IServiceCollection"/> and add policy with given <see cref="PcIdentityRequirement{TRole}"/>
        /// In order to use the policy use <see cref="AuthorizeAttribute"/> with name of policy
        /// The policy by default check if any of given roles fits the Policy
        /// </summary>
        /// <typeparam name="TRole">Role must inherit <see cref="Role"/></typeparam>
        /// <param name="this"><see cref="IServiceCollection"/></param>
        /// <param name="policyName">Name of policy which is used in <see cref="AuthorizeAttribute"/> as policy parameter</param>
        /// <param name="injectionType">Registration of given handler in ASP.NET Core DI. By default its scoped</param>
        /// <returns><see cref="IServiceCollection"/></returns>
        public static IServiceCollection AddPcIdentityPolicy <TRole>(this IServiceCollection @this, string policyName, AuthorizationHandlerInjectionType injectionType = AuthorizationHandlerInjectionType.Scoped)
            where TRole : Role, new()
        {
            var requirement = new PcIdentityRequirement <TRole>();

            if (injectionType == AuthorizationHandlerInjectionType.Scoped)
            {
                @this.AddScoped <IAuthorizationHandler, PcIdentityHandler <TRole> >();
            }
            else
            {
                @this.AddSingleton <IAuthorizationHandler, PcIdentityHandler <TRole> >();
            }

            @this.AddAuthorizationCore(options => options.AddPolicy(policyName, policy => policy.Requirements.Add(requirement)));

            return(@this);
        }
Exemple #2
0
        /// <summary>
        /// Adds singleton authorization handler into <see cref="IServiceCollection"/> and add policy with given <see cref="PcIdentityRequirement{TRole}"/>
        /// In order to use the policy use <see cref="AuthorizeAttribute"/> with name of policy
        /// The policy by default check if any of given roles fits the Policy
        /// </summary>
        /// <typeparam name="T1">Role must inherit <see cref="Role"/></typeparam>
        /// <typeparam name="T2">Role must inherit <see cref="Role"/></typeparam>
        /// <typeparam name="T3">Role must inherit <see cref="Role"/></typeparam>
        /// <typeparam name="T4">Role must inherit <see cref="Role"/></typeparam>
        /// <typeparam name="T5">Role must inherit <see cref="Role"/></typeparam>
        /// <typeparam name="T6">Role must inherit <see cref="Role"/></typeparam>
        /// <typeparam name="T7">Role must inherit <see cref="Role"/></typeparam>
        /// <typeparam name="T8">Role must inherit <see cref="Role"/></typeparam>
        /// <param name="this"><see cref="IServiceCollection"/></param>
        /// <param name="policyName">Name of policy which is used in <see cref="AuthorizeAttribute"/> as policy parameter</param>
        /// <param name="injectionType">Registration of given handler in ASP.NET Core DI. By default its scoped</param>
        public static IServiceCollection AddPcMultiRoleIdentityPolicy <T1, T2, T3, T4, T5, T6, T7, T8>(this IServiceCollection @this, string policyName, AuthorizationHandlerInjectionType injectionType = AuthorizationHandlerInjectionType.Scoped)
            where T1 : Role, new()
            where T2 : Role, new()
            where T3 : Role, new()
            where T4 : Role, new()
            where T5 : Role, new()
            where T6 : Role, new()
            where T7 : Role, new()
            where T8 : Role, new()

        {
            return(AddPcMultiRoleIdentityPolicy(@this, policyName, new List <Role> {
                new T1(), new T2(), new T3(), new T4(), new T5(), new T6(), new T7(), new T8()
            }, injectionType));
        }
Exemple #3
0
        internal static IServiceCollection AddPcMultiRoleIdentityPolicy(this IServiceCollection @this, string policyName, IList <Role> roles, AuthorizationHandlerInjectionType injectionType = AuthorizationHandlerInjectionType.Scoped)
        {
            var requirement = new PcMultiIdentityRequirement(roles);

            if (injectionType == AuthorizationHandlerInjectionType.Scoped)
            {
                @this.AddScoped <IAuthorizationHandler, PcMultiIdentityHandler>();
            }
            else
            {
                @this.AddSingleton <IAuthorizationHandler, PcMultiIdentityHandler>();
            }

            @this.AddAuthorizationCore(options => options.AddPolicy(policyName, policy => policy.Requirements.Add(requirement)));

            return(@this);
        }
Exemple #4
0
 /// <summary>
 /// Adds singleton authorization handler into <see cref="IServiceCollection"/> and add policy with given <see cref="PcIdentityRequirement{TRole}"/>
 /// In order to use the policy use <see cref="AuthorizeAttribute"/> with name of policy
 /// The policy by default check if any of given roles fits the Policy
 /// </summary>
 /// <typeparam name="T1">Role must inherit <see cref="Role"/></typeparam>
 /// <typeparam name="T2">Role must inherit <see cref="Role"/></typeparam>
 /// <param name="this"><see cref="IServiceCollection"/></param>
 /// <param name="policyName">Name of policy which is used in <see cref="AuthorizeAttribute"/> as policy parameter</param>
 /// <param name="injectionType">Registration of given handler in ASP.NET Core DI. By default its scoped</param>
 /// <returns><see cref="IServiceCollection"/></returns>
 public static IServiceCollection AddPcMultiRoleIdentityPolicy <T1, T2>(this IServiceCollection @this, string policyName, AuthorizationHandlerInjectionType injectionType = AuthorizationHandlerInjectionType.Scoped)
     where T1 : Role, new()
     where T2 : Role, new()
 {
     return(AddPcMultiRoleIdentityPolicy(@this, policyName, new List <Role> {
         new T1(), new T2()
     }, injectionType));
 }