WebSecurity.CreateUserAndAccount get a link to the created user

I am working on an ASP.NET MVC 4 web application. After creating the user and account (using WebSecurity.CreateUserAndAccount), I want to continue working with this user, for example. to use it as a foreign key in another model.

One approach is to provide the UserProfile to the user UserId and use this UserId to query the user. For instance:

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

It seems rather inelegant to me. Especially because of the support of UserId on its own. Is there a more elegant way to solve this problem?

+6
source share
1 answer

If you have a UserId as an Identity column, SimpleMembership will process it automatically. Then you can get the received userId with: var userId = WebSecurity.GetUserId (RegisterModel.UserName)

imho: SimpleMembership implementation is POS. Take a look at the code, this is a big mess. He is trying to be flexible, but not very flexible. UserId is also required to be int. Again, if you can live with it, the advantage is automatic integration with third-party authentication (facebook, twitter, etc.).

+12
source

Source: https://habr.com/ru/post/952226/


All Articles