基於角色的身分識別驗證2

來源:互聯網
上載者:User
前邊一個有把在 global 中通過使用者名稱到資料庫去驗證,這個中是直接在使用者登陸時把使用者角色存在 cookie 中,在 global 中去進行驗證,可能比前一個要效率高吧....

 
web.config
 

<authentication mode="Forms"> 
     <forms name=".test" loginUrl="bb.aspx" timeout="30" path="/"></forms>

      </authentication>
    <authorization>
      
            
            <deny users="?"></deny>
    </authorization>

User.xml
 

<?xml version="1.0" encoding="utf-8" ?> 
<roles>
  <user
    name="Bob"
    roles="Sales" />
  <user
    name="Jane"
    roles="Supervisor,Sales" />
</roles>

  bb.aspx
 private void Button1_Click(object sender, System.EventArgs e)
        {   

            if (confirm(this.TextBox1 .Text ,this.TextBox2.Text))
            {
                 string Roles=this.get_Role (this.TextBox1.Text );
                 

                FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1,this.TextBox1 .Text ,DateTime.Now, DateTime.Now.AddMinutes(30), false,Roles,"/") ; //建立身分識別驗證票對象
               string HashTicket = FormsAuthentication.Encrypt (Ticket) ; //加密序列化票為字串

            HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName,HashTicket) ;
            //產生Cookie
            Context.Response.Cookies.Add (UserCookie) ; //輸出Cookie
            Context.Response.Redirect (Context.Request["ReturnUrl"]) ; // 重定戶申請的初始頁面
      
}
                     
            
        } 

        public bool confirm(string name,string pass)
        {
            DataSet ds;
            ds=new DataSet ();
            ds.ReadXml (MapPath("User.xml"));
            DataTable dt=ds.Tables[0];
              
            DataRow[] dr=dt.Select("name='"+name+"'");

            if (dr.Length >0)
                return true;
                else
                return false;
              
             
        } 

        public  string  get_Role(string name)
        {
             DataSet ds;
             ds=new DataSet ();
             ds.ReadXml (MapPath("User.xml"));
              DataTable dt=ds.Tables[0];
              
                DataRow[] dr=dt.Select("name='"+name+"'");
              
            
             return dr[0][1].ToString ();
               
              
              
             
        }

Global.asax

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
           System.Web .HttpApplication  app=((HttpApplication)sender);
           System.Web.HttpContext  ctx=app.Context;
            if (ctx.Request .IsAuthenticated)
            {
                System.Web .Security.FormsIdentity id=((FormsIdentity)ctx.User .Identity) ;
                System.Web .Security.FormsAuthenticationTicket  ticket=id.Ticket ;
                 string [] Role=ticket.UserData .Split(',');
                 ctx.User =new System.Security.Principal.GenericPrincipal(id ,Role);

             }


        }

default.aspx

 

private void Page_Load(object sender, System.EventArgs e)
        {
            
            if (User.IsInRole("Sales"))
            {
                this.Response .Write ("Seles");
                 
             }

            if (User.IsInRole("Supervisor"))
            {
                this.Response .Write ("Supervisor");
                 
            }



            
                }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.