当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  在ASP.NET中使用Global.asax文件(2)

 在ASP.NET中使用Global.asax文件(2)

点击次数:26 次 发布日期:2008-11-26 12:28:02 作者:源代码网
源代码网推荐      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.");
源代码网推荐  }
源代码网推荐  
源代码网推荐    这个例子只是很简单地使用了一些Global.asax 文件中的事件;重要的是要意识到这些事件是与整个应用程序相关的。这样,所有放在其中的方法都会通过应用程序的代码被提供,这就是它的名字为Global 的原因。
源代码网推荐  
源代码网推荐    这里是前面的例子相应的 VB.NET 代码:
源代码网推荐  
源代码网推荐  Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
源代码网推荐   Application("Title") = "Builder.com Sample"
源代码网推荐  End Sub
源代码网推荐  Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
源代码网推荐   Session("startValue") = 0
源代码网推荐  End Sub
源代码网推荐  Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As
源代码网推荐  EventArgs)
源代码网推荐   ’ Extract the forms authentication cookie
源代码网推荐   Dim cookieName As String
源代码网推荐   cookieName = FormsAuthentication.FormsCookieName
源代码网推荐   Dim authCookie As HttpCookie
源代码网推荐   authCookie = Context.Request.Cookies(cookieName)
源代码网推荐   If (authCookie Is Nothing) Then
源代码网推荐    ’ There is no authentication cookie.
源代码网推荐    Return
源代码网推荐   End If
源代码网推荐   Dim authTicket As FormsAuthenticationTicket
源代码网推荐   authTicket = Nothing
源代码网推荐   Try
源代码网推荐    authTicket = FormsAuthentication.Decrypt(authCookie.Value)
源代码网推荐    Catch ex As Exception
源代码网推荐    ’ Log exception details (omitted for simplicity)
源代码网推荐    Return
源代码网推荐   End Try
源代码网推荐   Dim roles(2) As String
源代码网推荐   roles(0) = "One"
源代码网推荐   roles(1) = "Two"
源代码网推荐   Dim id As FormsIdentity
源代码网推荐   id = New FormsIdentity(authTicket)
源代码网推荐   Dim principal As GenericPrincipal
源代码网推荐   principal = New GenericPrincipal(id, roles)
源代码网推荐   ’ Attach the new principal object to the current HttpContext object
源代码网推荐   Context.User = principal
源代码网推荐  End Sub
源代码网推荐  
源代码网推荐  Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
源代码网推荐   Response.Write("Error encountered.")
源代码网推荐  End Sub
源代码网推荐  
源代码网推荐    资源
源代码网推荐  
源代码网推荐    Global.asax 文件是 ASP.NET 应用程序的中心点。它提供无数的事件来处理不同的应用程序级任务,比如用户身份验证、应用程序启动以及处理用户会话等。你应该熟悉这个可选文件,这样就可以构建出健壮的ASP.NET 应用程序。
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


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