当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  ASP.NET中的HTTP模块和处理程序 4

 ASP.NET中的HTTP模块和处理程序 4

点击次数:28 次 发布日期:2008-11-26 11:51:26 作者:源代码网
源代码网推荐      除了这些事件之外,我们还可以使用四个事件。我们可以通过实现Web应用程序的global.asax文件中一些方法来使用这些事件。
源代码网推荐  
源代码网推荐    这些事件是:
源代码网推荐  
源代码网推荐    · Application_OnStart
源代码网推荐  
源代码网推荐    当第一个请求到达Web应用程序的时候引发这个事件。
源代码网推荐  
源代码网推荐    · Application_OnEnd
源代码网推荐  
源代码网推荐    准备终止应用程序之前引发这个事件。
源代码网推荐  
源代码网推荐    · Session_OnStart
源代码网推荐  
源代码网推荐    用户对话的第一个请求引发这个事件。
源代码网推荐  
源代码网推荐    · Session_OnEnd
源代码网推荐  
源代码网推荐    放弃对话或者对话超期的时候引发这个事件。
源代码网推荐    在配置文件中注册HTTP模块
源代码网推荐  
源代码网推荐    当我们建立了HTTP模块并把它复制到Web应用程序的bin目录或者全局部件缓冲(Global Assembly Cache)之后,接下来就应该在web.config或machine.config中注册它了。
源代码网推荐  
源代码网推荐    我们可以使用<httpModules>和<add>节点把HTTP模块添加到Web应用程序中。实际上模块都使用<add>节点列举在<httpModules>和</httpModules>节点之内了。
源代码网推荐  
源代码网推荐    因为配置设置信息是可以继承的,所以子目录从父目录那儿继承配置设置信息。其结果是,子目录可能继承了一些不需要的HTTP模块(它们是父配置信息的一部分);因此,我们需要一种删除这些不需要的模块的方法。我们可以使用<remove>节点;如果我们希望删除从应用程序继承得到的所有HTTP模块,可以使用<clear>节点。
源代码网推荐  
源代码网推荐    下面的代码是添加HTTP模块的一个通用示例:
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  <httpModules>
源代码网推荐  <add type="classname, assemblyname" name="modulename" />
源代码网推荐  <httpModules>
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐    下面的代码是从应用程序中删除HTTP模块的一个通用示例:
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  <httpModules>
源代码网推荐  <remove name="modulename" />
源代码网推荐  <httpModules>
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐    在上面的XML中:
源代码网推荐  
源代码网推荐    · Type属性用类和部件名称的形式指定了HTTP模块的实际类型。
源代码网推荐  
源代码网推荐    · Name属性指定了模块的友好名称。其它应用程序可以使用这个名称来识别HTTP模块。
源代码网推荐  
源代码网推荐    ASP.NET运行时如何使用HTTP模块
源代码网推荐  
源代码网推荐    ASP.NET运行时使用HTTP模块实现某些特殊的功能。下面的片段来自于machine.config文件,它显示了ASP.NET运行时安装的HTTP模块:
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  <httpModules>
源代码网推荐   <add name="OutputCache" type="System.Web.Caching.OutputCacheModule"/>
源代码网推荐   <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
源代码网推荐   <add name="WindowsAuthentication"
源代码网推荐  type="System.Web.Security.WindowsAuthenticationModule"/>
源代码网推荐   <add name="FormsAuthentication"
源代码网推荐  type="System.Web.Security.FormsAuthenticationModule"/>
源代码网推荐   <add name="PassportAuthentication"
源代码网推荐  type="System.Web.Security.PassportAuthenticationModule"/>
源代码网推荐   <add name="UrlAuthorization"
源代码网推荐  type="System.Web.Security.UrlAuthorizationModule"/>
源代码网推荐   <add name="FileAuthorization"
源代码网推荐  type="System.Web.Security.FileAuthorizationModule"/>
源代码网推荐  </httpModules>
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐    ASP.NET使用上面一些HTTP模块来提供一些服务,例如身份验证和授权、对话管理和输出缓冲。由于这些模块都注册在machine.config文件中。
源代码网推荐  
源代码网推荐    实现一个提供安全服务的HTTP模块
源代码网推荐  
源代码网推荐    现在我们实现一个HTTP模块,它为我们的Web应用程序提供安全服务。该HTTP模块基本上是提供一种定制的身份认证服务。它将接收HTTP请求中的身份凭证,并确定该凭证是否有效。如果有效,与用户相关的角色是什么?通过User.Identity对象,它把这些角色与访问我们的Web应用程序页面的用户的标识关联起来。
源代码网推荐  下面是该HTTP模块的代码:
源代码网推荐  
源代码网推荐  using System;
源代码网推荐  using System.Web;
源代码网推荐  using System.Security.Principal;
源代码网推荐  
源代码网推荐  namespace SecurityModules
源代码网推荐  {
源代码网推荐   /// Class1的总体描述。
源代码网推荐  
源代码网推荐   public class CustomAuthenticationModule : IHttpModule
源代码网推荐   {
源代码网推荐    public CustomAuthenticationModule()
源代码网推荐    {
源代码网推荐    }
源代码网推荐    public void Init(HttpApplication r_objApplication)
源代码网推荐    {
源代码网推荐     // 向Application 对象注册事件处理程序。
源代码网推荐     r_objApplication.AuthenticateRequest +=
源代码网推荐  new EventHandler(this.AuthenticateRequest) ;
源代码网推荐    }
源代码网推荐  
源代码网推荐    public void Dispose()
源代码网推荐    {
源代码网推荐     // 此处空出,因为我们不需要做什么操作。
源代码网推荐    }
源代码网推荐  
源代码网推荐    private void AuthenticateRequest(object r_objSender,EventArgs r_objEventArgs)
源代码网推荐    {
源代码网推荐     // 鉴别用户的凭证,并找出用户角色。。
源代码网推荐     1. HttpApplication objApp = (HttpApplication) r_objSender ;
源代码网推荐     2. HttpContext objContext = (HttpContext) objApp.Context ;
源代码网推荐     3. if ( (objApp.Request["userid"] == null) ||
源代码网推荐     4.  (objApp.Request["password"] == null) )
源代码网推荐     5.  {
源代码网推荐     6.   objContext.Response.Write("<H1>Credentials not provided</H1>") ;
源代码网推荐     7.   objContext.Response.End() ;
源代码网推荐     8.  }
源代码网推荐  
源代码网推荐     9. string userid = "" ;
源代码网推荐     10. userid = objApp.Request["userid"].ToString() ;
源代码网推荐     11. string password = "" ;
源代码网推荐     12. password = objApp.Request["password"].ToString() ;
源代码网推荐   
源代码网推荐     13. string[] strRoles ;
源代码网推荐     14. strRoles = AuthenticateAndGetRoles(userid, password) ;
源代码网推荐     15. if ((strRoles == null) || (strRoles.GetLength(0) == 0))
源代码网推荐     16. {
源代码网推荐     17.  objContext.Response.Write("<H1>We are sorry but we could not
源代码网推荐  find this user id and password in our database</H1>") ;
源代码网推荐     18.  objApp.CompleteRequest() ;
源代码网推荐     19. }
源代码网推荐  
源代码网推荐     20. GenericIdentity objIdentity = new GenericIdentity(userid,
源代码网推荐  "CustomAuthentication") ;
源代码网推荐     21. objContext.User = new GenericPrincipal(objIdentity, strRoles) ;
源代码网推荐    }
源代码网推荐  
源代码网推荐    private string[] AuthenticateAndGetRoles(string r_strUserID,string r_strPassword)
源代码网推荐    {
源代码网推荐     string[] strRoles = null ;
源代码网推荐     if ((r_strUserID.Equals("Steve")) && (r_strPassword.Equals("15seconds")))
源代码网推荐     {
源代码网推荐      strRoles = new String[1] ;
源代码网推荐      strRoles[0] = "Administrator" ;
源代码网推荐     }
源代码网推荐     else if ((r_strUserID.Equals("Mansoor")) && (r_strPassword.Equals("mas")))
源代码网推荐     {
源代码网推荐      strRoles = new string[1] ;
源代码网推荐      strRoles[0] = "User" ;
源代码网推荐     }
源代码网推荐     return strRoles ;
源代码网推荐    }
源代码网推荐   }
源代码网推荐  }
源代码网推荐  
源代码网推荐    我们研究一下上面的代码。
源代码网推荐  
源代码网推荐    我们是从Init函数开始的。这个函数把处理程序的AuthenticateRequest事件插入Application(应用程序)对象的事件处理程序列表中。这将导致引发AuthenticationRequest事件的时候Application调用该方法。
源代码网推荐  
源代码网推荐    我们的HTTP模块初始化之后,我们就可以调用它的AuthenticateRequest方法来鉴别客户端请求。AuthenticateRequest方法是该安全/身份认证机制的核心。在这个函数中:
源代码网推荐  
源代码网推荐    1和2行提取HttpApplication和HttpContext对象。3到7行检测是否没有给我们提供了用户id或密码。如果没有提供,就显示错误信息,请求处理过程终止。
源代码网推荐  
源代码网推荐    9到12行从HttpRequest对象中提取用户id和密码。
源代码网推荐  
源代码网推荐    14行调用一个叫做AuthenticateAndGetRoles的辅助(helper)函数。这个函数主要执行身份验证并决定用户角色。上面的代码采用了硬编码(hard-coded),只允许两个用户使用,但是我们可以扩展这个方法,并添加代码与用户数据库交互操作并检索用户的角色。
源代码网推荐  
源代码网推荐    16到19行检测是否有角色与用户关联。如果没有就意味着传递给我们的凭证没有通过验证;因此该凭证是无效的。因此,给客户端发送一个错误信息,并且请求结束了。
源代码网推荐  
源代码网推荐    20和21行非常重要,因为这两行实际上告诉ASP.NET HTTP运行时已登录用户的身份。这两行成功执行以后,我们的aspx页面就能够使用User对象访问这些信息了。
源代码网推荐  
源代码网推荐    现在我们看一看这种身份验证机制的运行情况。目前我们只允许下面两个用户登录到系统:
源代码网推荐  
源代码网推荐    · User id = Steve, Password = 15seconds, Role = Administrator
源代码网推荐    · User id = Mansoor, Password = mas, Role = User
源代码网推荐  
源代码网推荐    注意用户id和密码是大小写敏感的(区分大小写)。
源代码网推荐  
源代码网推荐    首先试图不提供凭证登录系统,在IE中输入http://localhost/webapp2/index.aspx将看到下面的消息:
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华