AspNet.Identity Custom User and Custom Role should be simple; what am I missing?

强颜欢笑 提交于 2019-12-04 10:45:18

Try this way. I had the same issue, you need to provide the id.

    //
    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser() { 
                UserName = model.UserName,
                Id = Guid.NewGuid().ToString(),
                Created = DateTime.Now,
                LastLogin = null
            };

            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                AddErrors(result);
            }



        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!