• 在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;
                 }
             }
         } 
  • 相关阅读:
    值不丢失,虽然仅在局部函数中存在
    js 中和c类似
    天天QA
    request methods Hypertext Transfer Protocol (HTTP/1.1)
    单元测试
    access variables from the global scope 在全局范围内访问变量的2种方法
    summary
    安全跟效率之间的折中而已 记住一个大原则,安全和效率是对立的
    微信商城 Common Log Format Apache CustomLog
    僵尸进程 zombie
  • 原文地址:https://www.cnblogs.com/iHqq/p/1639808.html
Copyright © 2020-2023  润新知