Pracuję na aplikacji sieci web ASP.NET MVC 4. Po utworzeniu użytkownika i konta (przy użyciu WebSecurity.CreateUserAndAccount) chcę kontynuować pracę z tym użytkownikiem, np. za używanie go jako klucza obcego w innym modelu.WebSecurity.CreateUserAndAccount uzyskać odniesienie do utworzonego użytkownika
Jednym z podejść byłoby nadanie UserPrile niestandardowego UserID i wykorzystanie tego UserId do wysłania zapytania do użytkownika. Na przykład:
...
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...
}
...
Wydaje mi się to dość nieeleganckie. Zwłaszcza ze względu na utrzymanie UserId na własną rękę. Czy istnieje bardziej elegancki sposób na rozwiązanie tego problemu?