Esempio n. 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);
        }
Esempio n. 2
0
 protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PcIdentityRequirement <AdminRole> requirement)
 {
     return(base.HandleRequirementAsync(context, requirement));
 }