ASP.NET MVC2 +会员资格
当新用户刚刚注册到 ASP.NET MVC 应用程序时,我想在我的用户表中创建用户(我使用默认的 ASP.NET MVC 日志记录系统 - 成员资格。我想获取新注册用户的 guid 并在我的应用程序中创建用户数据库将具有相同的 guid 并保存不同的数据,例如姓名等。我该怎么办?我怎样才能获得注册的人 ID?我试图这样做,
public ActionResult Register(RegisterModel model) function in AccountController
但我不知道如何获得正确的数据注册用户的 guid 这不起作用:
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
if (createStatus == MembershipCreateStatus.Success)
{
FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
//string guid = Helpers.getLoggedUserGuid(); //return null
//if (Request.IsAuthenticated) { } // return false
//if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated) {} // return false
//Membership.getUser() // return null
//The most funny is that after this redirection below the user is logged in - I do not understand it
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View(model);
}
是否有可能在此函数中获取此 guid?(我知道我可以获取用户名等,但我不明白为什么我无法获取此 guid)
I would like to create user in my user table when new user have just registered to ASP.NET MVC application (I use the default ASP.NET MVC logging system - Membership. I want to get the new registered user's guid and create user in my database which will have the same guid and keep there different data like name surname etc. How can I do it? How can I just get the registered person ID? I was trying to do it in
public ActionResult Register(RegisterModel model) function in AccountController
but I do not know how to get the just registered user's guid this is not working:
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
if (createStatus == MembershipCreateStatus.Success)
{
FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
//string guid = Helpers.getLoggedUserGuid(); //return null
//if (Request.IsAuthenticated) { } // return false
//if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated) {} // return false
//Membership.getUser() // return null
//The most funny is that after this redirection below the user is logged in - I do not understand it
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View(model);
}
Is any possibility to get this guid in this function? (I know that I can get userName and so on but I can not understand why I can not get this guid)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以从
MembershipUser
获取 Guid:MSDN:
You can get Guid from
MembershipUser
:MSDN:
您无法通过您尝试的方法获得该值。除非您使用
Membership.GetUser()
您应该做的是:
创建一个通用控制器,例如:
然后,而不是让您的控制器派生自
Controller
使它们继承自新的自定义控制器,并且在您需要的任何方法中,您只需调用
this.User
来保存MembershipUser
或this.User
。 UserGuid 获取用户 ID。You can't get that value in the method you are trying. unless you use the
Membership.GetUser()
What you should do is:
create a common controller, for example:
and then, instead of having your Controllers derivate from
Controller
make them inherit from your new custom Controllerand in any method you need, you just call
this.User
to hold theMembershipUser
orthis.UserGuid
to get the User Id.