简体   繁体   中英

Global.asax and inheritance

I have a parent app and a child app. Which currently have two separate Global.asax files. I'm trying to have this child app inherit from the parent app's Global.asax file.

So I have a file in my App_Code folder with all of the code in it as follows:

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;

        }
    }

}

Now I have my parent Global.asax file as follows:

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

and here is the codebehind file:

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

now here is my child app Global.asax file:

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

and here is the codebehind file:

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

Both classes in the codebehindfiles are inheriting from the source in the App_Code folder, however, the authentication status is not being passed from one app to another. For instance, if I login on the parent app, the authentication does not carry over to the child app. The opposite is also true.

I'm hoping I gave you guys enough detail.

Thanks!

EDIT:

Heinzi stated in the comments that this is not an inheritance issue. I need to figure out how to have the child app use the parent's Global.asax file. If i delete the child app's Global.asax file authentication does not work at all for the child app. Any ideas?

This is not a problem related to inheritance.

If you want to authenticate users in different websites you should implement a Single sign-on strategy http://en.wikipedia.org/wiki/Single_sign-on

There are many example and way to do it. It is quite complex.

i think every Application has its own Session and State.. it is not possible to pass the Application object. one way it is done is by an Application persisting the data in a database and the other application reading the data from the common shared database and figuring out how to make sense of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM