asp.net權限控制

 有人說asp.net很簡單,其實呢這只是表面現象。要把asp.net學的很深真的是一件不容易的事情,從開發的角度來說asp.net要方便一些,只是移植性差了一些。學過java之後再來學習就會覺得很簡單,很快就可以上手。

下面就接着前面的博客寫一個權限控制和內置對象相結合的小例子,這個例子在學校裏可謂經典。所以我就借這個小例子來弄下。

1.開發一個用戶登錄表單,這裏只有登錄後的用戶才能進入下載頁面

 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="downloadfile._Default" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" > 
  6. <head runat="server"> 
  7.     <title>用戶登錄</title> 
  8. </head> 
  9. <body> 
  10.     <form id="form1" runat="server" action="ashx/ToLogin.ashx"> 
  11.      用戶名:<input type="text" name="fname"/><br /><br/> 
  12.      密碼:<input type="password" name="password"/><br /><br/> 
  13.      <input type="submit" value="登錄"/> 
  14.       <input type="reset" value="取消"/> 
  15.     </form> 
  16. </body> 
  17. </html> 

2.新建一個數據庫文件,並創建一個數據表

3.添加一個數據集,後面會使用類型化dataset來查詢數據庫

 

4.編寫一般處理程序處理用戶登錄

 

  1. using System; 
  2. using System.Collections; 
  3. using System.Data; 
  4. using System.Linq; 
  5. using System.Web; 
  6. using System.Web.Services; 
  7. using System.Web.Services.Protocols; 
  8. using System.Xml.Linq; 
  9. using System.Web.SessionState; 
  10. using downloadfile.DataSetTeacherTableAdapters; 
  11.  
  12. namespace downloadfile.ashx 
  13.     /// <summary> 
  14.     /// $codebehindclassname$ 的摘要說明 
  15.     /// </summary> 
  16.     [WebService(Namespace = "http://tempuri.org/")] 
  17.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
  18.     public class ToLogin : IHttpHandler, IRequiresSessionState 
  19.     { 
  20.  
  21.         public void Proce***equest(HttpContext context) 
  22.         { 
  23.             context.Response.ContentType = "text/plain"
  24.             string fname = context.Request["fname"]; 
  25.             string password = context.Request["password"]; 
  26.             m_teacherTableAdapter adpater = new m_teacherTableAdapter(); 
  27.            
  28.             var data = adpater.GetDataByFname(fname); 
  29.             var single = data.Single(); 
  30.           if (data.Count <= 0) 
  31.           { 
  32.               context.Response.Write("用戶名不存在"); 
  33.           } 
  34.           else { 
  35.               //判斷數據的唯一性,防止出現未知錯誤 
  36.              
  37.               //如果用戶輸入的密碼和數據庫的相同,則可以登錄成功 
  38.               if (single.fpassword.Equals(password)) 
  39.               { 
  40.                   context.Session["status"] = "login"
  41.                   context.Session["username"] = fname; 
  42.                   context.Session["password"] = password; 
  43.                   context.Session["userid"] = single.fid; 
  44.                   //context.Response.Write("登錄成功"); 
  45.                   context.Response.Redirect("../manage/allpicture.htm"); 
  46.  
  47.               }else
  48.                   context.Response.Redirect("../Default.aspx"); 
  49.               } 
  50.               
  51.           } 
  52.         } 
  53.  
  54.         public bool IsReusable 
  55.         { 
  56.             get 
  57.             { 
  58.                 return false
  59.             } 
  60.         } 
  61.     } 

5.編寫圖片的下載頁面

 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml" > 
  3. <head> 
  4.     <title>圖片下載頁面</title> 
  5. </head> 
  6. <body> 
  7.  <center> 
  8.    <div> 
  9.      <a href="../ashx/Download.ashx?filename=1.jpg">1.jpg</a> 
  10.      <a href="../ashx/Download.ashx?filename=2.jpg">2.jpg</a> 
  11.      <a href="../ashx/Download.ashx?filename=3.jpg">3.jpg</a> 
  12.      <a href="../ashx/Download.ashx?filename=4.jpg">4.jpg</a> 
  13.    </div>  
  14.  </center> 
  15.    
  16. </body> 
  17. </html> 

6.編寫下載的處理程序,如果是普通用戶則下載的圖片加水印

 

  1. using System; 
  2. using System.Collections; 
  3. using System.Data; 
  4. using System.Linq; 
  5. using System.Web; 
  6. using System.Web.Services; 
  7. using System.Web.Services.Protocols; 
  8. using System.Xml.Linq; 
  9. using downloadfile.DataSetTeacherTableAdapters; 
  10. using System.Web.SessionState; 
  11. using System.Drawing; 
  12. using System.Drawing.Imaging; 
  13.  
  14. namespace downloadfile.ashx 
  15.     /// <summary> 
  16.     /// $codebehindclassname$ 的摘要說明 
  17.     /// </summary> 
  18.     [WebService(Namespace = "http://tempuri.org/")] 
  19.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
  20.     public class Download : IHttpHandler,IRequiresSessionState   
  21.     { 
  22.  
  23.         public void Proce***equest(HttpContext context) 
  24.         { 
  25.             
  26.              
  27.            
  28.             //如果用戶已經 
  29.             string status = (string)context.Session["status"]; 
  30.            
  31.            if (status!=null
  32.             { 
  33.                
  34.  
  35.                 
  36.                //取得文件名稱 
  37.                string filename=context.Request["filename"]; 
  38.                 //設置消息頭用來下載文件 
  39.                 context.Response.ContentType="application/octet-stream"
  40.                 context.Response.AddHeader("Content-Disposition"string.Format("attachment;filename=\"{0}\"", filename)); 
  41.                 //從session取得登錄用戶的fid 
  42.                int fid=(int)context.Session["userid"]; 
  43.                 //根據用戶id查詢該用戶的級別,根據級別獲得不同的下載 
  44.                 //數據集 
  45.                
  46.                 //通過用戶的id查詢用戶的級別 
  47.                 m_teacherTableAdapter adpater = new m_teacherTableAdapter(); 
  48.                 var data = adpater.GetDataById(fid); 
  49.                 var single = data.Single(); 
  50.                 if (single.level == 1) 
  51.                 { 
  52.                     //context.Response.WriteFile("../image/" + filename); 
  53.                     using (Bitmap bitmap = new Bitmap(context.Server.MapPath("../image/" + filename))) 
  54.                     { 
  55.                         using (Graphics gra = Graphics.FromImage(bitmap)) 
  56.                         { 
  57.                             gra.DrawString("免費下載"new Font("宋體", 20), Brushes.Red, 0, 0); 
  58.                             
  59.                         } 
  60.                         bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg); 
  61.  
  62.                     }  
  63.                    
  64.                 } 
  65.                 else { 
  66.                     context.Response.WriteFile("../image/" + filename); 
  67.                 } 
  68.  
  69.  
  70.             } 
  71.             else 
  72.             { 
  73.                 context.Response.Write(status); 
  74.                 //context.Response.Write("請先登錄"); 
  75.             } 
  76.            
  77.         } 
  78.  
  79.         public bool IsReusable 
  80.         { 
  81.             get 
  82.             { 
  83.                 return false
  84.             } 
  85.         } 
  86.     } 

7.程序的運行效果

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章