// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddCors(options => { options.AddPolicy("AllowOrigin", builder => builder.WithOrigins("http://localhost:4200")); }); var tokenOptions = Configuration.GetSection(key: "TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer( options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.SecurityKey(tokenOptions.SecurityKey) }; } ); }
public AccessToken CreateToken(User user, List <OperationClaim> operationClaims) { var securityKey = SecurityKeyHelper.SecurityKey(_tokenOptions.SecurityKey); var singinCredentials = SingingCredentialHelper.CreateSingingCredentials(securityKey); var jwt = CreateJwtSecurityToken(_tokenOptions, user, singinCredentials, operationClaims); var jwtSecurityTokenHelper = new JwtSecurityTokenHandler(); var token = jwtSecurityTokenHelper.WriteToken(jwt); return(new AccessToken { Token = token, Expiration = _accessTokenExpiration }); }