[英]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.