/// <summary> /// Add Jwt authorization to application. /// </summary> /// <param name="services"></param> public static void AddJwtAuthorizarion(this IServiceCollection services) { services.AddSingleton <JwtAuthorizationSettings>(); services.AddSingleton <JwtAuthorizationTokenWriter>(); services.AddScoped <IUserContext, UserContext>(); JwtAuthorizationSettings jwtAuthorizationSettings = services.BuildServiceProvider() .GetService <JwtAuthorizationSettings>(); byte[] key = Encoding.ASCII.GetBytes(jwtAuthorizationSettings.SecretKey); services.AddAuthentication(config => { config.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; config.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(config => { config.RequireHttpsMetadata = false; config.SaveToken = false; config.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false }; }); }
public JwtAuthorizationTokenWriter(JwtAuthorizationSettings jwtAuthorizationSettings) { this.JwtAuthorizationSettings = jwtAuthorizationSettings; }