Beispiel #1
0
        public static void AddTestServices(this IServiceCollection services)
        {
            services.AddSingleton <Microsoft.AspNetCore.Authorization.IAuthorizationHandler, TestPolicyHandler>();
            services.AddSingleton <Microsoft.AspNetCore.Authorization.IAuthorizationHandler, TestMultiplePolicyHandler>();

            //services.AddTransient<Cryptography>( provider => { return new Cryptography(DateTime.Now.Millisecond, 128); } );
            //services.AddTransient<Cryptography>();
            //services.AddScoped<DbContext, IdentityContext>( provider => { return new IdentityContext(provider); } );

            services.AddAuthorization(options => {
                // options.AddPolicy("MustBeMOA995", policy => {
                //     policy.AddRequirements(new TestPolicyRequirement("moa995"));
                // });
            });
            services.Configure <Microsoft.AspNetCore.Authorization.AuthorizationOptions>(options => {
                options.AddPolicy("MustBeMOA995", policy => {
                    policy.Requirements.Add(new TestPolicyRequirement("moa995"));
                });
                options.AddPolicy("MustBeAuthenticated", policy => {
                    policy.RequireAuthenticatedUser();
                });
                options.AddPolicy("MustHaveAdminClaim", policy => {
                    policy.RequireClaim(ClaimTypes.Role, "admin");
                });
                options.AddPolicy("MustHaveMOA995Claim", policy => {
                    policy.RequireClaim(ClaimTypes.Name, "moa995");
                });
            });

            //services.AddMvc().AddMvcOptions(opt => { opt.RequireHttpsPermanent = true; });

            // services.AddMvcCore( options => {
            //     options.Filters.Add<TestActionFilter>();
            //     options.Filters.Add<TestAuthorizationFilter>();
            // } ).AddViews().AddJsonFormatters().AddRazorPages().AddRazorViewEngine();


            services.AddMvcCore().AddViews().AddJsonFormatters().AddRazorPages().AddRazorViewEngine().AddAuthorization();
            services.Configure <Microsoft.AspNetCore.Mvc.MvcOptions>(options => {
                options.Filters.Add <TestActionFilter>();
                options.Filters.Add <TestAuthorizationFilter>();
                //options.RequireHttpsPermanent = true;
            });

            services.AddSingleton <IdentityContext>(provider =>
            {
                var db = new IdentityContext();
                db.Database.EnsureCreated();
                return(db);
            });
            // services.AddIdentity<TestUserIdentity, IdentityRole>( options => {
            //     options.Lockout.MaxFailedAccessAttempts = 3;
            // } ).AddUserStore<TestUserStore>().AddUserValidator<TestUserValidator>();
            services.AddTransient <IPasswordHasher <TestUserIdentity>, PasswordHasher <TestUserIdentity> >();
            services.AddTransient <TestUserStore>(provider => {
                return(new TestUserStore(provider.GetService <IdentityContext>()));
            });
            services.AddScoped <PasswordValidator <TestUserIdentity> >();
            services.AddIdentityCore <TestUserIdentity>(config => {
                config.Lockout.MaxFailedAccessAttempts = 3;
            }).AddUserStore <TestUserStore>();

            //services.AddTransient<TestUserValidator>();
            //services.AddTransient<TestUserStore>( provider => { return new TestUserStore(provider.GetService<IdentityContext>()); } );

            // services.Configure<IdentityOptions>( options => {
            //     options.Lockout.MaxFailedAccessAttempts = 3;
            // } );
        }
 public TestUserStore(IdentityContext db)
 {
     this._db = db;
 }