欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » ASP.Net » 正文

热门阅读

ASP.NET的HTTP模块和处理程序之模块实现(1)

创建时间:2011年06月12日 23:39  阅读次数:(4450)
分享到:

实现一个提供安全服务的HTTP模块

现在我们实现一个HTTP模块,它为我们的Web应用程序提供安全服务。该HTTP模块基本上是提供一种定制的身份认证服务。它将接收HTTP请求中的身份凭证,并确定该凭证是否有效。如果有效,与用户相关的角色是什么?通过User.Identity对象,它把这些角色与访问我们的Web应用程序页面的用户的标识关联起来。

下面是该HTTP模块的代码:

  1. using System;  
  2. using System.Web;  
  3. using System.Security.Principal;  
  4.  
  5. namespace SecurityModules  
  6. {  
  7.  /// Class1的总体描述。  
  8.  
  9.  public class CustomAuthenticationModule : IHttpModule  
  10.  {  
  11. public CustomAuthenticationModule()  
  12. {  
  13. }  
  14. public void Init(HttpApplication r_objApplication)  
  15. {  
  16.  // 向Application 对象注册事件处理程序。  
  17.  r_objApplication.AuthenticateRequest +=   
  18. new EventHandler(this.AuthenticateRequest) ;  
  19. }  
  20.  
  21. public void Dispose()  
  22. {  
  23.  // 此处空出,因为我们不需要做什么操作。  
  24. }  
  25.  
  26. private void AuthenticateRequest(object r_objSender,EventArgs r_objEventArgs)  
  27. {  
  28.  // 鉴别用户的凭证,并找出用户角色。。  
  29.  1. HttpApplication objApp = (HttpApplication) r_objSender ;  
  30.  2. HttpContext objContext = (HttpContext) objApp.Context ;  
  31.  3. if ( (objApp.Request["userid"] == null) ||  
  32.  4.  (objApp.Request["password"] == null) )  
  33.  5.  {  
  34.  6. objContext.Response.Write("<H1>Credentials not provided</H1>") ;  
  35.  7. objContext.Response.End() ;  
  36.  8.  }  
  37.  
  38.  9. string userid = "" ;  
  39.  10. userid = objApp.Request["userid"].ToString() ;  
  40.  11. string password = "" ;  
  41.  12. password = objApp.Request["password"].ToString() ;  
  42.    
  43.  13. string[] strRoles ;  
  44.  14. strRoles = AuthenticateAndGetRoles(userid, password) ;  
  45.  15. if ((strRoles == null) || (strRoles.GetLength(0) == 0))  
  46.  16. {  
  47.  17.  objContext.Response.Write("<H1>We are sorry but we could not  
  48. find this user id and password in our database</H1>") ;  
  49.  18.  objApp.CompleteRequest() ;  
  50.  19. }  
  51.  
  52.  20. GenericIdentity objIdentity = new GenericIdentity(userid,  
  53. "CustomAuthentication") ;  
  54.  21. objContext.User = new GenericPrincipal(objIdentity, strRoles) ;  
  55. }  
  56.  
  57. private string[] AuthenticateAndGetRoles(string r_strUserID,string r_strPassword)  
  58. {  
  59.  string[] strRoles = null ;  
  60.  if ((r_strUserID.Equals("Steve")) && (r_strPassword.Equals("15seconds")))  
  61.  {  
  62. strRoles = new String[1] ;  
  63. strRoles[0] = "Administrator" ;  
  64.  }  
  65.  else if ((r_strUserID.Equals("Mansoor")) && (r_strPassword.Equals("mas")))  
  66.  {  
  67. strRoles = new string[1] ;  
  68. strRoles[0] = "User" ;   
  69.  }  
  70.  return strRoles ;  
  71. }  
  72.  }  

来源:
说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论0条
  • 暂无任何评论,请留下您对本文章的看法,共同参入讨论!
发表评论:
留言人:
内  容:
请输入问题 42+73=? 的结果(结果是:115)
结  果: