简体   繁体   English

使用数据库值的ASP.NET MVC身份登录

[英]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: 我有一个类似的要求,最终我做了以下事情:

  • Used migrations to create the Identity tables 使用迁移来创建身份表
  • Modified the Identity classes to use and INT instead of GUID as the Id 修改了Identity类以使用INT代替GUID作为Id
  • Migrated my data from the Employee table to the AspNetUsers table 将我的数据从Employee表迁移到AspNetUsers表
  • In your case, map the EmployeeCode to the AspNetUsers.Id field and the password to the AspNetUsers.PasswordHash. 在您的情况下,将EmployeeCode映射到AspNetUsers.Id字段,将密码映射到AspNetUsers.PasswordHash。
  • Depending on how your passwords are saved in the Db, you might need to add an extra step here to get them into the table in a hashed format. 根据您在Db中保存密码的方式,您可能需要在此处添加一个额外的步骤,以便以散列格式将它们添加到表中。

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中注册用户,并使现有用户通过相同的过程进行处理,如下所示:

  1. Get a list of all the users in your Employee table 获取您的Employee表中所有用户的列表
  2. For each user, use the owin context to retrive the UserManager and SignIn Manager 对于每个用户,使用owin上下文来检索UserManager和SignIn Manager
  3. Create a new ApplicationUser using the EmployeeCode and EmployeePassword 使用EmployeeCode和EmployeePassword创建一个新的ApplicationUser
  4. Use the UserManager to create the user in the Database 使用UserManager在数据库中创建用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM