Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Profile Provider in Asp.Net MVC

0.00/5 (No votes)
5 Oct 2010 1  
Finding very little information on how to  implement Profiles in Asp.Net MVC, I thought I'd share my solution to implement this.For demonstration

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Finding very little information on how to  implement Profiles in Asp.Net MVC, I thought I'd share my solution to implement this.

For demonstration purposes, let's assume you want to store a user's First and Last Name only.

Firstly I created a "UserProfile" class that derive from "System.Web.Profile.ProfileBase" and implemented it as follows:

    public class UserProfile : ProfileBase
    {
        [SettingsAllowAnonymous(false)]
        public string FirstName
        {
            get { return base["FirstName"] as string; }
            set { base["FirstName"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string LastName
        {
            get { return base["LastName"] as string; }
            set { base["LastName"] = value; }
        }

        public static UserProfile GetUserProfile(string username)
        {
            return Create(username) as UserProfile;
        }

        public static UserProfile GetUserProfile()
        {
            return Create(Membership.GetUser().UserName) as UserProfile;
        }
    }

That being done, I ensure my Profile Provider have been set up properly in my web.config (note how I inherit from the above class)

    <profile inherits="Krok.Core.BusinessLogic.Models.Account.UserProfile">
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider"
             type="System.Web.Profile.SqlProfileProvider"
             connectionStringName="ApplicationServices" applicationName="/" />
      </providers>
    </profile>

As I like to keep logic seperated, and also only use strongly typed view models, I created a view model class seperately:

    public class UserProfileViewModel
    {
        [DisplayName("First Name")]
        [DataType(DataType.Text)]
        public string FirstName { get; set; }

        [DisplayName("Last Name")]
        [DataType(DataType.Text)]
        public string LastName { get; set; }
    }

Now, I created two action methods, one for displaying a user's profile, one for editing it:

        [Authorize]
        public ViewResult Profile()
        {
            UserProfile profile = UserProfile.GetUserProfile(User.Identity.Name);
            var model = new UserProfileViewModel
                                             {
                                                 FirstName = profile.FirstName,
                                                 LastName = profile.LastName
                                             };
           
            return View(model);
        }

        [HttpPost]
        [Authorize]
        public ActionResult Profile(UserProfileViewModel model)
        {
            if (ModelState.IsValid)
            {
                UserProfile profile = UserProfile.GetUserProfile(User.Identity.Name);
                profile.FirstName = model.FirstName;
                profile.LastName = model.LastName;
                profile.Save();
                return RedirectToRoute("Account.Profile");
            }
            return View(model);
        }

Now, you obviously just will create two views, both strongly typed (UserProfileViewModel)

Hope this help for anyone that need to implement Profiles in a Asp.Net MVC application.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here