public AccessToken CreateToken(User user, List <OperationClaim> operationClaims) { var securityKey = SecurityKeyHelper.CreateSKey(_tokenOptions.SecurityKey); var signingC = SigningCredentialsHelper.CreateSigningC(securityKey); var jwt = CreateSecurityT(_tokenOptions, user, signingC, operationClaims); var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var token = jwtSecurityTokenHandler.WriteToken(jwt); return(new AccessToken { Token = token, Expiration = _accessTokenExpiration }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddHttpClient(); services.AddControllers(); services.AddApiVersioning(o => { o.ReportApiVersions = true; o.AssumeDefaultVersionWhenUnspecified = true; o.DefaultApiVersion = new ApiVersion(1, 0); }); // AddScoped, AddTransient, AddSingleton ??? services.AddScoped <IUserService, UserManager>(); services.AddScoped <IUserDal, EfUserDal>(); services.AddScoped <ICategoryService, CategoryManager>(); services.AddScoped <ICategoryDal, EfCategoryDal>(); services.AddScoped <IProductService, ProductManager>(); services.AddScoped <IProductDal, EfProductDal>(); services.AddScoped <ITrademarkDal, EfTrademarkDal>(); services.AddScoped <ITrademarkService, TrademarkManager>(); services.AddScoped <IAuthService, AuthManager>(); services.AddScoped <IUserOperationClaimDal, EfUserOperationClaimDal>(); services.AddScoped <IUserOperationClaimService, UserOperationClaimManager>(); services.AddTransient <ITokenHelper, JwtHelper>(); services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddDbContext <JwtContext>(); services.AddMvc(option => { option.EnableEndpointRouting = false; }); services.AddEntityFrameworkSqlServer(); services.AddAutoMapper(typeof(Startup)); services.AddCors(options => { options.AddPolicy("AllowOrigin", builder => builder.WithOrigins("http://localhost:3000").AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()); }); services.AddControllersWithViews() .AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore ); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = false, //true ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Auidence, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSKey(tokenOptions.SecurityKey) }; }); }