Wednesday, 21 August 2013

WebSecurity.CreateUserAndAccount get reference to created user

WebSecurity.CreateUserAndAccount get reference to created user

I am working on a ASP.NET MVC 4 web application. After creating a user and
account (using WebSecurity.CreateUserAndAccount), i want continue working
with this user, e.g. for using him as a foreign key in an other model.
One approach would be to give the UserProfile a custom UserId, and to use
this UserId for querying the user. For example:
...
try
{
int CustomUserId = Rnd.Next(1000000, 9999999);
WebSecurity.CreateUserAndAccount(RegisterModel.UserName,
RegisterModel.Passwordword,
propertyValues: new { UserId = CustomUserId });
WebSecurity.Login(UserName, Password);
var UserProfile = (from UserProf in _db.UserProfiles
where UserProf.UserId == CustomUserId
select UserProf).FirstOrDefault();
CustomModel cm = new CustomModel();
cm.User = UserProfile;
//Add cm to database an so on...
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
//Bla...
}
...
This seems to me pretty inelegant. Especially because of maintaining the
UserId on my own. Is there a more elegant way to solve this Problem?

No comments:

Post a Comment