ASP.NET Provider模型

转载:http://www.cnblogs.com/mqingqing123/archive/2006/05/09/394721.html

providers模型配置简介(一)

一.ASP.NET2.0可以利用的Providers模型

 下面列出了ASP.NET2.0已经提供的Providers模型

 成员ProviderMembership Provider):验证您站点的用户

 角色ProviderRole Provider):授权您站点的用户

  档案ProviderProfile Provider):用于个性化您站点的设置

导航ProviderSiteMap Provider):使用站点地图(web.sitemap)来导航你的站点

会话ProviderSession State Store Provider):利用基础数据库存放Session

二.Membership Provider (主要用于自定义用户验证等)

1. web.config配置中添加:
      <authentication mode="Forms">
        <forms defaultUrl="Default.aspx"  loginUrl="login.aspx" protection="All" timeout="60" name=".clb" ></forms>
      </authentication>
      <authorization>
        <deny users="?"/>
      </authorization>
      <membership defaultProvider="MyMembership">
        <providers>
          <add name="MyMembership" type="Test.code.MyMembership"/>
        </providers>
      </membership>

      <roleManager defaultProvider="MyRoleProvider" enabled="true">
        <providers>
          <add name="MyRoleProvider" type="Test.code.MyRoleProvider"/>
        </providers>
      </roleManager>

2.添加 MyMembership类,如下:

 

ExpandedBlockStart.gif 代码
using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

namespace  Test.code
{
    
public   class  MyMembership:System.Web.Security.MembershipProvider
    {

        
public   override   bool  ValidateUser( string  username,  string  password)
        {
            
// 编写检测用户密码的代码。

        }
        
public   override   string  ApplicationName
        {
            
get
            {
                
throw   new  Exception( " The method or operation is not implemented. " );
            }
            
set
            {
                
throw   new  Exception( " The method or operation is not implemented. " );
            }
        }

        
public   override   bool  ChangePassword( string  username,  string  oldPassword,  string  newPassword)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override   bool  ChangePasswordQuestionAndAnswer( string  username,  string  password,  string  newPasswordQuestion,  string  newPasswordAnswer)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override  MembershipUser CreateUser( string  username,  string  password,  string  email,  string  passwordQuestion,  string  passwordAnswer,  bool  isApproved,  object  providerUserKey,  out  MembershipCreateStatus status)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override   bool  DeleteUser( string  username,  bool  deleteAllRelatedData)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override   bool  EnablePasswordReset
        {
            
get  {  throw   new  Exception( " The method or operation is not implemented. " ); }
        }

        
public   override   bool  EnablePasswordRetrieval
        {
            
get  {  throw   new  Exception( " The method or operation is not implemented. " ); }
        }

        
public   override  MembershipUserCollection FindUsersByEmail( string  emailToMatch,  int  pageIndex,  int  pageSize,  out   int  totalRecords)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override  MembershipUserCollection FindUsersByName( string  usernameToMatch,  int  pageIndex,  int  pageSize,  out   int  totalRecords)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override  MembershipUserCollection GetAllUsers( int  pageIndex,  int  pageSize,  out   int  totalRecords)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override   int  GetNumberOfUsersOnline()
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override   string  GetPassword( string  username,  string  answer)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override  MembershipUser GetUser( string  username,  bool  userIsOnline)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override  MembershipUser GetUser( object  providerUserKey,  bool  userIsOnline)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override   string  GetUserNameByEmail( string  email)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override   int  MaxInvalidPasswordAttempts
        {
            
get  {  throw   new  Exception( " The method or operation is not implemented. " ); }
        }

        
public   override   int  MinRequiredNonAlphanumericCharacters
        {
            
get  {  throw   new  Exception( " The method or operation is not implemented. " ); }
        }

        
public   override   int  MinRequiredPasswordLength
        {
            
get  {  throw   new  Exception( " The method or operation is not implemented. " ); }
        }

        
public   override   int  PasswordAttemptWindow
        {
            
get  {  throw   new  Exception( " The method or operation is not implemented. " ); }
        }

        
public   override  MembershipPasswordFormat PasswordFormat
        {
            
get  {  throw   new  Exception( " The method or operation is not implemented. " ); }
        }

        
public   override   string  PasswordStrengthRegularExpression
        {
            
get  {  throw   new  Exception( " The method or operation is not implemented. " ); }
        }

        
public   override   bool  RequiresQuestionAndAnswer
        {
            
get  {  throw   new  Exception( " The method or operation is not implemented. " ); }
        }

        
public   override   bool  RequiresUniqueEmail
        {
            
get  {  throw   new  Exception( " The method or operation is not implemented. " ); }
        }

        
public   override   string  ResetPassword( string  username,  string  answer)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override   bool  UnlockUser( string  userName)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }

        
public   override   void  UpdateUser(MembershipUser user)
        {
            
throw   new  Exception( " The method or operation is not implemented. " );
        }
    }
}

 

 

本人比较懒,代码就略掉了,自己写吧。

ValidateUser方法重写后,就可以使用login控件啦,而不需要配置VS默认的数据库.

 

 

3。MyRoleProvider类编写。需要继承System.Web.Security.RoleProvider ,重写方法后。

 

在代码中就可以使用Roles.方法名使用啦。。

例如:Roles.IsUserInRole("admin") 检查当前用户是否属于admin组,

当然前题要在MyRoleProvider 中重写IsUserInRole方法啦。

 

 

 

转载于:https://www.cnblogs.com/qingyi/archive/2010/01/04/1638899.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值