我有一个父应用程序和一个子应用程序。目前有两个单独的 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
{
}
}
代码隐藏文件中的两个类都继承自 App_Code 文件夹中的源代码,但是,身份验证状态不会从一个应用程序传递到另一个应用程序。例如,如果我登录父应用程序,则身份验证不会转移到子应用程序。反之亦然。
我希望我给了你们足够的细节。
谢谢!
编辑:
Heinzi 在评论中表示,这不是继承问题。我需要弄清楚如何让子应用程序使用父级的 Global.asax 文件。如果我删除子应用程序的 Global.asax 文件身份验证对子应用程序根本不起作用。有任何想法吗?