protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity <Users>() .ToTable("Users"); modelBuilder.Entity <Users>().HasData(new Users { Id = new Guid("755561b1-a76e-4d13-a4ad-c41920d6c4f2").ToString(), Name = "Sinan", Surname = "Şahin", UserName = "******", NormalizedUserName = "******", PasswordHash = PasswordHash.CreateMd5Hash("123456"), Gender = 'E', RecordTime = DateTime.Now, SecurityStamp = Guid.NewGuid().ToString() }, new Users { Id = new Guid("c3fca1f0-9528-43c5-a2e6-510befb52908").ToString(), Name = "Şahin", Surname = "Sinan", UserName = "******", NormalizedUserName = "******", PasswordHash = PasswordHash.CreateMd5Hash("123456asd"), Gender = 'E', RecordTime = DateTime.Now, SecurityStamp = Guid.NewGuid().ToString(), }); }
public async Task <IActionResult> Login([FromBody] LoginModel loginModel) { var user = _userManager.Users.FirstOrDefault(u => u.UserName == loginModel.Username); var password = _userManager.Users.FirstOrDefault(u => u.PasswordHash == PasswordHash.CreateMd5Hash(loginModel.Password)); if (user != null && password != null) { var authClaims = new[] { new Claim(JwtRegisteredClaimNames.Sub, user.UserName), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()) }; var appSettingJwtToken = _config.GetValue <string>("JwtToken"); var authSigninKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(appSettingJwtToken)); var token = new JwtSecurityToken( issuer: "http://localhost:57930", audience: "http://localhost:57930", expires: DateTime.Now.AddHours(3), claims: authClaims, signingCredentials: new Microsoft.IdentityModel.Tokens.SigningCredentials(authSigninKey, SecurityAlgorithms.HmacSha256) ); var userToken = new JwtSecurityTokenHandler().WriteToken(token); user.Token = userToken; _dataRepository.Update(user, user); return(Ok( new { token = userToken, expiration = token.ValidTo })); } return(Unauthorized()); }