6

ASP.NET MVC 4 Web アプリケーションに取り組んでいます。(WebSecurity.CreateUserAndAccount を使用して) ユーザーとアカウントを作成した後、たとえば、他のモデルで外部キーとして使用するために、このユーザーを引き続き使用したいと考えています。

1 つの方法は、UserProfile にカスタム UserId を与え、この UserId を使用してユーザーをクエリすることです。例えば:

...
  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...
            }
...

これは私にはかなり非エレガントに思えます。特に、自分で UserId を維持しているためです。この問題を解決するよりエレガントな方法はありますか?

4

1 に答える 1