如何从Account控制器创建新的用户身份?

时间:2016-08-23 13:48:02

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

我已将我的数据库与MVC asp.net身份数据库合并,我想使用来自其他控制器的asp.net身份创建一个新用户,但我无法成功添加用户甚至我的代码工作没有错误这是代码:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Email,EmailConfirmed,Password,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,UserName")] AspNetUsers aspNetUsers)
{
    if (ModelState.IsValid)
    {
        //    ApplicationUserManager manager = new ApplicationUserManager(new UserStore<ApplicationUser>());

        //    var manager =   HttpContext.Current.GetOwinContext().GetUserManager<UserManager<User>>();
        var store = new UserStore<ApplicationUser>();
            var manager = new ApplicationUserManager(store);
            var user = new ApplicationUser() { Email = aspNetUsers.Email, UserName = aspNetUsers.UserName };
         var result=   manager.CreateAsync(user, aspNetUsers.PasswordHash);
        manager.Create(user, aspNetUsers.Password);
        //    db.AspNetUsers.Add(aspNetUsers);
        //    db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(aspNetUsers);
}

1 个答案:

答案 0 :(得分:0)

var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
ApplicationUserManager _userManager = new ApplicationUserManager(store);
var manger = _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
var user = new ApplicationUser() { Email = aspNetUsers.Email, UserName = aspNetUsers.UserName };
var usmanger= manger.Create(user, aspNetUsers.PasswordHash);