public static IServiceCollection ConfigureServices(IServiceCollection services) { var keyHasherService = new KeyHasherService(SHA512.Create()); services.AddSingleton(keyHasherService); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); services.AddSingleton <IAuthService, AuthService>(); services.AddSingleton <UserValidator, UserValidator>(); services.AddSingleton <TokenService, TokenService>(); return(services); }
public AuthenticationController( IUsuarioService usuarioService, UsuarioValidator validator, TokenGenerationService tokenGenerator, KeyHasherService keyHasherService, IConfiguration config) { _usuarioService = usuarioService; _validator = validator; _tokenGenerator = tokenGenerator; _keyHasherService = keyHasherService; _config = config; }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddScoped(typeof(IRepositoryBase <>), typeof(RepositoryBase <>)); services.AddScoped(typeof(IServiceBase <>), typeof(ServiceBase <>)); services.AddTransient <IUsuarioRepository, UsuarioRepository>(); services.AddTransient <IUsuarioService, UsuarioService>(); services.AddTransient <TokenGenerationService>(); services.AddTransient <UsuarioValidator>(); services.AddDbContext <AuthenticationContext>(); //services.AddAutomapper(); services.AddMvc(config => { config.ReturnHttpNotAcceptable = true; }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1); var keyHasherService = new KeyHasherService(SHA512.Create()); services.AddSingleton(keyHasherService); services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new OpenApiInfo() { Title = "Authentication Api", Version = "v1", Contact = new OpenApiContact() { Name = "Bruno", Email = "*****@*****.**", Url = new Uri("https://github.com/brunovitorprado") }, }); }); #region JWT configurations var signingConfigurations = new SigningConfigurations(); services.AddSingleton(signingConfigurations); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = Configuration["TokenConfigurations:Issuer"], ValidateAudience = true, ValidAudience = Configuration["TokenConfigurations:Audience"], ValidateIssuerSigningKey = true, RequireExpirationTime = true, ValidateLifetime = true, ClockSkew = TimeSpan.Zero }; }); services.AddAuthorization(auth => { auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser().Build()); }); #endregion }