user2586804
user2586804

Reputation: 321

How to load the profile of a specific user

I have a MVC Controller action for creating a new user. I'm trying to store the new users additional details, such as FullName.

Problem is the Profile is for the currently authenticated user, in my case the admin. How can I reference the profile of the newly created user?

var user = Membership.CreateUser(newUser.Username, newUser.Password, newUser.Email);
Profile.SetPropertyValue("fullname", newUser.FullName); //wrong - stored against the admin that is creating the user
user.??? //I do have the user, but can I get the profile?

Upvotes: 0

Views: 870

Answers (1)

R.C
R.C

Reputation: 10565

Creating a user will add it to the list of users. However, this does not authenticate or authorize the new user for the current request. You also need to authenticate the user in the current request context. Profile is not accessible immediately upon user creation.

If you want to create a Profile for a newly-created User in the same request, you need to call HttpContext.Current.Profile.Initialize(userName, true). HttpContext.Profile will give you the ProfileBase object and then calling Initialize() method will initialize the profile of the userName you pass to it.You can then populate the newly initialized profile and continue with your changes and save it.

Use HttpContext.Current.Profile, when it is required to create/access the Profile immediately upon creation. On other requests, you can use/continue with ProfileBase.Create(userName) to get an instance of a profile for the specified user name.

However, if you authenticate the user immediately upon creation, you will be able to access the profile in its simplest way.

Upvotes: 2

Related Questions