C# ASP.Net核心使用自定义标识而不扩展
我需要将一个Laravel项目迁移到ASP.NETCore,我使数据库反向生成模型(ScaffoldDBContext) 我需要使用JWT作为身份验证方法,大多数逻辑都是作为API的服务,使用从C# ASP.Net核心使用自定义标识而不扩展,c#,laravel,asp.net-core,asp.net-identity,C#,Laravel,Asp.net Core,Asp.net Identity,我需要将一个Laravel项目迁移到ASP.NETCore,我使数据库反向生成模型(ScaffoldDBContext) 我需要使用JWT作为身份验证方法,大多数逻辑都是作为API的服务,使用从IdentityUser扩展而来的User模型,我必须将一些字段映射到表列,并添加其他(PasswordHash),在DbContext上,我必须忽略许多字段,因为不需要它 modelBuilder.Entity<User>().Ignore(c => c.AccessFailedCou
IdentityUser
扩展而来的User
模型,我必须将一些字段映射到表列,并添加其他(PasswordHash
),在DbContext上,我必须忽略许多字段,因为不需要它
modelBuilder.Entity<User>().Ignore(c => c.AccessFailedCount)
.Ignore(c => c.LockoutEnabled)
.Ignore(c => c.TwoFactorEnabled)
.Ignore(c => c.EmailConfirmed)
.Ignore(c => c.LockoutEnd)
.Ignore(c => c.NormalizedEmail)
.Ignore(c => c.NormalizedUserName)
.Ignore(c => c.PhoneNumber)
.Ignore(c => c.PhoneNumberConfirmed)
.Ignore(c => c.SecurityStamp)
.Ignore(c => c.UserName)
.Ignore(c => c.ConcurrencyStamp);
IdentityUser
抱歉,我的英语不好根据描述,用户名和电子邮件似乎无效,请尝试输入有效值。“是否可以不使用IdentityUser”您的意思是不想使用Asp.net核心标识?如果是这种情况,您可以通过EF core直接访问数据,而不是使用UserManager或RoleManager。
[HttpPost]
[Route("ChangePassword")]
public async Task<IActionResult> ChangePassword([FromBody] UserLoginRequest user)
{
if (!ModelState.IsValid)
return BadRequest(new RegistrationResponse()
{
Errors = new List<string>()
{
"Invalid payload"
},
Success = false
});
var existingUser = await _context.Users.Where(u => u.Email == user.Email).FirstAsync();
if (existingUser == null)
{
return BadRequest(new RegistrationResponse()
{
Errors = new List<string>()
{
"Invalid login request"
},
Success = false
});
}
// var _user = await _userManager.FindByEmailAsync(user.Email);
var newPassword = user.Password;
// var hashedPassword = _userManager.PasswordHasher.HashPassword(existingUser, newPassword);
var token = await _userManager.GeneratePasswordResetTokenAsync(existingUser);
var res = await _userManager.ResetPasswordAsync(existingUser, token, newPassword);
if (res.Succeeded)
{
return Ok(new SuccessResponse<IdentityResult>{Success = true, Data = res});
}
return StatusCode(500,new SuccessResponse<IEnumerable<IdentityError>>{Success = false, Data = res.Errors});
}
{
"success": false,
"data": [
{
"code": "InvalidUserName",
"description": "Username '' is invalid, can only contain letters or digits."
},
{
"code": "InvalidEmail",
"description": "Email '' is invalid."
}
]
}