开发者

Tying MembershipUser and Web.Profile together for newly created users

I am using ASP.NET MVC 3. I am using what essentially came with for free in the Visual Studio project template for an MVC project with the "Internet Application" option. Basically this brings in Forms authentication and provides some basic elements to manage user login and stuff.

I am also using the web profiles stuff with this to store some custom fields. Everything开发者_运维百科 was going great. I use SuperFunProfile as a wrapper around the Profile instance to make it easier to get at profile properties.

Until I wanted to set a property of a Profile right away after signing the user up.

The problem I can't solve is that this.Request.RequestContext.HttpContext.Profile contains the profile for the anonymous user. How can I get a new profile for the user now that he should be signed up and signed in?

    public ActionResult SignUp(SignUpModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            MembershipCreateStatus createStatus = this.MembershipService.CreateUser(model.UserName, model.Password, model.Email);

            if (createStatus == MembershipCreateStatus.Success)
            {
                this.FormsService.SignIn(model.UserName, false /* createPersistentCookie */);

                var profile = new SuperFunProfile(this.Request.RequestContext.HttpContext.Profile);
                profile.DisplayName = model.UserName;
                profile.Save();

                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError(string.Empty, AccountValidation.ErrorCodeToString(createStatus));
            }
        }

I poked around Membership and Web.Profile, but I am not seeing anything that looks like it will get me closer to my goal.

Maybe I should just create a ProfileModel that I store myself into the DB rather than using Web.Profile? I could key that on MembershipUser.ProviderUserKey which would make it easier to create a ProfileModel at sign up, I suppose.


I think you can use MigrateAnonymous event.

When users log in (that is, when they stop being anonymous users), the MigrateAnonymous event is raised. You can handle this event to migrate information from the user's anonymous identity to the new authenticated identity, if necessary. The following code example shows how to migrate information when a user is authenticated.

In your global.asax use something like

public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args)
{
  ProfileCommon anonymousProfile = Profile.GetProfile(args.AnonymousID);

  Profile.ZipCode = anonymousProfile.ZipCode; //Your custom property
  Profile.CityAndState = anonymousProfile.CityAndState;//Your custom property
  Profile.StockSymbols = anonymousProfile.StockSymbols;//Your custom property

  ////////
  // Delete the anonymous profile. If the anonymous ID is not 
  // needed in the rest of the site, remove the anonymous cookie.

  ProfileManager.DeleteProfile(args.AnonymousID);
  AnonymousIdentificationModule.ClearAnonymousIdentifier(); 

  // Delete the user row that was created for the anonymous user.
  Membership.DeleteUser(args.AnonymousID, true);

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜