public TokenService( TokenConfigurationDto tokenConfiguration, SiginConfigurations siginConfigurations, INotification notification ) : base(notification) { this._tokenConfiguration = tokenConfiguration; this._siginConfigurations = siginConfigurations; }
public TokenService( TokenConfigurationDto tokenConfiguration, SiginConfigurations siginConfigurations, IDistributedCache cache, INotification notification ) : base(notification) { this._tokenConfiguration = tokenConfiguration; this._siginConfigurations = siginConfigurations; this._cache = cache; }
public static void Resolver(this IServiceCollection services, IConfiguration configuration) { var tokenConfiguration = new TokenConfigurationDto(); new ConfigureFromConfigurationOptions <TokenConfigurationDto>( configuration.GetSection("Token") ).Configure(tokenConfiguration); services.AddSingleton(tokenConfiguration); var siginConfigurations = new SiginConfigurations(); services.AddSingleton(siginConfigurations); }
public static void Resolver(this IServiceCollection services, IConfiguration configuration) { var tokenConfiguration = new TokenConfigurationDto(); new ConfigureFromConfigurationOptions <TokenConfigurationDto>( configuration.GetSection("Token") ).Configure(tokenConfiguration); services.AddSingleton(tokenConfiguration); var siginConfigurations = new SiginConfigurations(); services.AddSingleton(siginConfigurations); services.AddAuthentication(authOption => { authOption.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; authOption.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(bearerOption => { var paramSolution = bearerOption.TokenValidationParameters; paramSolution.IssuerSigningKey = siginConfigurations.Key; paramSolution.ValidAudience = tokenConfiguration.Audience; paramSolution.ValidIssuer = tokenConfiguration.Issuer; paramSolution.ValidateIssuerSigningKey = true; paramSolution.ValidateLifetime = true; paramSolution.ClockSkew = TimeSpan.Zero; }); services.AddAuthorization(auth => { auth.AddPolicy("auth", new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser().Build()); }); }
public static void Resolver(this IServiceCollection services, IConfiguration configuration) { var tokenConfiguration = new TokenConfigurationDto(); new ConfigureFromConfigurationOptions <TokenConfigurationDto>( configuration.GetSection("Token") ).Configure(tokenConfiguration); var siginConfigurations = new SiginConfigurations(); services.AddSwaggerGen(swagger => { swagger.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Version = "v1", Title = "financeiro-api", Description = "Aplicação para controle de despessas e receitas pessoais.", Contact = new Microsoft.OpenApi.Models.OpenApiContact { Name = "Luiz Mário", Email = "*****@*****.**", Url = new Uri("https://www.linkedin.com/in/luiz-mário-ferreira-191936ba") } }); var securitySchema = new OpenApiSecurityScheme { Description = "JWT Authorization header using the Bearer scheme.", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, BearerFormat = "JWT", Scheme = "bearer", Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }; swagger.AddSecurityDefinition("Bearer", securitySchema); swagger.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, new string[] {} } }); services.AddAuthentication(option => { option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = false, ValidateIssuerSigningKey = true, ValidIssuer = tokenConfiguration.Issuer, ValidAudience = tokenConfiguration.Audience, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(siginConfigurations.Key.ToString())) }; }); var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); swagger.IncludeXmlComments(xmlPath); }); }