Exemplo n.º 1
0
        public void ConfigureServices(IServiceCollection services)
        {
            string connection = Configuration.GetConnectionString("DefaultConnection");

            DependenciesRoot.InjectDependencies(services, connection);
            services.AddAutoMapper(typeof(Startup));
        }
Exemplo n.º 2
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            string connection = Configuration.GetConnectionString("DefaultConnection");

            //var appSettingsSection = Configuration.GetSection("AppSettings");
            //services.Configure<AppSettings>(appSettingsSection);
            //var appSettings = Configuration.Get<AppSettings>();
            //var key = Encoding.ASCII.GetBytes(appSettings.Secret);
            var key = Encoding.ASCII.GetBytes("Words Secret SecretSecretSecretSecretSecretSecretSecretSecret");

            DependenciesRoot.InjectDependencies(services, connection);

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(x =>
            {
                x.Events = new JwtBearerEvents
                {
                    OnTokenValidated = context =>
                    {
                        var userService = context.HttpContext.RequestServices.GetRequiredService <IUserService>();
                        var userId      = int.Parse(context.Principal.Identity.Name);
                        var user        = userService.GetUser(userId);
                        if (user == null)
                        {
                            context.Fail("Unauthorized");
                        }
                        return(Task.CompletedTask);
                    }
                };
                x.RequireHttpsMetadata      = false;
                x.SaveToken                 = true;
                x.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };
            });

            var     mapperConfig = new MapperConfiguration(conf => conf.AddProfile(new MappingProfile()));
            IMapper mapper       = mapperConfig.CreateMapper();

            services.AddSingleton(mapper);
            //services.AddAutoMapper(typeof(Startup));
        }