コード例 #1
0
        public static IMvcBuilder AddIdentityApi <TUser, TRole, TTenant, TKey>(this IMvcBuilder mvc,
                                                                               IConfiguration identityConfig, IConfiguration securityConfig, Action <MvcOptions> setupAction = null)
            where TUser : IdentityUserExtended <TKey>
            where TRole : IdentityRoleExtended <TKey>
            where TTenant : IdentityTenant
            where TKey : IEquatable <TKey>
        {
            var services = mvc.Services;

            var options = new SecurityOptions();

            securityConfig.Bind(options);

            services.AddAuthorization(x =>
            {
                x.AddPolicy(Constants.Security.Policies.ManageUsers,
                            b =>
                {
                    b.RequireClaimExtended(services, options, options.Claims.PermissionClaim,
                                           ClaimValues.ManageUsers);
                });
                x.AddPolicy(Constants.Security.Policies.ManageRoles,
                            b =>
                {
                    b.RequireClaimExtended(services, options, options.Claims.PermissionClaim,
                                           ClaimValues.ManageRoles);
                });
                x.AddPolicy(Constants.Security.Policies.ManageTenants,
                            b =>
                {
                    b.RequireClaimExtended(services, options, options.Claims.PermissionClaim,
                                           ClaimValues.ManageTenants);
                });
            });

            services.Configure <IdentityApiOptions>(identityConfig);
            services.Configure <RazorViewEngineOptions>(x => { x.ViewLocationExpanders.Add(new DynamicViewLocationExpander <TUser>()); });

            mvc.AddControllers <TUser, TRole, TTenant, TKey>();
            services.AddSingleton <IDynamicComponent>(r =>
            {
                var o = r.GetRequiredService <IOptions <IdentityApiOptions> >();
                return(new IdentityApiComponent
                {
                    Namespace = () => o.Value.RootPath ?? string.Empty
                });
            });
            services.AddSingleton <IDynamicComponent>(r =>
            {
                var o = r.GetRequiredService <IOptions <SecurityOptions> >();
                return(new TokensComponent
                {
                    Namespace = () => o.Value.Tokens?.Path ?? string.Empty
                });
            });

            return(mvc);
        }