ASP.NET 使用
ProfileBase 类创建用于用户配置文件的类。在启动启用了用户配置文件的应用程序时,ASP.NET 会创建一个类型为
ProfileCommon 的新类,该类从
ProfileBase 类继承。强类型访问器被添加到profile配置节中为每个属性定义的
ProfileCommon 类中。
ProfileCommon 类的强类型访问器调用
ProfileBase 基类的GetPropertyValue 和SetPropertyValue方法,分别用于配置文件属性值的检索和设置。
ProfileCommon 类的一个实例被设置为 ASP.NET 应用程序的Profile属性的值。
呵呵,上面的简介从MSDN拷贝过来的。现在进入正题,大家在使用Profile的时候是不是有些郁闷的地方?比方说不能在BLL类库工程中使用ProfileCommon,这样就不可避免的在页面层中写些ProfileCommon的操作,我觉得这样操作实在太麻烦了,在查找了相关资料的时候最后还是搞定了,可以完全在后台类库工程中使用ProfileCommon的方法,废话完毕(已经知道的就别看拉,呵呵)。
首先我自己创建了一个EclProfileCommon的类,此类继承于ProfileBase类,下面是此类的代码
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Web;
using
System.Web.Profile;
![](/Images/OutliningIndicators/None.gif)
namespace
ecl.Common.Comm
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class EclProfileCommon : ProfileBase
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[SettingsAllowAnonymous(true)]
[ProfileProvider("SqlProfileProvider")]
public string EnglishName
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return base["EnglishName"].ToString(); }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ base["EnglishName"] = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
[SettingsAllowAnonymous(true)]
[ProfileProvider("SqlProfileProvider")]
public string ChineseName
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return base["ChineseName"].ToString(); }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ base["ChineseName"] = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
[SettingsAllowAnonymous(true)]
[ProfileProvider("SqlProfileProvider")]
public string Telephone
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return base["Telephone"].ToString(); }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ base["Telephone"] = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
[SettingsAllowAnonymous(true)]
[ProfileProvider("SqlProfileProvider")]
public string CustomSex
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return base["CustomSex"].ToString(); }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ base["CustomSex"] = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
[SettingsAllowAnonymous(true)]
[ProfileProvider("SqlProfileProvider")]
public string Country
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return base["Country"].ToString(); }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ base["Country"] = value; }
}
}
}
第一步搞定:现在开始配置下web.config
<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="20">
<providers>
<remove name="AspNetSqlProvider" />
<add name="SqlProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="SqlServices"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresUniqueEmail="true"
requiresQuestionAndAnswer="true"
minRequiredPasswordLength="4"
minRequiredNonalphanumericCharacters="0"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="100"
applicationName="/" />
</providers>
</membership>
<profile enabled="true" defaultProvider="SqlProfileProvider" inherits="ecl.Common.Comm.EclProfileCommon">
<providers>
<clear/>
<add name="SqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="SqlServices" applicationName="/"/>
</providers>
<!--<properties>
<add name="EnglishName" type="string" />
<add name="ChineseName" type="string" />
<add name="CustomSex" type="string" />
<add name="Telephone" type="string" />
<add name="Country" type="string"/>
</properties>-->
</profile>
大家注意:<profile enabled="true" defaultProvider="SqlProfileProvider" inherits="ecl.Common.Comm.EclProfileCommon">
中的我配置了inherits="ecl.Common.Comm.EclProfileCommon"这句话的意思是让net生成的ProfileCommon类继承此类这个很关键哦,呵呵
接下来看我在BLL里的调用吧:
/**/
/// <summary>
/// 新增用户信息
/// </summary>
/// <param name="obj">用户实体</param>
public
void
AddMember(Account obj)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (obj == null)
throw new NullReferenceException("参数不可以为 Null !");
![](/Images/OutliningIndicators/InBlock.gif)
MembershipCreateStatus status;
![](/Images/OutliningIndicators/InBlock.gif)
Membership.CreateUser(obj.NewUserName, obj.Password, obj.Email, obj.PasswordQuestion, obj.PasswordAnswer, obj.IsApproved, out status);
![](/Images/OutliningIndicators/InBlock.gif)
if (status != MembershipCreateStatus.Success)
throw new ApplicationException(status.ToString());
![](/Images/OutliningIndicators/InBlock.gif)
MembershipUser mu = Membership.GetUser(obj.NewUserName);
![](/Images/OutliningIndicators/InBlock.gif)
mu.Comment = obj.Comment;
![](/Images/OutliningIndicators/InBlock.gif)
Membership.UpdateUser(mu);
![](/Images/OutliningIndicators/InBlock.gif)
EclProfileCommon p = (EclProfileCommon)ProfileBase.Create(mu.UserName);
p.ChineseName = obj.ChineseName;
p.EnglishName = obj.EnglishName;
p.CustomSex = obj.Sex;
p.Telephone = obj.Telephone;
p.Save();
}
整个流程就这样了,我不太会表述。不明白的可以与我交流:zcyhappy@msn.com
达人表笑我!!!共享大家交流下,呵呵
欢迎来
www.msotec.com 交流学习