[英]ASP.NET MVC Identity login using database values
For the past couple of days I've been trying to get the login functionality working for my ASP.NET MVC application. 在过去的几天里,我一直在尝试使登录功能适用于我的ASP.NET MVC应用程序。 The user data for my application is stored in a MS SQL database (2008 R2).
我的应用程序的用户数据存储在MS SQL数据库(2008 R2)中。 The table for this data is as follows:
该数据的表格如下:
table EMPLOYEE
EMPLOYEECODE int identity
NAME varchar(75) not null
PASSWORD varchar(255) not null
Which results in the following model using the Entity Framework: 使用实体框架将产生以下模型:
public partial class EMPLOYEE
{
public int EMPLOYEECODE { get; set; }
public string NAME { get; set; }
public string PASSWORD { get; set; }
}
What I would like to do, is edit the provided (default) AccountController to enable it to use EMPLOYEE(EMPLOYEECODE, PASSWORD) to verify and login users. 我想做的是编辑提供的(默认)AccountController,以使其能够使用EMPLOYEE(EMPLOYEECODE,PASSWORD)来验证和登录用户。 I think I need to alter my UserManager to make it use EMPLOYEE and the fields EMPLOYEECODE and PASSWORD but I don't know how.
我想我需要更改UserManager以使其使用EMPLOYEE以及EMPLOYEECODE和PASSWORD字段,但我不知道如何。 The current login method is as follows:
当前的登录方法如下:
[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);
}
My UserManager is instanciated as follows: 我的UserManager实例如下:
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
How could I solve this problem? 我该如何解决这个问题? I have also tried using an authentication cookie, but I don't know how I can retrieve the username from the cookie.
我也尝试过使用身份验证cookie,但是我不知道如何从cookie中检索用户名。 Any help would be highly appreciated!
任何帮助将不胜感激!
UserManager和UserStore通过其自己的用户,角色等表与ASP.NET Identity身份验证关联。如果要使用其他表,则必须创建自定义登录功能。
I had a similar request and what I ended up doing was the following: 我有一个类似的要求,最终我做了以下事情:
If your passwords need to be hashed, instead of migration the data, take a look at how users are registered in Identity and put your existing users through the same process like so: 如果需要对密码进行哈希处理,而不是迁移数据,请查看如何在Identity中注册用户,并使现有用户通过相同的过程进行处理,如下所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.