Membership之Profile

    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;

namespace ecl.Common.Comm
{
    
public class EclProfileCommon : ProfileBase
    
{
        [SettingsAllowAnonymous(
true)]
        [ProfileProvider(
"SqlProfileProvider")]
        
public string EnglishName
        
{
            
get return base["EnglishName"].ToString(); }
            
set base["EnglishName"= value; }
        }


        [SettingsAllowAnonymous(
true)]
        [ProfileProvider(
"SqlProfileProvider")]
        
public string ChineseName
        
{
            
get return base["ChineseName"].ToString(); }
            
set base["ChineseName"= value; }
        }


        [SettingsAllowAnonymous(
true)]
        [ProfileProvider(
"SqlProfileProvider")]
        
public string Telephone
        
{
            
get return base["Telephone"].ToString(); }
            
set base["Telephone"= value; }
        }


        [SettingsAllowAnonymous(
true)]
        [ProfileProvider(
"SqlProfileProvider")]
        
public string CustomSex
        
{
            
get return base["CustomSex"].ToString(); }
            
set base["CustomSex"= value; }
        }


        [SettingsAllowAnonymous(
true)]
        [ProfileProvider(
"SqlProfileProvider")]
        
public string Country
        
{
            
get return base["Country"].ToString(); }
            
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)
        
{
            
if (obj == null)
                
throw new NullReferenceException("参数不可以为 Null !");

            MembershipCreateStatus status;

            Membership.CreateUser(obj.NewUserName, obj.Password, obj.Email, obj.PasswordQuestion, obj.PasswordAnswer, obj.IsApproved, 
out status);

            
if (status != MembershipCreateStatus.Success)
                
throw new ApplicationException(status.ToString());

            MembershipUser mu 
= Membership.GetUser(obj.NewUserName);

            mu.Comment 
= obj.Comment;

            Membership.UpdateUser(mu);

            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   交流学习
posted @ 2007-08-28 17:14  咖啡淡了  阅读(2206)  评论(4编辑  收藏  举报