在asp.net mvc中使用注册/登录动作
本文关键字:注册 登录 asp net mvc | 更新日期: 2023-09-27 18:19:15
我在启动新的asp.net应用程序时遇到问题。我选择asp.net mvc模板与个人用户帐户认证和项目生成和启动正常。我的问题是:我该怎么做才能使用注册/登录选项?根据我的理解,负责这些操作的方法是自动生成的,但是我是否应该对数据库模式做一些关于用户信息的操作(它不应该也自动创建吗?)
无论如何,在注册表格中填写字段后,我得到一个文件找不到的错误,错误是在第155行发现的:var result = await UserManager。CreateAsync(用户、model.Password);
我在windows 8上使用Visual Studio Community 2015。提前谢谢大家
public async Task<IActionResult> Register(RegisterVM registerVM)
{
if (!ModelState.IsValid)
{
return View();
}
AppUser appUser = new AppUser()
{
Fullname = registerVM.Fullname,
UserName = registerVM.Username,
Email = registerVM.Email
};
IdentityResult result = await _userManager.CreateAsync(appUser, registerVM.Password);
if (!result.Succeeded)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
return View(registerVM);
}
await _userManager.AddToRoleAsync(appUser, Roless.Admin.ToString());
await _signInManager.SignInAsync(appUser, true);
return RedirectToAction("index", "home");
}
public IActionResult Login()
{
return View();
}
[HttpPost]
[AutoValidateAntiforgeryToken]
public async Task<IActionResult> Login(LoginVM loginVM, string returnurl)
{
if (!ModelState.IsValid)
{
return View();
}
AppUser dbUser = await _userManager.FindByEmailAsync(loginVM.Email);
if (dbUser == null)
{
ModelState.AddModelError("", "Ya email ya da Password sehvdir");
return View(loginVM);
}
SignInResult result = await _signInManager.PasswordSignInAsync(dbUser, loginVM.Password, loginVM.RememerMe, true);
if (result.IsLockedOut)
{
ModelState.AddModelError("", "Your Account Is Lock Out");
return View(loginVM);
}
if (!result.Succeeded)
{
ModelState.AddModelError("", "Ya Email ya da Password sehvdir");
return View(loginVM);
}
if (returnurl == null)
{
return RedirectToAction("index", "home");
}
foreach (var item in await _userManager.GetRolesAsync(dbUser))
{
if (item.Contains(Roless.Admin.ToString()))
{
return RedirectToAction("index", "Dashboard", new { area = "AdminF" });
}
}
return Redirect(returnurl);
}