次の方法で共有


Custom Profiles with ASP.NET

I recently built a demo for a customer that required some extra fields for each registered user. And if you know anything about ASP.NET 2.0 and going forward, you're probably thinking that I should or did use the Profile object. And, yes, that is an option - add some field definitions to the web.config, turn on the SqlProfileProvider, and done. Easy.

But, what if you want to search on some of this new data. That's a problem! SqlProfileProvider stores your extended data as blobs, not as individual columns. So, I could write my own Profile provider, but I'm in Evangelism (not in consulting) so I'm looking for an easy way out. BAM! SqlTableProfileProvider to the rescue.

There was a little extra setup work, but not rocket science. Add the SqlTableProfileProvider code to the App_Code folder, update the web.config with the new column definitions and database connection string, make sure that my user primary key is configured per their instructions, and we're off to the races.

Example:

<add name="LastName"
type="string"
defaultValue="[null]"
customProviderData="LastName;nvarchar" />

After this, accessing the profile info just works, i.e. like Profile.LastName. There are a couple of patterns that you'll soon start repeating also. First, there's when you need to work with a different user's profile than the currenlty logged-in user(as in creating a new user). The important lines here are ProfileBase.Create and then saving the profile.

 MembershipCreateStatus IsCreated = MembershipCreateStatus.ProviderError;
MembershipUser mu = null;
 mu = Membership.CreateUser(this.UsernameTextBox.Text, this.PasswordTextBox.Text, "1@2.com", "Q", "A", true, out IsCreated);
if (IsCreated == MembershipCreateStatus.Success && mu != null)
{
    ProfileCommon userProfile = (ProfileCommon)System.Web.Profile.ProfileBase.Create(mu.UserName);

    userProfile.FirstName = this.FirstNameTextBox.Text;
    userProfile.LastName = this.LastNameTextBox.Text;

    userProfile.Save();

    Roles.AddUserToRole(mu.UserName, "CustomerRole"); 
 }

After this, there's when you need to get the user's identifier - like when you're using the user primary key to join with other tables. The challenge here is that User.Identity.Name works great for ASP.NET Membership functions, but doesn't help here. Accessing the user's unique identfiers is as simple as accessing the MembersshipUser.ProviderUserKey. Using this property directly is frowned upon by some (like Mr. Guthrie), so you might consider a different approach - but it was a quick an easy choice for me:

 MembershipUser mu = Membership.GetUser();

cmd = new SqlCommand("OrderCreate", conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(
    new SqlParameter("@userId", mu.ProviderUserKey));

 

So, there you go. I hope this will help someone whenever they need a quick way to use ASP.NET Profile data in real ways including lookup's. Be sure to check out the paper on the above link for exact instructions.