I want to get UserId when registering
[AllowAnonymous]
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
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);
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));
}
}
return View(model);
}
But an error occurred ("The reference to the object is not installed in the instance of the object."). How to get userId method in register?
Thank.
source
share