ASP.NET dostarcza wygodnego i prostego mechanizmu do obsługi profili użytkowników. Do profilu użytkownika można łatwo dodać kolejne dane – w tym celu wystarczy odpowiednio zmodyfikować web.config, np. w następujący sposób:
<profile enabled="true" automaticSaveEnabled="true" defaultProvider="SqlProvider">
<properties>
<add name="Name" type ="string" allowAnonymous="false" />
<add name="Theme" type ="string" allowAnonymous="false" />
</properties>
<providers>
<add name="SqlProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="LocalSql" applicationName="MyApplicationName"/>
</providers>
</profile>
Następnie można odwoływać się do ustawień profilu przez obiekt
Profile, np.:
string myName=Profile.Name; Profile.Theme="NewAwesomeTheme";
Niestety! powyższy kod będzie działać tylko gdy korzystamy z tzw.
„web
site project”, nie działa natomiast w „web
application project”. Dlaczego? Gdyż w przypadku „web
site project” tworzona jest automatycznie w czasie działania
dodatkowa klasa do obsługi profili, niestety w „web
application project” dodatkowa klasa nie powstaje
automatycznie.
Oczywiście można
dalej obsługiwać profile „ręcznie”, np.:
string myName=
(string)System.Web.HttpContext.Current.Profile.GetPropertyValue("Name");
System.Web.HttpContext.Current.Profile.SetPropertyValue
("Theme", "NewAwesomeTheme");
Można nawet „ręcznie wydłubać” klasę obsługującą profile,
np. podobną do tej:
namespace MyAwesomeApplication {
using System;
using System.Web;
using System.Web.Profile;
using System.Configuration;
public partial class WebProfile {
private System.Web.Profile.ProfileBase _profileBase;
public WebProfile() {
this._profileBase = new System.Web.Profile.ProfileBase();
}
public WebProfile(System.Web.Profile.ProfileBase profileBase) {
this._profileBase = profileBase;
}
public virtual string Name {
get {
return ((string)(this.GetPropertyValue("Name")));
}
set {
this.SetPropertyValue("Name", value);
}
}
public virtual string Theme {
get {
return ((string)(this.GetPropertyValue("Theme")));
}
set {
this.SetPropertyValue("Theme", value);
}
}
public static WebProfile Current {
get {
return new WebProfile(System.Web.HttpContext.Current.Profile);
}
}
public virtual System.Web.Profile.ProfileBase ProfileBase {
get {
return this._profileBase;
}
}
public virtual object this[string propertyName] {
get {
return this._profileBase[propertyName];
}
set {
this._profileBase[propertyName] = value;
}
}
public virtual string UserName {
get {
return this._profileBase.UserName;
}
}
public virtual bool IsAnonymous {
get {
return this._profileBase.IsAnonymous;
}
}
public virtual bool IsDirty {
get {
return this._profileBase.IsDirty;
}
}
public virtual System.DateTime LastActivityDate {
get {
return this._profileBase.LastActivityDate;
}
}
public virtual System.DateTime LastUpdatedDate {
get {
return this._profileBase.LastUpdatedDate;
}
}
public virtual System.Configuration.SettingsProviderCollection Providers {
get {
return this._profileBase.Providers;
}
}
public virtual System.Configuration.SettingsPropertyValueCollection PropertyValues {
get {
return this._profileBase.PropertyValues;
}
}
public virtual System.Configuration.SettingsContext Context {
get {
return this._profileBase.Context;
}
}
public virtual bool IsSynchronized {
get {
return this._profileBase.IsSynchronized;
}
}
public static System.Configuration.SettingsPropertyCollection Properties {
get {
return System.Web.Profile.ProfileBase.Properties;
}
}
public static WebProfile GetProfile(string username) {
return new WebProfile(System.Web.Profile.ProfileBase.Create(username));
}
public static WebProfile GetProfile(string username, bool authenticated) {
return new WebProfile(System.Web.Profile.ProfileBase.Create(username, authenticated));
}
public virtual object GetPropertyValue(string propertyName) {
return this._profileBase.GetPropertyValue(propertyName);
}
public virtual void SetPropertyValue(string propertyName, object propertyValue) {
this._profileBase.SetPropertyValue(propertyName, propertyValue);
}
public virtual System.Web.Profile.ProfileGroupBase GetProfileGroup(string groupName) {
return this._profileBase.GetProfileGroup(groupName);
}
public virtual void Initialize(string username, bool isAuthenticated) {
this._profileBase.Initialize(username, isAuthenticated);
}
public virtual void Save() {
this._profileBase.Save();
}
public virtual void Initialize(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyCollection properties, System.Configuration.SettingsProviderCollection providers) {
this._profileBase.Initialize(context, properties, providers);
}
public static System.Configuration.SettingsBase Synchronized(System.Configuration.SettingsBase settingsBase) {
return System.Web.Profile.ProfileBase.Synchronized(settingsBase);
}
public static System.Web.Profile.ProfileBase Create(string userName) {
return System.Web.Profile.ProfileBase.Create(userName);
}
public static System.Web.Profile.ProfileBase Create(string userName, bool isAuthenticated) {
return System.Web.Profile.ProfileBase.Create(userName, isAuthenticated);
}
}
}
Natomiast jeszcze łatwiej można wykorzystać narzędzie „Web
Profile Builder”
(http://code.msdn.microsoft.com/WebProfileBuilder),
które instalujemy, następnie modyfikujemy plik projektu (w sensie
Visual Studio), poprzez dodanie linii:
<Import Project="$(MSBuildExtensionsPath)\WebProfileBuilder\WebProfileBuilder.targets" />
Dzięki temu podczas kompilacji zostanie automatycznie wytworzony
plik „WebProfile.cs”, który wystarczy dodać do projektu. Plik
ten będzie automatycznie aktualizowany po każdej zmianie
web.config.
Proste prawda?







Brak komentarzy:
Prześlij komentarz