我有一个父应用程序和一个子应用程序。其中目前有两个独立的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文件,身份验证对于子应用程序完全不起作用。有任何想法吗?
为什么应该从同一个源继承导致共享身份验证状态?如果'B1'和'B2'都从'A'继承,这*表示'B1'和'B2'可以在需要'A'的地方使用。作为一种奖励,为了节省一些打字费用,“B1”和“B2”的行为就好像写在“A”上的代码已被复制并粘贴到“B1”和“B2”中(有一些例外和差异,但这是总体思路)。继承与任何类型的共享状态(如登录凭证)都无关。 – Heinzi
是的,你是对的。我将如何去消除子应用程序的Global.asax文件并让它使用父母的Global.asax呢? –
我不认为这与“谁的全球化”有关。使用asax文件“,这只是两个应用程序之间共享凭据的问题。搜索[aspx共享身份验证]时有相当多的Google点击(http://www.google.com/search?q=aspx+shared +身份验证),但我不太了解如何推荐一个特定的解决方案,我会建议您查看一下,如果您仍然需要帮助,请在此提出一个新问题。 – Heinzi