我想獲得用戶ID註冊時ASP.NET MVC創建用戶時獲取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?
謝謝。
你什麼時候得到的異常? – Xharze 2012-04-21 11:25:22
我用註釋行標記了它。 – 2012-04-21 11:33:32
After「FormsAuthentication.SetAuthCookie ...」 – 2012-04-21 11:34:38