Customize Profile Provider
1. Implement ProfileProvider class add override the Init method , GetPropertyValues, SetPropertyValues.
#region
private string _appName;
private int _cookieExpire;
private string _cookieName;
#endregion
#region
public override string ApplicationName
{
get
{
return _appName;
}
set
{
_appName = value;
}
}
public string CookieName
{
get
{
return _cookieName;
}
set
{
_cookieName = value;
}
}
public int CookieExpire
{
get
{
return _cookieExpire;
}
set
{
_cookieExpire = value;
}
}
#endregion
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
if (config == null)
throw new ArgumentNullException("Null configuration parameters");
if (string.IsNullOrEmpty(name))
{
name = "CookieProfileProvider";
}
base.Initialize(name, config);
_appName = config["applicationName"];
if (string.IsNullOrEmpty(_appName))
_appName = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
config.Remove("applicationName");
_cookieName = config["CookieName"];
if (string.IsNullOrEmpty(_cookieName))
_cookieName = ".GaryCookie";
config.Remove("CookieName");
bool result = int.TryParse(config["CookieExpire"],out _cookieExpire);
if(!result)
_cookieExpire=10;
config.Remove("CookieExpire");
if (config.Count > 0)
{
string attrib = config.GetKey(0);
if (!String.IsNullOrEmpty(attrib))
throw new ProviderException("Unrecognized attribute: " + attrib);
}
}
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context,
SettingsPropertyCollection collection)
{
string username = (string)context["UserName"];
bool authenticated = (bool)context["IsAuthenticated"];
SettingsPropertyValueCollection settings = new SettingsPropertyValueCollection();
if (collection.Count == 0)
return settings;
foreach (SettingsProperty sp in collection)
{
settings.Add(new SettingsPropertyValue(sp));
}
string cookie = _cookieName + "_" + username;
HttpCookie cookieObj = HttpContext.Current.Request.Cookies[cookie];
if (cookieObj == null)
return settings;
string data = cookieObj["SerializedData"];
BinaryFormatter formater = new BinaryFormatter();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(data));
Hashtable table = formater.Deserialize(ms) as Hashtable;
foreach (SettingsPropertyValue spv in settings)
{
spv.Deserialized = true;
spv.PropertyValue = table[spv.Name];
}
ms.Close();
return settings;
}
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
{
string username = (string)context["UserName"];
bool authenticated = (bool)context["IsAuthenticated"];
if (String.IsNullOrEmpty(username) || collection.Count == 0)
return;
string cookie = _cookieName + "_" + username;
HttpCookie cookieObj = HttpContext.Current.Request.Cookies[cookie];
if (cookieObj == null)
cookieObj = new HttpCookie(cookie);
cookieObj.Expires=DateTime.Now.AddMinutes(CookieExpire);
Hashtable table = new Hashtable();
foreach (SettingsPropertyValue pv in collection)
{
if (!(bool)pv.Property.Attributes["AllowAnonymous"] && !authenticated)
{
continue;
}
table.Add(pv.Name, pv.PropertyValue);
}
BinaryFormatter formater = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formater.Serialize(ms, table);
string dataString = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
ms.Close();
cookieObj["SerializedData"] = dataString;
HttpContext.Current.Response.Cookies.Add(cookieObj);
}
private string _appName;
private int _cookieExpire;
private string _cookieName;
#endregion
#region
public override string ApplicationName
{
get
{
return _appName;
}
set
{
_appName = value;
}
}
public string CookieName
{
get
{
return _cookieName;
}
set
{
_cookieName = value;
}
}
public int CookieExpire
{
get
{
return _cookieExpire;
}
set
{
_cookieExpire = value;
}
}
#endregion
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
if (config == null)
throw new ArgumentNullException("Null configuration parameters");
if (string.IsNullOrEmpty(name))
{
name = "CookieProfileProvider";
}
base.Initialize(name, config);
_appName = config["applicationName"];
if (string.IsNullOrEmpty(_appName))
_appName = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
config.Remove("applicationName");
_cookieName = config["CookieName"];
if (string.IsNullOrEmpty(_cookieName))
_cookieName = ".GaryCookie";
config.Remove("CookieName");
bool result = int.TryParse(config["CookieExpire"],out _cookieExpire);
if(!result)
_cookieExpire=10;
config.Remove("CookieExpire");
if (config.Count > 0)
{
string attrib = config.GetKey(0);
if (!String.IsNullOrEmpty(attrib))
throw new ProviderException("Unrecognized attribute: " + attrib);
}
}
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context,
SettingsPropertyCollection collection)
{
string username = (string)context["UserName"];
bool authenticated = (bool)context["IsAuthenticated"];
SettingsPropertyValueCollection settings = new SettingsPropertyValueCollection();
if (collection.Count == 0)
return settings;
foreach (SettingsProperty sp in collection)
{
settings.Add(new SettingsPropertyValue(sp));
}
string cookie = _cookieName + "_" + username;
HttpCookie cookieObj = HttpContext.Current.Request.Cookies[cookie];
if (cookieObj == null)
return settings;
string data = cookieObj["SerializedData"];
BinaryFormatter formater = new BinaryFormatter();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(data));
Hashtable table = formater.Deserialize(ms) as Hashtable;
foreach (SettingsPropertyValue spv in settings)
{
spv.Deserialized = true;
spv.PropertyValue = table[spv.Name];
}
ms.Close();
return settings;
}
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
{
string username = (string)context["UserName"];
bool authenticated = (bool)context["IsAuthenticated"];
if (String.IsNullOrEmpty(username) || collection.Count == 0)
return;
string cookie = _cookieName + "_" + username;
HttpCookie cookieObj = HttpContext.Current.Request.Cookies[cookie];
if (cookieObj == null)
cookieObj = new HttpCookie(cookie);
cookieObj.Expires=DateTime.Now.AddMinutes(CookieExpire);
Hashtable table = new Hashtable();
foreach (SettingsPropertyValue pv in collection)
{
if (!(bool)pv.Property.Attributes["AllowAnonymous"] && !authenticated)
{
continue;
}
table.Add(pv.Name, pv.PropertyValue);
}
BinaryFormatter formater = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formater.Serialize(ms, table);
string dataString = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
ms.Close();
cookieObj["SerializedData"] = dataString;
HttpContext.Current.Response.Cookies.Add(cookieObj);
}
//Web.config
<anonymousIdentification enabled="true"/>
<profile defaultProvider="MyProfileProvider" enabled="true">
<providers>
<clear/>
<add name="MyProfileProvider" type="MyProfileProvider"/>
</providers>
<properties>
<add allowAnonymous="true" defaultValue="Blue" name="BackColor" type="string"/>
<add allowAnonymous="true" defaultValue="MyTest" name="CustomDatas" type="String"/>
</properties>
</profile>
<profile defaultProvider="MyProfileProvider" enabled="true">
<providers>
<clear/>
<add name="MyProfileProvider" type="MyProfileProvider"/>
</providers>
<properties>
<add allowAnonymous="true" defaultValue="Blue" name="BackColor" type="string"/>
<add allowAnonymous="true" defaultValue="MyTest" name="CustomDatas" type="String"/>
</properties>
</profile>
//
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
Response.Write(Profile.CustomDatas + "<br/>" + Profile.BackColor + "<br/>");
}
protected void Button1_Click(object sender, EventArgs e)
{
Profile.BackColor = TextBox1.Text;
Profile.CustomDatas = TextBox2.Text;
Response.Write(Profile.CustomDatas + "<br/>" + Profile.BackColor + "<br/>");
}
{
if(!IsPostBack)
Response.Write(Profile.CustomDatas + "<br/>" + Profile.BackColor + "<br/>");
}
protected void Button1_Click(object sender, EventArgs e)
{
Profile.BackColor = TextBox1.Text;
Profile.CustomDatas = TextBox2.Text;
Response.Write(Profile.CustomDatas + "<br/>" + Profile.BackColor + "<br/>");
}
浙公网安备 33010602011771号