accessing profile.newproperty in MVC web applications

前端 未结 1 1782
青春惊慌失措
青春惊慌失措 2021-01-15 05:08

I recently asked this question How to persist anon user selection (ex: theme selection). and started to learn about ASP.NET profiles and their properties in the web config.

相关标签:
1条回答
  • 2021-01-15 05:44

    I just saw your question, yes you are right, the answer I posted was related to web sites and therefore, it doesn't work with Web Applications or MVC

    Here I will show you code to work with profiles in MVC using anonymous and authenticated user profiles

    Output

    Anonymous user - No profile set yet

    enter image description here

    Anonymous user - profile set

    enter image description here

    Authenticated user - profile migrated

    enter image description here

    Web.config

    <anonymousIdentification enabled="true"/>
    <profile inherits="ProfileInWebApplicationMVC1.UserProfile">
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
      </providers>
    </profile>
    

    UserProfile class

    public class UserProfile : ProfileBase
    {
        public static UserProfile GetProfile()
        {
            return HttpContext.Current.Profile as UserProfile;
        }
    
        [SettingsAllowAnonymous(true)]
        public DateTime? LastVisit
        {
            get { return base["LastVisit"] as DateTime?; }
            set { base["LastVisit"] = value; }
        }
    
        public static UserProfile GetProfile(string userID)
        {
            return ProfileBase.Create(userID) as UserProfile;
        }
    }
    

    Home controller

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
    
            var p = UserProfile.GetProfile();
    
            return View(p.LastVisit);
        }
    
        [HttpPost]
        public ActionResult SaveProfile()
        {
            var p = UserProfile.GetProfile();
    
            p.LastVisit = DateTime.Now;
            p.Save();
    
            return RedirectToAction("Index");
        }
    

    Index view

    @if (!this.Model.HasValue)
    {
        @: No profile detected
    }
    else
    {
        @this.Model.Value.ToString()
    }
    
    @using (Html.BeginForm("SaveProfile", "Home"))
    {
        <input type="submit" name="name" value="Save profile" />
    }
    

    And finally, when you are an anonymous user you can have your own profile however, once you register to the site, you need to migrate your current profile to be used with your new account. This is because ASP.Net membership, creates a new profile when a user logs-in

    Global.asax, code to migrate profiles

        public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args)
        {
            var anonymousProfile = UserProfile.GetProfile(args.AnonymousID);
            var f = UserProfile.GetProfile(); // current logged in user profile
    
            if (anonymousProfile.LastVisit.HasValue)
            {
                f.LastVisit = anonymousProfile.LastVisit;
                f.Save();
            }
    
            ProfileManager.DeleteProfile(args.AnonymousID);
            AnonymousIdentificationModule.ClearAnonymousIdentifier();
            Membership.DeleteUser(args.AnonymousID, true);
        }
    
    0 讨论(0)
提交回复
热议问题