here's a custom implementation of the asp.net profile provider working with the system.web.profile namespace
public class AsteryxProfileProvider : ProfileProvider
{
public AsteryxProfileProvider () { }
public string UserName { get; set; }
public override string ApplicationName
{
get { return AppSettings.ApplicationName(); }
set { }
}
public override void Initialize(string name, NameValueCollection config)
{
base.Initialize(name, config);
}
private void GetUsername(SettingsContext context)
{
UserName = ((string)context["UserName"]).Remove(0, ((string)context["UserName"]).IndexOf(@"\") + 1);
}
private List<ProfileProperty> GetPropertyValues()
{
DataSet ds = ProfileDA.GetProfileData(UserName, ApplicationName);
List<ProfileProperty> props = new List<ProfileProperty>();
foreach (DataRow row in ds.Tables[0].Rows)
props.Add(new ProfileProperty((String)row["PropertyName"], (String)row["StringValue"], (Byte[])row["BinaryValue"]));
return props;
}
public override System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyCollection collection)
{
GetUsername(context);
List<ProfileProperty> Propertys = GetPropertyValues();
System.Configuration.SettingsPropertyValue propValue = null;
System.Configuration.SettingsPropertyValueCollection Values = new System.Configuration.SettingsPropertyValueCollection();
foreach (System.Configuration.SettingsProperty property in collection)
{
propValue = new System.Configuration.SettingsPropertyValue(property);
ProfileProperty ProfileProp = Propertys.Find(delegate(ProfileProperty Predicate) { return Predicate.PropertyName == property.Name; });
if (ProfileProp == null)
{
if (property.PropertyType.IsPrimitive || property.PropertyType == typeof(String))
{
propValue.PropertyValue = null;
propValue.SerializedValue = null;
propValue.Deserialized = false;
}
}
else
{
propValue.Deserialized = false;
if (property.SerializeAs == SettingsSerializeAs.String)
{
propValue.SerializedValue = ProfileProp.StringValue;
}
else if (property.SerializeAs == SettingsSerializeAs.Binary)
{
propValue.SerializedValue = ProfileProp.BinaryValue;
}
}
Values.Add(propValue);
}
return Values;
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="collection"></param>
public override void SetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyValueCollection collection)
{
GetUsername(context);
ProfileProperty NewProfileProp = null;
foreach (System.Configuration.SettingsPropertyValue PropertyValue in collection)
{
if (PropertyValue.IsDirty && !PropertyValue.UsingDefaultValue)
{ {
NewProfileProp = new ProfileProperty();
NewProfileProp.PropertyName = PropertyValue.Name;
if (PropertyValue.Deserialized && PropertyValue.Property.SerializeAs != SettingsSerializeAs.Binary)
{
NewProfileProp.StringValue = PropertyValue.PropertyValue.ToString();
NewProfileProp.BinaryValue = new Byte[0];
}
else if (!PropertyValue.Deserialized && PropertyValue.Property.SerializeAs != SettingsSerializeAs.Binary)
{
NewProfileProp.StringValue = PropertyValue.PropertyValue.ToString();
NewProfileProp.BinaryValue = new Byte[0];
}
else if (PropertyValue.Deserialized && PropertyValue.Property.SerializeAs == SettingsSerializeAs.Binary)
{
NewProfileProp.BinaryValue = (Byte[])PropertyValue.SerializedValue;
NewProfileProp.StringValue = String.Empty;
}
else if (!PropertyValue.Deserialized && PropertyValue.Property.SerializeAs == SettingsSerializeAs.Binary)
{
MemoryStream strm = new System.IO.MemoryStream();
BinaryFormatter bin = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bin.Serialize(strm, PropertyValue.PropertyValue);
Byte[] PropertyData = new Byte[strm.Length];
strm.Seek(0, System.IO.SeekOrigin.Begin);
strm.Read(PropertyData, 0, PropertyData.Length);
NewProfileProp.BinaryValue = PropertyData;
NewProfileProp.StringValue = String.Empty;
}
ProfileDA.SetProfileData(UserName, NewProfileProp.PropertyName, NewProfileProp.StringValue, NewProfileProp.BinaryValue, ApplicationName);
}
}
}
//Other functions not implemented
}
public class ProfileProperty
{
public int SystemID { get; set; }
public int EmployeeID { get; set; }
public String Ntlogin { get; set; }
public String PropertyName { get; set; }
public String StringValue { get; set; }
public Byte[] BinaryValue { get; set; }
public DateTime LastUpdated { get; set; }
public ProfileProperty(string propertyName, string strValue, Byte[] binaryValue)
{
PropertyName = propertyName;
StringValue = strValue;
BinaryValue = binaryValue;
}
public ProfileProperty() { }
}
public static class ActiveUserCounter
{
private static DateTime LastTime = Convert.ToDateTime("1/1/1900");
private static int UserCount = 0;
public static int GetUserCount(string ntlogin)
{
if (UserCount == 0
|| DateTime.Now.AddMinutes(-1) > LastTime)
{
UserCount = ProfileDA.CountApplicationUsers(AppSettings.ApplicationName(), ntlogin);
LastTime = DateTime.Now;
}
return UserCount;
}
}
Patrick McNamara, BS-IS/CS, MBA, MAED
ASP.NET Web Application Developer
Asteryx, LLC.
http://asteryx.com
pat@asteryx.com