[英]ASP.NET (6.0) Core MVC Login and Registration using Identity
[英]ASP.NET MVC Identity login using database values
在过去的几天里,我一直在尝试使登录功能适用于我的ASP.NET MVC应用程序。 我的应用程序的用户数据存储在MS SQL数据库(2008 R2)中。 该数据的表格如下:
table EMPLOYEE
EMPLOYEECODE int identity
NAME varchar(75) not null
PASSWORD varchar(255) not null
使用实体框架将产生以下模型:
public partial class EMPLOYEE
{
public int EMPLOYEECODE { get; set; }
public string NAME { get; set; }
public string PASSWORD { get; set; }
}
我想做的是编辑提供的(默认)AccountController,以使其能够使用EMPLOYEE(EMPLOYEECODE,PASSWORD)来验证和登录用户。 我想我需要更改UserManager以使其使用EMPLOYEE以及EMPLOYEECODE和PASSWORD字段,但我不知道如何。 当前的登录方法如下:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
我的UserManager实例如下:
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
我该如何解决这个问题? 我也尝试过使用身份验证cookie,但是我不知道如何从cookie中检索用户名。 任何帮助将不胜感激!
UserManager和UserStore通过其自己的用户,角色等表与ASP.NET Identity身份验证关联。如果要使用其他表,则必须创建自定义登录功能。
我有一个类似的要求,最终我做了以下事情:
如果需要对密码进行哈希处理,而不是迁移数据,请查看如何在Identity中注册用户,并使现有用户通过相同的过程进行处理,如下所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.