Global.asax父应用程序和子应用程序

时间:2013-01-25 16:25:50

标签: c# asp.net

我正在研究一些遗留代码,我正在尝试将Global.asax文件移动到父应用程序,它将管理所有子应用程序。目前,我们在所有子应用程序中都有一个Global.asax文件(错误)。

当我尝试从子应用程序中删除Global.asax文件时,它遗憾地找不到父应用程序的Global.asax文件。因此,我无法在子应用程序中保持身份验证。我想知道是否有一个简单的解决方法。

结构:

parent app 
  files...
  childapp
    files..
    global.asax
  global.asax

我希望childapp找到父级的Global.asax文件。

谢谢你们!

编辑:

Global.asax中

(在父应用和子应用中相同)

protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        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;

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

1 个答案:

答案 0 :(得分:0)

正如其他人所说,可能有更好的方法来实现你的目标,但严格来说,如果我正确理解你的问题,它应该是可能的。

每个Global.asax继承自的类必须位于共享程序集中。然后,您可以更改每个Global.asax文件的标记以从该共享类继承。

<%@ Application Inherits="MySharedGlobal" Language="C#" %>

您可以删除每个应用程序(父级和子级)的代码,但必须保留Glabal.asax文件。

您可以将共享代码移动到基类中,就像每个应用程序对象在后面的代码中继承一样容易。这样您就可以根据需要添加特定于应用程序的逻辑。

现在,您还可以创建一个IHttpModule,将共享代码放入其中,并在父应用程序中加载时也将在子应用程序中加载(通过配置继承)。

编辑:
如果我正确理解您,您希望在每个应用程序中运行相同的代码,而无需一遍又一遍地复制和粘贴。如果您需要为每个嵌套站点提供实际的“应用程序”,那么该代码必须多次运行。但这并不意味着它无法共享。

一种简单的方法是创建实现IHttpModule的类并将其添加到父应用程序配置文件中。在'Init'方法中,您可以挂钩AuthenticateRequest事件并运行从那里发布的相同代码,并从每个子应用程序中的Global.asax代码中删除它。

实现它的另一种方法是创建一个挂接到同一事件的HttpApplication实现(就像现在一样)。然后,您可以将共享代码放在基类中,Global.asax代码后面的每个类都将继承该文件。然后,您可以从文件后面的每个Global.asax代码中删除代码。

如果您创建IHttpModule,则可以删除Global.asax文件。否则,他们需要留下来。

相关问题