[英]Global.asax and inheritance
我有一個父應用程序和一個子應用程序。 當前有兩個單獨的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文件夾中的源繼承,但是,身份驗證狀態沒有從一個應用程序傳遞到另一個應用程序。 例如,如果我登錄父應用程序,則身份驗證不會延續到子應用程序。 反之亦然。
我希望我能給你們足夠的細節。
謝謝!
編輯:
海因茲在評論中指出,這不是繼承問題。 我需要弄清楚如何讓子級應用程序使用父級的Global.asax文件。 如果我刪除子應用程序的Global.asax文件身份驗證對於子應用程序根本不起作用。 有任何想法嗎?
這不是與繼承有關的問題。
如果要在其他網站上對用戶進行身份驗證,則應實施單一登錄策略http://en.wikipedia.org/wiki/Single_sign-on
有很多示例和方法。 這很復雜。
我認為每個應用程序都有自己的會話和狀態。無法傳遞該應用程序對象。 一種方法是通過應用程序將數據持久存儲在數據庫中,而另一個應用程序從公用共享數據庫中讀取數據並弄清楚如何理解數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.