Wednesday, September 21, 2011

Using ASP.Net Profile Feature in a Web Application Project

So, I decided to take advantage of the Profile feature of .NET to store some personal data per user. Shouldn't be a big deal right? After all, you just call the appropriate assembly and then, you should be able to invoke Profile.Save() and like magic the data is now in your ASP database on the Profiles table.

This is when I came across a huge set of posts and threads about how complex and confusing it can be to implement the Profile feature if you have a Web Application Project.

Among the confusing, credit should be due to Lee Dumond and Scott Gu as usual for posting some awesome examples in their blogs.

By following Lee's blog, below is my implementation of the same technique to be able to use Profiles in my Web Application Project. I thought it would be a good idea to post it, in order to give you a second example.

Step 1. Created a Class named ProfileInfo which exposes the properties that I want to save in my profile, in this case, let's work with two FirstName and Last Name

namespace Project1.Account
{
    [Serializable]
    public class ProfileInfo
    {
        public string FirstName { getset; } 
        public string LastName { getset; } 
    }
}

Step 2. Created a second Class named wProfile, this class is inherited from the ProfileBase class and will be used to expose the profile functionality on other pages in the site.

using System.Web;
using System.Web.Profile;
namespace Project1.Account
{
    public class wProfile : ProfileBase
    {
 
        public ProfileInfo ProfileInfo
        { 
            get { return (ProfileInfo) GetPropertyValue("ProfileInfo"); }       
        } 
 
        public static wProfile GetProfile() 
        { 
            return (wProfileHttpContext.Current.Profile; 
        } 
 
        public static wProfile GetProfile(string userName) 
        { 
            return (wProfile) Create(userName); 
        }  
    }
}

Step 3. Modify your Web.config file to implement a SQLProfileProvider. Notice the "inherits" on the defaultProvider definition. Also noticed that the connection ApplicationServices was previously defined.

    <profile defaultProvider="AspNetSqlProfileProvider" inherits="Project1.Account.wProfile"
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/wMyApp"/>
      </providers>
    </profile>

Step 4. Implementing the logic in my ASPX pages

        protected void Page_Load(object sender, EventArgs e)
        {
            wProfile lProfile = wProfile.GetProfile(Membership.GetUser().UserName);
            Label1.Text = "Welcome, " + lProfile.ProfileInfo.FirstName + " " + lProfile.ProfileInfo.LastName;
        }

Happy Coding, Hope this helps,
Will




8 comments:

  1. It is enough just to use the following constructions to access profile values:

    To get value from profile:
    HttpContext.Current.Profile.GetPropertyValue("FirstName");
    To set value:
    HttpContext.Current.Profile.SetPropertyValue("FirstName", "ValueYouWantToSet");

    ReplyDelete
  2. Totally agree with the above comment as long as your project type is not Web Application. Otherwise profiles are not available in the context.

    ReplyDelete
  3. could you tell me how to create the new profile in aspnet_Profile?Thank u.

    ReplyDelete
  4. Just desire to ѕay your aгticle is as surprіsing.

    The сlaritу іn уour post is just cool anԁ i coulԁ assume you are аn еxрert on this ѕubject.

    Fine with уour permiѕѕion allow me to gгab your RSS feed to keep updated
    with fοrthcoming post. Thankѕ а million and
    pleaѕe contіnue the enjоyable ωork.


    My blog post; Daniel Chavez Moran

    ReplyDelete
  5. Still if your cаr iѕ in consummate ωorking order,
    if thе market note vаluе of the vehicle ѕinks beѕіdes what will bе
    emplοyed to deciԁe your future policy reportage anԁ auto insurancе gradeѕ.


    Look into my web blog - online auto insurance

    ReplyDelete
  6. When іt follows to choοsing аuto insurancе, іt's advisable phoned a No Fault State.

    My site auto insurance online

    ReplyDelete
  7. Every ωeekend i useԁ to рay a quick visit this wеb site, because i
    want enjоyment, аs thiѕ thiѕ ωebsite conatіons in fact ρleаsant funny stuff too.


    Feеl fгеe to viѕit mу weblοg -
    reputation management

    ReplyDelete
  8. Thank you sir for your nice article, can you please tell me the demo example or simple application that how to use generic list in asp.net profile.

    ReplyDelete