|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionCreating a website with membership feature is easy in ASP.NET. The Membership feature of ASP.NET is easy to install (using aspnet_regsql) and just as easy to use in your code. This article will not cover how to install and use it. If you want to learn more about ASP.NET's Membership feature, you should take a look at the article: Multipart Series on ASP.NET's Membership, Roles, and Profile from 4GuysFromRolla. You will have to set up custom properties in your web.config file like this: <profile>
<properties>
<add name="PostalCode" />
</properties>
</profile>
You can even group your properties like this: <profile>
<properties>
<group name="Personal">
<add name="FirstName" type="System.String" />
<add name="LastName" type="System.String" />
<add name="Email" type="System.String" />
<add name="BirthDate" type="System.DateTime" />
</group>
<group name="Address">
<add name="Street" type="System.String" />
<add name="ZipCode" type="System.String" />
<add name="City" type="System.String" />
</group>
</properties>
</profile>
To access these properties, you can use the The program is very easy, and uses The web.config example above would give us this code: public static class UserProfile
{
public class Personal
{
public static System.String FirstName
{
get { return HttpContext.Current.Profile.GetPropertyValue("FirstName")
as System.String; }
set { HttpContext.Current.Profile.SetPropertyValue("FirstName", value); }
}
public static System.String LastName
{
get { return HttpContext.Current.Profile.GetPropertyValue("LastName")
as System.String; }
set { HttpContext.Current.Profile.SetPropertyValue("LastName", value); }
}
public static System.String Email
{
get { return HttpContext.Current.Profile.GetPropertyValue("Email")
as System.String; }
set { HttpContext.Current.Profile.SetPropertyValue("Email", value); }
}
public static System.DateTime BirthDate
{
get { return (System.DateTime)HttpContext.Current.Profile.
GetPropertyValue("BirthDate"); }
set { HttpContext.Current.Profile.SetPropertyValue("BirthDate", value); }
}
}
public class Address
{
public static System.String Street
{
get { return HttpContext.Current.Profile.GetPropertyValue("Street")
as System.String; }
set { HttpContext.Current.Profile.SetPropertyValue("Street", value); }
}
public static System.String ZipCode
{
get { return HttpContext.Current.Profile.GetPropertyValue("ZipCode")
as System.String; }
set { HttpContext.Current.Profile.SetPropertyValue("ZipCode", value); }
}
public static System.String City
{
get { return HttpContext.Current.Profile.GetPropertyValue("City")
as System.String; }
set { HttpContext.Current.Profile.SetPropertyValue("City", value); }
}
}
public static void Save()
{
HttpContext.Current.Profile.Save();
}
}
To make it even more easy, you can add the built-in properties in the same class: public static bool IsAuthenticated
{
get { return HttpContext.Current.User.Identity.IsAuthenticated; }
}
public static Guid UserId
{
get { return (Guid)Membership.GetUser().ProviderUserKey; }
}
public static string Email
{
get { return Membership.GetUser().Email; }
}
public static DateTime DateCreated
{
get { return Membership.GetUser().CreationDate; }
}
public static string UserName
{
get { return Membership.GetUser().UserName; }
}
Using this class is easy. This example will show you how to read properties and change properties: // read properties
string firstname = UserProfile.Personal.FirstName;
// write properties
UserProfile.Personal.FirstName = "Bill";
UserProfile.Personal.LastName = "Gates";
// save changes
UserProfile.Save();
Points of Interest
|
||||||||||||||||||||||||||||||||||||||||