我想在注册时获取 UserId
[AllowAnonymous]
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
//Object reference not set to an instance of an object.
Guid id = (Guid)System.Web.Security.Membership.GetUser().ProviderUserKey;
System.Web.Security.Roles.AddUserToRole(model.UserName, "User");
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
但是发生错误(“对象引用未设置为对象的实例。”)。如何在注册方法中获取 userId?
谢谢。