Пример #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddDefaultIdentity <ApplicationUser>(options =>
            {
                options.SignIn.RequireConfirmedAccount  = false;
                options.Password.RequiredLength         = 6;
                options.Password.RequireUppercase       = false;
                options.Password.RequireLowercase       = false;
                options.Password.RequireDigit           = false;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequiredUniqueChars    = 0;
                options.ClaimsIdentity.UserIdClaimType  = "UserID";
            })
            .AddRoles <IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>();

            services.AddAuthentication(options =>
            {
                //options.DefaultAuthenticateScheme = "ClientCookie";
                //options.DefaultSignInScheme = "ClientCookie";
                //options.DefaultChallengeScheme = "Server";
            })
            .AddCookie("ClientCookie", options => options.SlidingExpiration = true)
            .AddJwtBearer("Server", options =>
            {
                options.RequireHttpsMetadata      = false;
                options.SaveToken                 = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer      = SD.Issuer,
                    ValidAudience    = SD.Audience,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SD.SecretKey)),
                    ClockSkew        = TimeSpan.Zero
                };
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy(Policies.User, Policies.UserPolicy());
                options.AddPolicy(Policies.Admin, Policies.AdminPolicy());
                options.AddPolicy(Policies.UserAndAdmin, Policies.UserAndAdminPolicy());
                options.AddPolicy(Policies.Adult, Policies.AdultPolicy());
            });

            services.AddScoped <IAuthorizationHandler, AdultsOnlyHandler>();

            services.AddScoped <IUnitOfWork, UnitOfWork>();
            services.AddScoped <IDataService, DataService>();
            services.AddScoped <IShopService, ShopService>();
            services.AddControllersWithViews().AddRazorRuntimeCompilation();
        }