在Identity 增加自己的属性 部门,并且使用access mdb文件实现角色验证

代码
using System;
using System.Collections;
using System.Data.OleDb;

/****************************************************************************

 * 函数使用说明 
 * 一、首先在使用web.config中设置 数据库连接字符串 mdb文件
 *   <connectionStrings>
    <add name="Main" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source='D:\Backup\我的文档\Visual Studio 2008\WebSites\WebSite2\App_Data\data.mdb'"/>
  </connectionStrings>
 * 二、在代码中加入引用 using MyUserLoginClass;
 * 三、在  protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)中书写代码
          {
            Login m_Login = (Login)sender;
            MyPrincipal principal = new MyPrincipal(m_Login.UserName, m_Login.Password);

            if(principal.Identity.IsAuthenticated)
            {
                // 如果用户通过验证,则生成用户验证票 
                Context.User = principal;

                MyIdentity myid= (MyIdentity)(Context.User.Identity);
                
                Label1.Text = myid.Department;

                System.Web.Security.FormsAuthentication.SetAuthCookie(m_Login.UserName, true);

                e.Authenticated = true;


                return;
            }
            else
            {
                m_Login.FailureText = "用户名或密码错误";
                e.Authenticated = false;
                return;
            }

*****************************************************************************
*/

namespace MyUserLoginClass

     
/// <summary> 
     
/// MyPrincipal 的摘要说明。 
     
/// </summary> 
     
/// 实现IPrincipal接口 
     public class MyPrincipal : System.Security.Principal.IPrincipal 
     {
         
private MyIdentity identity; 

        
private ArrayList roleList;        

        
public MyPrincipal(string UserName,string Password) 
        { 
           
// 
           
// TODO: 在此处添加构造函数逻辑 
           
// 
           identity = new MyIdentity(UserName,Password);

           
if (identity.IsAuthenticated)
           {
               roleList 
= new ArrayList();
               roleList 
= identity.RoleList;
           }             
        } 
         
        
public ArrayList RoleList 
        { 
           
get 
           { 
              
return roleList; 
           } 
        } 
        
#region IPrincipal 成员 

        
public System.Security.Principal.IIdentity Identity 
        { 
           
get 
           { 
              
// TODO:    添加 MyPrincipal.Identity getter 实现 
              return identity; 
           } 
           
set 
           { 
              identity 
= (MyIdentity)value; 
           } 
        } 

        
public bool IsInRole(string role) 
        { 
           
// TODO:    添加 MyPrincipal.IsInRole 实现 
           return roleList.Contains(role); 
        } 

        
#endregion 
     }
     
public class MyIdentity : System.Security.Principal.IIdentity
     {
         
private string m_UserName;
         
private string m_Password;
         
private string m_AuthenticationType;
         
private string m_Department;
         
private string m_Role;
         
private ArrayList roleList;

         
public MyIdentity(string UserName, string Password)
         {
             
// 
             
// TODO: 在此处添加构造函数逻辑 
             
// 
             m_UserName = UserName;
             m_Password 
= Password;   
         }
       
         
//验证 是否有效用户
         private bool CanPass()
         {
             
//这里朋友们可以根据自己的需要改为从数据库中验证用户名和密码, 
             
//这里为了方便我直接指定的字符串 
             bool bPass = false;

             
using (OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Main"].ConnectionString))
             {
                 OleDbCommand cmd 
= new OleDbCommand("SELECT * FROM 用户表 WHERE ((用户名=@UserName) AND (密码=@Password))", conn);
                 cmd.Parameters.AddWithValue(
"UserName", m_UserName);
                 cmd.Parameters.AddWithValue(
"Password", m_Password);
                 
try
                 {
                     conn.Open();

                     OleDbDataReader dr 
= cmd.ExecuteReader();

                     
if (dr.Read()) 
                     {
                         roleList 
= new ArrayList();

                         m_Department 
= dr["部门"].ToString();

                         m_Role 
= dr["角色"].ToString();

                         
string[] strRole = m_Role.Split(';');

                         
foreach (string s in strRole)
                         {
                             roleList.Add(s);
                         }
                         bPass 
= true
                     }
                     
else { bPass = false; }

                     dr.Close();

                     conn.Close();
                 }
                 
catch (Exception ex)
                 {

                 }
             }
             
return bPass;
         }         
         
public string Department
         {
             
get
             {
                 
return m_Department;
             }
         }
         
public string Password
         {
             
get
             {
                 
return m_UserName;
             }
             
set
             {
                 m_UserName 
= value;
             }
         }
         
         
public bool IsAuthenticated
         {
             
get
             {
                 
// TODO:    添加 MyIdentity.IsAuthenticated getter 实现 
                 return CanPass();
             }
         }
         
public ArrayList RoleList
         {
             
get
             {
                 
return roleList;
             }
         } 

         
public string Name
         {
             
get
             {
                 
// TODO:    添加 MyIdentity.Name getter 实现 
                 return m_UserName;
             }
             
set
             {
                 m_UserName 
= value;
             }
         }

         
public string AuthenticationType
         {
             
get
             {
                 
// TODO:    添加 MyIdentity.AuthenticationType getter 实现 
                 return m_AuthenticationType;
             }
             
set
             {
                 m_AuthenticationType 
= value;
             }
         }
     } 

 

posted @ 2010-01-05 17:44  Tony(iHqq)  阅读(432)  评论(0编辑  收藏  举报