Global.asax应用程序文件

分类: asp.net   出处:iocblog整理  更新时间:2009-12-26   添加到收藏  

     global.asax 文件,有时候叫做 asp.net 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法。你可以使用这个文件实现应用程序安全性以及其它一些任务。
   global.asax 文件被配置为任何(通过 url 的)直接 http 请求都被自动拒绝,所以用户不能下载或查看其内容。asp.net 页面框架能够自动识别出对global.asax 文件所做的任何更改。在 global.asax 被更改后asp.net 页面框架会重新启动应用程序,包括关闭所有的浏览器会话,去除所有状态信息,并重新启动应用程序域。
  
  
  global.asax 文件继承自httpapplication 类,它维护一个httpapplication 对象池,并在需要时将对象池中的对象分配给应用程序。global.asax 文件包含以下事件:
  
  ・ application_init:在应用程序被实例化或第一次被调用时,该事件被触发。对于所有的httpapplication 对象实例,它都会被调用。
  
  ・ application_disposed:在应用程序被销毁之前触发。这是清除以前所用资源的理想位置。 (文章来源 www.iocblog.net)
  
  ・ application_error:当应用程序中遇到一个未处理的异常时,该事件被触发。
  
  ・ application_start:在httpapplication 类的第一个实例被创建时,该事件被触发。它允许你创建可以由所有httpapplication 实例访问的对象。
  
  ・ application_end:在httpapplication 类的最后一个实例被销毁时,该事件被触发。在一个应用程序的生命周期内它只被触发一次。
  
  ・ application_beginrequest:在接收到一个应用程序请求时触发。对于一个请求来说,它是第一个被触发的事件,请求一般是用户输入的一个页面请求(url)。
  
  ・ application_endrequest:针对应用程序请求的最后一个事件。
  
  ・ application_prerequesthandlerexecute:在 asp.net 页面框架开始执行诸如页面或 web 服务之类的事件处理程序之前,该事件被触发。
  
  ・ application_postrequesthandlerexecute:在 asp.net 页面框架结束执行一个事件处理程序时,该事件被触发。
  
  ・ applcation_presendrequestheaders:在 asp.net 页面框架发送 http 头给请求客户(浏览器)时,该事件被触发。
  
  ・ application_presendcontent:在 asp.net 页面框架发送内容给请求客户(浏览器)时,该事件被触发。
  
  ・ application_acquirerequeststate:在 asp.net 页面框架得到与当前请求相关的当前状态(session 状态)时,该事件被触发。
  
  ・ application_releaserequeststate:在 asp.net 页面框架执行完所有的事件处理程序时,该事件被触发。这将导致所有的状态模块保存它们当前的状态数据。
  
  ・ application_resolverequestcache:在 asp.net 页面框架完成一个授权请求时,该事件被触发。它允许缓存模块从缓存中为请求提供服务,从而绕过事件处理程序的执行。
  
  ・ application_updaterequestcache:在 asp.net 页面框架完成事件处理程序的执行时,该事件被触发,从而使缓存模块存储响应数据,以供响应后续的请求时使用。
  
  ・ application_authenticaterequest:在安全模块建立起当前用户的有效的身份时,该事件被触发。在这个时候,用户的凭据将会被验证。
  
  ・ application_authorizerequest:当安全模块确认一个用户可以访问资源之后,该事件被触发。
  
  ・ session_start:在一个新用户访问应用程序 web 站点时,该事件被触发。
  
  ・ session_end:在一个用户的会话超时、结束或他们离开应用程序 web 站点时,该事件被触发。
  
  
  
  
  
  使用这些事件的一个关键问题是知道它们被触发的顺序。application_init 和application_start 事件在应用程序第一次启动时被触发一次。相似地,application_disposed 和 application_end 事件在应用程序终止时被触发一次。此外,基于会话的事件(session_start 和 session_end)只在用户进入和离开站点时被使用。其余的事件则处理应用程序请求,这些事件被触发的顺序是:
  
  ・ application_beginrequest
  
  ・ application_authenticaterequest
  
  ・ application_authorizerequest
  
  ・ application_resolverequestcache
  
  ・ application_acquirerequeststate
  
  ・ application_prerequesthandlerexecute
  
  ・ application_presendrequestheaders
  
  ・ application_presendrequestcontent
  
  ・ <<执行代码>>
  
  ・ application_postrequesthandlerexecute
  
  ・ application_releaserequeststate
  
  ・ application_updaterequestcache
  
  ・ application_endrequest
  
  这些事件常被用于安全性方面。下面这个 c# 的例子演示了不同的global.asax 事件,该例使用application_authenticate 事件来完成通过 cookie 的基于表单(form)的身份验证。此外,application_start 事件填充一个应用程序变量,而session_start 填充一个会话变量。application_error 事件显示一个简单的消息用以说明发生的错误。
  
  protected void application_start(object sender, eventargs e) {
  application["title"] = "builder.com sample";
  }
  protected void session_start(object sender, eventargs e) {
  session["startvalue"] = 0;
  }
  protected void application_authenticaterequest(object sender, eventargs e) {
  // extract the forms authentication cookie
  string cookiename = formsauthentication.formscookiename;
  httpcookie authcookie = context.request.cookies[cookiename];
  if(null == authcookie) {
  // there is no authentication cookie.
  return;
  }
  formsauthenticationticket authticket = null;
  try {
  authticket = formsauthentication.decrypt(authcookie.value);
  } catch(exception ex) {
  // log exception details (omitted for simplicity)
  return;
  }
  if (null == authticket) {
  // cookie failed to decrypt.
  return;
  }
  // when the ticket was created, the userdata property was assigned
  // a pipe delimited string of role names.
  string[2] roles
  roles[0] = "one"
  roles[1] = "two"
  // create an identity object
  formsidentity id = new formsidentity( authticket );
  // this principal will flow throughout the request.
  genericprincipal principal = new genericprincipal(id, roles);
  // attach the new principal object to the current httpcontext object
  context.user = principal;
  }
  protected void application_error(object sender, eventargs e) {
  response.write("error encountered.");
  }
 (文章来源 www.iocblog.net)



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。