Using ASP .NET Membership and Profile with MVC, how can I create a user and set it to HttpContext.Cu
- by Jeremy Gruenwald
I've read the other questions on the topic of MVC membership and profiles, but I'm missing something. I implemented a custom Profile object in code as described by Joel here:
http://stackoverflow.com/questions/426609/asp-net-membership-how-to-assign-profile-values
I can't get it to work when I'm creating a new user, however. When I do this:
Membership.CreateUser(userName, password);
Roles.AddUserToRole(userName, "MyRole");
the user is created and added to a role in the database, but HttpContext.Current.User is still empty, and Membership.GetUser() returns null, so this (from Joel's code) doesn't work:
static public AccountProfile CurrentUser
{
get { return (AccountProfile)
(ProfileBase.Create(Membership.GetUser().UserName)); }
}
AccountProfile.CurrentUser.FullName = "Snoopy";
I've tried calling Membership.GetUser(userName) and setting Profile properties that way, but the set properties remain empty, and calling AccountProfile.CurrentUser(userName).Save() doesn't put anything in the database. I've also tried indicating that the user is valid & logged in, by calling Membership.ValidateUser, FormsAuthentication.SetAuthCookie, etc., but the current user is still null or anonymous (depending on the state of my browser cookies).
I have the feeling I'm missing some essential piece of understanding about how Membership, Authentication, and Profiles fit together. Do I have to do a round trip before the current User will be populated? Any advice would be much appreciated.