国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

基于.net的用戶處理機制。

2024-07-10 12:56:09
字體:
來源:轉載
供稿:網友
  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。
  • 基于.net的principal、identity接口的用戶處理機制。
    siteidentity.cs
    using system;
    using system.text;
    using system.collections;
    using system.security.cryptography;
    namespace contextuser
    {
     /// <summary>
     /// siteidentity 的摘要說明。
     /// </summary>
     public class siteidentity: system.security.principal.iidentity
     {
      private string username;
      private string emailaddress;
      private int userid; 

      public siteidentity(string currentusername )
      {
       username = currentusername;
       emailaddress = "[email protected]";
       userid =1;
      }
      public string authenticationtype
      {
       get
       {
        return "custom authentication";
       }
       set 
       {
        // do nothing
       }
      }
      public bool isauthenticated
      {
       get 
       {
        // assumption: all instances of a siteidentity have already
        // been authenticated.
        return true;
       }
      }
      public string name
      {
       get 
       {
        return username;
       }
      }
      public string emailaddress
      {
       get 
       {
        return emailaddress;
       }
      }
      public int userid
      {
       get 
       {
        return userid;
       }
      }
     }
    }
    -----------------------------------------------------------------------------------------------
    siteprincipal.cs
    using system;
    using system.collections;
    using system.security;
    using system.security.cryptography;
    namespace contextuser
    {
     /// <summary>
     /// siteprincipal 的摘要說明。
     /// </summary>
     public class siteprincipal:  system.security.principal.iprincipal
     {
      protected system.security.principal.iidentity identity;
      protected arraylist permissionlist;//許可動態數組
      protected arraylist rolelist;//角色動態數組
      public system.security.principal.iidentity identity
      {
       get 
       {
        return identity;
       }
       set 
       {
        identity = value;
       }
      }
      public siteprincipal( string username )//構造函數邏輯
      {  
       
       identity = new siteidentity( username );
       rolelist = new arraylist();//添加角色數組 以后可以用從數據庫獲取數值代替
       rolelist.add("admin");
       rolelist.add("123");
       permissionlist = new arraylist();
       permissionlist.add(2);
       //permissionlist = datauser.geteffectivepermissionlist( userid );
       //rolelist = datauser.getuserroles( userid );
      }
      public static siteprincipal validatelogin(string username, string password)//驗證登陸
      {
       if(username == "1111111" && password == "1111111")//可以用從數據庫驗證替代
       {
        return new siteprincipal( username );
       }
       else
       {
        return null;
       }
          
      } 
      public bool isinrole(string role)
      {
       return rolelist.contains( role );
      }
      public bool haspermission( int permissionid )
      {
       return permissionlist.contains( permissionid );
      }
      public arraylist roles
      {
       get 
       {
        return rolelist;
       }
      }
      public arraylist permissions
      {
       get 
       {
        return permissionlist;
       }
      }
     }
    }

    下面繼承page 用于需要驗證的頁面繼承
    basepage.cs
    using system;
    using system.web;
    using system.web.ui;
    using system.diagnostics;
    namespace contextuser
    {
     /// <summary>
     /// basepage 的摘要說明。
     /// </summary>
     public class basepage : system.web.ui.page
     {
      public basepage()
      {
      }
      protected override void oninit(eventargs e)
      {
       base.oninit(e);
       this.load += new system.eventhandler(this.basepage_load);
      }
      private void basepage_load(object sender, system.eventargs e)
      {
              
       if (context.user.identity.isauthenticated) 
       {
        if (!(context.user is siteprincipal))
        {
         response.write("context.user.identity.is  authenticated");
         siteprincipal newuser = new siteprincipal( context.user.identity.name);
         context.user = newuser;
         response.write(context.user.isinrole("123"));
        }
     
       }
      }
     

     }
    }
    --------------------------------------------------------------------------
    登陸頁面
    using system;
    using system.collections;
    using system.componentmodel;
    using system.data;
    using system.drawing;
    using system.web;
    using system.web.security;
    using system.web.sessionstate;
    using system.web.ui;
    using system.web.ui.webcontrols;
    using system.web.ui.htmlcontrols;
    namespace contextuser
    {
     /// <summary>
     /// webform1 的摘要說明。
     /// </summary>
     public class webform1 : contextuser.basepage
     {
      protected system.web.ui.webcontrols.textbox username;
      protected system.web.ui.webcontrols.textbox pwd;
      protected system.web.ui.webcontrols.label label1;
      protected system.web.ui.webcontrols.label label2;
      protected system.web.ui.webcontrols.button button1;
     
      private void page_load(object sender, system.eventargs e)
      {
       // 在此處放置用戶代碼以初始化頁面
      }
      #region web 窗體設計器生成的代碼
      override protected void oninit(eventargs e)
      {
       //
       // codegen: 該調用是 asp.net web 窗體設計器所必需的。
       //
       initializecomponent();
       base.oninit(e);
      }
      
      /// <summary>
      /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
      /// 此方法的內容。
      /// </summary>
      private void initializecomponent()
      {    
       this.button1.click += new system.eventhandler(this.button1_click);
       this.load += new system.eventhandler(this.page_load);
      }
      #endregion
      private void button1_click(object sender, system.eventargs e)
      {
       siteprincipal principal = siteprincipal.validatelogin(username.text,pwd.text);
       if(principal==null)
       {
        label1.text = "用戶名或密碼不正確";

       }
       else
       {
        context.user = principal;
        formsauthentication.setauthcookie( username.text, true );    
        if(context.user.isinrole("123"))
        {
         label2.text = "用戶" + context.user.identity.name + "屬于admin組";
        }
        else
        {
         label2.text = "用戶" + context.user.identity.name + "不屬于admin組";
        }
       }
      }
     }
    }
    ---------------------------------------------------
    驗證頁面 需要繼承basepage
    using system;
    using system.collections;
    using system.componentmodel;
    using system.data;
    using system.drawing;
    using system.web;
    using system.web.sessionstate;
    using system.web.ui;
    using system.web.ui.webcontrols;
    using system.web.ui.htmlcontrols;
    namespace contextuser
    {
     /// <summary>
     /// _default 的摘要說明。
     /// </summary>
     public class _default : contextuser.basepage
     {
      protected system.web.ui.webcontrols.label label1;
     
      private void page_load(object sender, system.eventargs e)
      {
       if (!context.user.identity.isauthenticated || 
        !((siteprincipal)context.user).haspermission(2))
       {
        page.response.redirect("webform1.aspx");
      
       }
       else
       {
        page.response.write("通過");
       }
       
     
      }
      #region web 窗體設計器生成的代碼
      override protected void oninit(eventargs e)
      {
       //
       // codegen: 該調用是 asp.net web 窗體設計器所必需的。
       //
       base.oninit(e);//------------------------------------------------------------注意這里需要放在上邊
       initializecomponent();
       
      }
      
      /// <summary>
      /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
      /// 此方法的內容。
      /// </summary>
      private void initializecomponent()
      {    
       this.load += new system.eventhandler(this.page_load);
      }
      #endregion
     }
    }

    參考了一些網友的文章還有wrox站點高級編程 在此感謝!
    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 阿城市| 周口市| 苍南县| 始兴县| 岳阳县| 八宿县| 霍邱县| 南昌市| 同江市| 阿拉善盟| 石泉县| 冷水江市| 荣成市| 林西县| 宜君县| 濮阳县| 鞍山市| 无锡市| 武威市| 长寿区| 土默特右旗| 佳木斯市| 遂昌县| 湘乡市| 新源县| 奉节县| 策勒县| 舒兰市| 曲阜市| 兴宁市| 西和县| 淮北市| 陈巴尔虎旗| 潜江市| 郎溪县| 泾源县| 富平县| 饶河县| 余姚市| 丰台区| 汕尾市|