Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 WebSecurity.CreateUserAndAccount获取对已创建用户的引用_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 4 WebSecurity.CreateUserAndAccount获取对已创建用户的引用

Asp.net mvc 4 WebSecurity.CreateUserAndAccount获取对已创建用户的引用,asp.net-mvc-4,Asp.net Mvc 4,我正在开发一个ASP.NET MVC 4 web应用程序。创建用户和帐户(使用WebSecurity.CreateUserAndAccount)后,我希望继续与此用户合作,例如,在其他模型中将其用作外键 一种方法是为UserProfile提供一个自定义UserId,并使用该UserId查询用户。例如: ... try { int CustomUserId = Rnd.Next(1000000, 9999999);

我正在开发一个ASP.NET MVC 4 web应用程序。创建用户和帐户(使用WebSecurity.CreateUserAndAccount)后,我希望继续与此用户合作,例如,在其他模型中将其用作外键

一种方法是为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...
            }
...

在我看来这很不雅观。特别是因为我自己维护用户ID。有没有更优雅的方法来解决这个问题?

如果您将UserId作为标识列,SimpleMembership将自动处理它。然后,您可以使用:var userId=WebSecurity.GetUserId(RegisterModel.UserName)获取结果userId

imho:SimpleMembership实现是一个POS。看看代码,它是一个大混乱。它试图变得灵活,但结果不是很灵活。用户ID也必须是一个int。同样,如果你能接受它,好处是自动与第三方身份验证(facebook、twitter等)集成