MVC 6中的身份管理

时间:2016-05-25 03:47:08

标签: asp.net-mvc asp.net-core-mvc

我已将MVC 5项目转换为MVC 6并且在身份管理方面存在一些问题。

作为注册过程的一部分,用户使用ApplicationUserManager选择保存在UserRoles表中的角色。

在MVC 5版本中,我的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);
    }

由于MVC 6中没有IdentityConfig.cs,ApplicationUserManager在哪里取代或取代它以允许我使用AddToRole()?

干杯, 凯文。

1 个答案:

答案 0 :(得分:-1)

我还没有在MVC6中使用身份,但文档中有一些文章可能对你有用。

Introduction to IdentityMigrating Authentication and Identity