我已经将一个MVC5项目切换到了MVC6,在身份管理方面遇到了一些问题。
作为注册过程的一部分,用户选择一个角色,该角色使用ApplicationUserManager保存在UserRoles表中。
在MVC5版本中,我的AccoutnControler中的代码是
public ApplicationUserManager UserManager {
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}稍后在输入控制器上...
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, IsRegComplete = true};
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
UserManager.AddToRole(user.Id, model.Role);
await SignInAsync(user, isPersistent: false);
if (model.Role == "Owner")
{
return RedirectToAction("Create", "Owners");
}
else
{
return RedirectToAction("Create", "ServiceCompanies");
}
}
else
{
AddErrors(result);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}由于在MVC6中没有IdentityConfig.cs,那么ApplicationUserManager会去哪里,或者是什么来代替它来允许我使用AddToRole()?
干杯凯文。
发布于 2016-05-25 12:47:28
我还没有在MVC6中使用identity,但是文档中的一些文章可能会对您有所帮助。
Introduction to Identity和Migrating Authentication and Identity
https://stackoverflow.com/questions/37427461
复制相似问题