Example #1
0
        } // end of Application_BeginRequest

        /// <summary>
        /// Handles the AuthenticateRequest event of the Application control.
        /// If the client is authenticated with the application, then determine
        /// which security roles he/she belongs to and replace the "User" intrinsic
        /// with a custom IPrincipal security object that permits "User.IsInRole"
        /// role checks within the application
        /// Roles are cached in the browser in an in-memory encrypted cookie.  If the
        /// cookie doesn't exist yet for this session, create it.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            Reader      contextReader = new Reader(new WebContextReader());
            HttpContext context       = contextReader.Current;

            if (context.Items["PortalSettings"] != null)
            {
                // Obtain PortalSettings from Current Context
                PortalSettings portalSettings = (PortalSettings)context.Items["PortalSettings"];

                // Auto-login a user who has a portal Alias login cookie
                // Try to authenticate the user with the cookie value
                if (!context.Request.IsAuthenticated &&
                    (context.Request.Cookies["Rainbow_" + portalSettings.PortalAlias] != null))
                {
                    if (context.Request.Cookies["Rainbow_" + portalSettings.PortalAlias].Expires > DateTime.Now)
                    {
                        string user;
                        user = context.Request.Cookies["Rainbow_" + portalSettings.PortalAlias.ToLower()].Value;

                        //jminond - option to kill cookie after certain time always
                        int minuteAdd = Config.CookieExpire;

                        // Create the FormsAuthentication cookie
                        FormsAuthentication.SetAuthCookie(user, true);

                        // Create a FormsAuthentication ticket.
                        FormsAuthenticationTicket cTicket = new FormsAuthenticationTicket
                                                            (
                            1,            // version
                            user,         // user name
                            DateTime.Now, // issue time
                            DateTime.Now.AddMinutes(minuteAdd),
                            false,        // don't persist cookie
                            string.Empty  // roles
                                                            );

                        // Set the current User Security to the FormsAuthenticated User
                        context.User = new RainbowPrincipal(new FormsIdentity(cTicket), null);
                    }
                }
                else
                {
                    // jminond - if user asked to persist, he should have a cookie
                    if ((context.Request.IsAuthenticated) &&
                        (context.Request.Cookies["Rainbow_" + portalSettings.PortalAlias] == null))
                    {
                        PortalSecurity.KillSession();
                    }
                }

                //if (context.Request.IsAuthenticated && !(context.User is WindowsPrincipal))
                //{
                //    // added by Jonathan Fong 22/07/2004 to support LDAP
                //    //string[] names = Context.User.Identity.Name.Split("|".ToCharArray());
                //    string[] names = context.User.Identity.Name.Split('|');
                //    if (names.Length == 3 && names[2].StartsWith("cn="))
                //    {
                //        context.User = new RainbowPrincipal(
                //            new User(context.User.Identity.Name, "LDAP"), LDAPHelper.GetRoles(names[2]));
                //    }
                //    else
                //    {
                //        // Add our own custom principal to the request containing the roles in the auth ticket
                //        context.User = new RainbowPrincipal(context.User.Identity, PortalSecurity.GetRoles());
                //    }
                //    // Remove Windows specific custom settings
                //    if (portalSettings.CustomSettings != null)
                //        portalSettings.CustomSettings.Remove("WindowsAdmins");
                //}
                //    // [email protected] - need to get a unique id for user
                //else if (Config.WindowMgmtControls)
                //{
                //    // Need a uid, even for annoymous users
                //    string annoyUser;
                //    // cookie bag
                //    IWebBagHolder abag = BagFactory.instance.create(BagFactory.BagFactoryType.CookieType);
                //    // user data already set
                //    annoyUser = (string) abag[GlobalInternalStrings.UserWinMgmtIndex];
                //    // if no cookie then let's get one
                //    if (annoyUser == null)
                //    {
                //        // new uid for window mgmt
                //        Guid guid = Guid.NewGuid();
                //        // save the data into a cookie bag
                //        abag[GlobalInternalStrings.UserWinMgmtIndex] = guid.ToString();
                //    }
                //}
            }
        } // end of Application_AuthenticateRequest