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类,如下:

 

代码
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方法啦。

 

 

 

posted @ 2010-01-04 14:53  [静水流深]  阅读(552)  评论(0编辑  收藏  举报