2013-01-31 91 views
0

我有一個父應用程序和一個子應用程序。其中目前有兩個獨立的Global.asax文件。我試圖讓這個子應用程序繼承父應用程序的Global.asax文件。Global.asax和繼承

所以我在App_Code文件夾一個文件都在它的代碼如下:

namespace NsGlobalAsax 
{ 
    public class GlobalAsax : System.Web.HttpApplication 
    { 
     public GlobalAsax() 
     { 
      // 
      // TODO: Add constructor logic here 
      // 
     } 



     void Session_Start(object sender, EventArgs e) 
     { 
      // add some data to the Session so permanent SessionId is assigned 
      // otherwise new SessionId is assigned to the user until some data 
      // is actually written to Session 
      Session["Start"] = DateTime.Now; 

      // get current context 
      HttpContext currentContext = HttpContext.Current; 

      if (currentContext != null) 
      { 
       if (!OnlineVisitorsUtility.Visitors.ContainsKey(currentContext.Session.SessionID)) 
        OnlineVisitorsUtility.Visitors.Add(currentContext.Session.SessionID, new WebsiteVisitor(currentContext)); 
      } 

     } 

     void Session_End(object sender, EventArgs e) 
     { 
      // Code that runs when a session ends. 
      // Note: The Session_End event is raised only when the sessionstate mode 
      // is set to InProc in the Web.config file. If session mode is set to StateServer 
      // or SQLServer, the event is not raised. 

      if (this.Session != null) 
       OnlineVisitorsUtility.Visitors.Remove(this.Session.SessionID); 
     } 



     public void Application_AuthenticateRequest(Object sender, EventArgs e) 
     { 
      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) 
      { 
       //Write the exception to the Event Log. 
       return; 
      } 

      if (null == authTicket) 
      {//Cookie failed to decrypt. 
       return; 
      } 

      //When the ticket was created, the UserData property was assigned a 
      //pipe-delimited string of group names. 
      String[] groups = authTicket.UserData.Split(new char[] { '|' }); 

      //Create an Identity. 
      GenericIdentity id = new GenericIdentity(authTicket.Name, "LdapAuthentication"); 

      //This principal flows throughout the request. 
      GenericPrincipal principal = new GenericPrincipal(id, groups); 

      Context.User = principal; 

     } 
    } 

} 

現在我有我的父母Global.asax文件如下:

<%@ Application Language="C#" CodeBehind="Global.asax.cs" src="Global.asax.cs" Inherits="RootAsax.BaseGlobal" %> 

和這裏是代碼隱藏文件:

namespace RootAsax 
{ 
    public class BaseGlobal : NsGlobalAsax.GlobalAsax 
    {} 
} 

現在這裏是我的孩子的應用程序Global.asax文件:

<%@ Application Codebehind="Global.asax.cs" Inherits="FormsAuthAd.Global" Language="C#" %> 

,這裏是代碼隱藏文件:

namespace FormsAuthAd 
{ 
    public class Global : NsGlobalAsax.GlobalAsax 
    { 
    } 
} 

在codebehindfiles兩個類都在App_Code文件夾中的源繼承,然而,認證狀態不是從一個應用程序傳遞到另一個。例如,如果我在父應用程序上登錄,則身份驗證不會傳遞給子應用程序。相反的情況也是如此。

我希望我給你們足夠的細節。

謝謝!

編輯:

Heinzi在評論中指出,這不是一個繼承問題。我需要弄清楚如何讓子應用程序使用父母的Global.asax文件。如果我刪除子應用程序的Global.asax文件,身份驗證對於子應用程序完全不起作用。有任何想法嗎?

+0

爲什麼應該從同一個源繼承導致共享身份驗證狀態?如果'B1'和'B2'都從'A'繼承,這*表示'B1'和'B2'可以在需要'A'的地方使用。作爲一種獎勵,爲了節省一些打字費用,「B1」和「B2」的行爲就好像寫在「A」上的代碼已被複制並粘貼到「B1」和「B2」中(有一些例外和差異,但這是總體思路)。繼承與任何類型的共享狀態(如登錄憑證)都無關。 – Heinzi

+0

是的,你是對的。我將如何去消除子應用程序的Global.asax文件並讓它使用父母的Global.asax呢? –

+0

我不認爲這與「誰的全球化」有關。使用asax文件「,這只是兩個應用程序之間共享憑據的問題。搜索[aspx共享身份驗證]時有相當多的Google點擊(http://www.google.com/search?q=aspx+shared +身份驗證),但我不太瞭解如何推薦一個特定的解決方案,我會建議您查看一下,如果您仍然需要幫助,請在此提出一個新問題。 – Heinzi

回答

0

我認爲每個應用程序都有自己的會話和狀態..它是不可能通過 應用程序對象。其中一種方式是通過應用程序 將數據保存在數據庫中,而另一應用程序讀取來自公共共享數據庫的數據並確定如何使其具有意義。