Esempio n. 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            var appSecretSection = Configuration.GetSection("AppSecret");

            services.Configure <AppSecret>(appSecretSection);
            services.AddScoped <IUserRepository, UserRepository>();
            services.AddScoped <IUserService, UserService>();
            services.AddScoped <ITokenRepository, TokenRepository>();
            services.AddScoped <ITokenService, TokenService>();
            services.AddControllers();
            var appSecret = appSecretSection.Get <AppSecret>();
            var secretKey = appSecret.JwtSecret.ToBytes();

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = false;
                x.SaveToken                 = true;
                x.TokenValidationParameters = Jwt
                                              .GetAppTokenValidationParameters(secretKey);
            });

            services.AddDbContext <UserContext>(
                options => options.UseMySql(appSecret.MySqlConnectionString,
                                            mySqlOptions =>
            {
                mySqlOptions.ServerVersion(new Version(8, 0, 17), Pomelo.EntityFrameworkCore.MySql.Infrastructure.ServerType.MySql);
            })
                );

            services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = appSecret.RedisConfig;
                options.InstanceName  = "token-cache";
            });
        }