public CreateTokenRespone GenergateToken(User user, string id) { try { var tokenHandler = new JwtSecurityTokenHandler(); var secretKey = _jwtSettings.getSkey(); var signingCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256); var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, id), new Claim(ClaimTypes.Name, user.UserName), new Claim(ClaimTypes.Role, user.Role), new Claim(JwtRegisteredClaimNames.Nbf, new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds().ToString()), new Claim(JwtRegisteredClaimNames.Exp, new DateTimeOffset(DateTime.Now.AddMinutes(5)).ToUnixTimeSeconds().ToString()) }; var tokenOptions = new JwtSecurityToken( claims: claims, expires: DateTime.Now.AddMinutes(5), signingCredentials: signingCredentials ); var token = tokenHandler.WriteToken(tokenOptions); return(new CreateTokenRespone { Success = true, Token = token, error = "" }); } catch (Exception ex) { return(new CreateTokenRespone { Success = false, Token = "", error = ex.Message }); } }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddControllers(); //------------------------------------ services.AddDbContext <TechShopDB>(options => { options.UseSqlServer(Configuration.GetConnectionString("StrConection")); }); //------------------------------------- services.AddIdentity <ApplicationUser, IdentityRole>() .AddEntityFrameworkStores <TechShopDB>(); services.Configure <IdentityOptions>(options => { // Password settings. options.Password.RequireDigit = false; options.Password.RequireLowercase = false; options.Password.RequireNonAlphanumeric = false; options.Password.RequireUppercase = false; options.Password.RequiredLength = 3; options.Password.RequiredUniqueChars = 0; }); //----------------------------------------------- var jwtset = new JwtSettings(); Configuration.Bind(nameof(JwtSettings), jwtset); services.AddSingleton(jwtset); services.AddCors(options => { options.AddPolicy("EnableCORS", builder => { builder.AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod(); }); }); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(opt => { opt.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false, ValidateAudience = false, ValidateLifetime = true, ValidateIssuerSigningKey = true, IssuerSigningKey = jwtset.getSkey() }; }); //-------------------------------------- services.AddScoped <IUserService, UserService>(); services.AddScoped <IProductService, ProductService>(); services.AddScoped <ICartService, CartService>(); services.AddScoped <INavService, NavService>(); //-------------------------------------- services.AddAutoMapper(typeof(Startup)); }