Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var authenticationOptions = new AuthenticationOptions(Configuration);

            services.AddSingleton(authenticationOptions);

            services.AddTransient <IAuthenticationService, AuthenticationService>();
            services.AddTransient <IUsersService, UsersService>();
            services.AddTransient <IUserGroupsService, UserGroupsService>();

            services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetSection("databaseConnectionString").Value, b => b.MigrationsAssembly("WebAPI")));

            services.AddIdentity <User, ApplicationRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            services.Configure <IdentityOptions>(options =>
            {
                options.User.RequireUniqueEmail         = true;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase       = false;
                options.Password.RequireDigit           = false;
                options.Password.RequireLowercase       = false;
            });

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
            {
                var keyByteArray                  = Encoding.ASCII.GetBytes(authenticationOptions.Secret);
                var signingKey                    = new SymmetricSecurityKey(keyByteArray);
                options.RequireHttpsMetadata      = false;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    IssuerSigningKey         = signingKey,
                    ValidAudience            = authenticationOptions.Audience,
                    ValidIssuer              = authenticationOptions.Issuer,
                    ClockSkew                = TimeSpan.FromMinutes(0),
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true
                };
            });

            services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
            {
                builder
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowAnyOrigin();
            }));

            services.AddMvcCore()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddAuthorization()
            .AddApiExplorer();

            services.AddMvc(options =>
            {
                options.Filters.Add(typeof(ValidateModelStateAttribute));
            });
        }
Exemplo n.º 2
0
 private void JwtBearerDeaults(AuthenticationOptions obj)
 {
     throw new NotImplementedException();
 }