コード例 #1
0
ファイル: Security.cs プロジェクト: wforney/portal
        public static string SignOn(string user, string password, bool persistent, string redirectPage)
        {
            // Obtain PortalSettings from Current Context
            var PortalSettings = (PortalSettings)HttpContext.Current.Items[StringsPortalSettings];

            MembershipUser usr;
            var            accountSystem = new UsersDB();

            // Attempt to Validate User Credentials using UsersDB
            usr = accountSystem.Login(user, password, PortalSettings.PortalAlias);

            // Thierry (tiptopweb), 12 Apr 2003: Save old ShoppingCartID
            //          ShoppingCartDB shoppingCart = new ShoppingCartDB();
            //          string tempCartID = ShoppingCartDB.GetCurrentShoppingCartID();
            if (usr != null && usr.IsApproved)
            {
                // Ender, 31 July 2003: Support for the monitoring module by Paul Yarrow
                if (Config.EnableMonitoring)
                {
                    try
                    {
                        Monitoring.LogEntry(
                            (Guid)usr.ProviderUserKey, PortalSettings.PortalID, -1, "Logon", string.Empty);
                    }
                    catch
                    {
                        ErrorHandler.Publish(LogLevel.Info, "Cannot monitoring login user " + usr.UserName);
                    }
                }

                // Use security system to set the UserID within a client-side Cookie
                FormsAuthentication.SetAuthCookie(usr.ToString(), persistent);

                // Appleseed Security cookie Required if we are sharing a single domain
                // with portal Alias in the URL

                // Set a cookie to persist authentication for each portal
                // so user can be reauthenticated
                // automatically if they chose to Remember Login
                int minuteAdd = 0;

                int.TryParse(PortalSettings.CustomSettings["SITESETTINGS_PORTALTIMEOUT"].ToString(), out minuteAdd);
                if (minuteAdd > 0)
                {
                    minuteAdd = Convert.ToInt32(PortalSettings.CustomSettings["SITESETTINGS_PORTALTIMEOUT"]);
                }
                else
                {
                    minuteAdd = Config.CookieExpire;
                }

                var hck = HttpContext.Current.Response.Cookies["Appleseed_" + PortalSettings.PortalAlias.ToLower()];
                if (hck != null)
                {
                    hck.Value = usr.ToString(); // Fill all data: name + email + id
                    hck.Path  = "/";

                    if (persistent)
                    {
                        // Keep the cookie?
                        hck.Expires = DateTime.Now.AddYears(50);
                    }
                    else
                    {
                        var time = DateTime.Now;
                        var span = new TimeSpan(0, 0, minuteAdd, 0, 0);
                        hck.Expires = time.Add(span);
                    }

                    HttpContext.Current.Response.Cookies.Add(hck);
                }

                // set session timeout from portal settings
                if (minuteAdd > 0)
                {
                    if (persistent)
                    {
                        CreateTicket(user, persistent, DateTime.Now.AddYears(50));
                    }
                    else
                    {
                        CreateTicket(user, persistent, DateTime.Now.AddMinutes(minuteAdd));
                    }
                }
                HttpCookie userlogin = new HttpCookie("userlogin");
                userlogin["persistent"] = persistent ? "1" : "0";
                userlogin.Expires       = DateTime.Now.AddYears(50);
                HttpContext.Current.Response.Cookies.Add(userlogin);

                //if we have returnurl in querystring, it should be redirect on it once logged in
                if (string.IsNullOrEmpty(redirectPage) && !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["ReturnUrl"]))
                {
                    redirectPage = HttpContext.Current.Request.QueryString["ReturnUrl"];
                }

                if (string.IsNullOrEmpty(redirectPage))
                {
                    // Redirect browser back to originating page
                    HttpContext.Current.Response.Redirect(
                        HttpContext.Current.Request.UrlReferrer != null
                            ? HttpContext.Current.Request.UrlReferrer.ToString()
                            : (HttpContext.Current.Request.ApplicationPath != null ? HttpContext.Current.Request.ApplicationPath.ToString() : "/"));

                    return(usr.Email);
                }

                HttpContext.Current.Response.Redirect(redirectPage);
            }

            return(null);
        }
コード例 #2
0
ファイル: Security.cs プロジェクト: wforney/portal
        /// <summary>
        /// Single point logoff
        /// </summary>
        /// <param name="urlToRedirect">
        /// The URL to redirect.
        /// </param>
        /// <param name="removeLogin">
        /// if set to <c>true</c> [remove login].
        /// </param>
        /// <remarks>
        /// </remarks>
        public static void SignOut(string urlToRedirect, bool removeLogin)
        {
            var st     = new StackTrace(new StackFrame(1, true));
            var frames = st.GetFrames();

            if (frames != null)
            {
                var stackString = frames.Aggregate(string.Empty, (current, frame) => current + ("> " + frame.GetMethod().Name));

                ErrorHandler.Publish(LogLevel.Info, "Hago signout: " + stackString);
            }

            // Log User Off from Cookie Authentication System
            FormsAuthentication.SignOut();

            // Invalidate roles token
            var hck = HttpContext.Current.Response.Cookies["portalroles"];

            if (hck != null)
            {
                hck.Value   = null;
                hck.Expires = new DateTime(1999, 10, 12);
                hck.Path    = "/";
            }

            if (removeLogin)
            {
                // Obtain PortalSettings from Current Context
                var PortalSettings = (PortalSettings)HttpContext.Current.Items[StringsPortalSettings];

                // Invalidate Portal Alias Cookie security
                var xhck = HttpContext.Current.Response.Cookies["Appleseed_" + PortalSettings.PortalAlias.ToLower()];
                if (xhck != null)
                {
                    xhck.Value   = null;
                    xhck.Expires = new DateTime(1999, 10, 12);
                    xhck.Path    = "/";
                }
            }

            // [START]  [email protected] remove user window information
            // User Information
            // valid user
            if (HttpContext.Current.User != null)
            {
                // Obtain PortalSettings from Current Context
                // Ender 4 July 2003: Added to support the Monitoring module by Paul Yarrow
                var PortalSettings = (PortalSettings)HttpContext.Current.Items[StringsPortalSettings];

                // User Information
                var            users = new UsersDB();
                MembershipUser user  = users.GetSingleUser(
                    HttpContext.Current.User.Identity.Name, PortalSettings.PortalAlias);

                if (user != null && user.ProviderUserKey != null)
                {
                    // get user id
                    var uid = (Guid)user.ProviderUserKey;

                    if (!uid.Equals(Guid.Empty))
                    {
                        try
                        {
                            if (Config.EnableMonitoring)
                            {
                                Monitoring.LogEntry(uid, PortalSettings.PortalID, -1, "Logoff", string.Empty);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }

            // [END ]  [email protected] remove user window information

            // Redirect user back to the Portal Home Page
            if (urlToRedirect.Length > 0)
            {
                HttpContext.Current.Response.Redirect(urlToRedirect);
            }
        }
コード例 #3
0
        public static string SignOn(string user, string password, bool persistent, string redirectPage)
        {
            // Obtain PortalSettings from Current Context
            var PortalSettings = (PortalSettings)HttpContext.Current.Items[StringsPortalSettings];

            MembershipUser usr;
            var            accountSystem = new UsersDB();

            // Attempt to Validate User Credentials using UsersDB
            usr = accountSystem.Login(user, password, PortalSettings.PortalAlias);

            // Thierry (tiptopweb), 12 Apr 2003: Save old ShoppingCartID
            //          ShoppingCartDB shoppingCart = new ShoppingCartDB();
            //          string tempCartID = ShoppingCartDB.GetCurrentShoppingCartID();

            if (usr != null)
            {
                // Ender, 31 July 2003: Support for the monitoring module by Paul Yarrow
                if (Config.EnableMonitoring)
                {
                    try
                    {
                        Monitoring.LogEntry(
                            (Guid)usr.ProviderUserKey, PortalSettings.PortalID, -1, "Logon", string.Empty);
                    }
                    catch
                    {
                        ErrorHandler.Publish(LogLevel.Info, "Cannot monitoring login user " + usr.UserName);
                    }
                }

                // Use security system to set the UserID within a client-side Cookie
                FormsAuthentication.SetAuthCookie(usr.ToString(), persistent);

                // Appleseed Security cookie Required if we are sharing a single domain
                // with portal Alias in the URL

                // Set a cookie to persist authentication for each portal
                // so user can be reauthenticated
                // automatically if they chose to Remember Login
                var hck = HttpContext.Current.Response.Cookies["Appleseed_" + PortalSettings.PortalAlias.ToLower()];
                if (hck != null)
                {
                    hck.Value = usr.ToString(); // Fill all data: name + email + id
                    hck.Path  = "/";

                    if (persistent)
                    {
                        // Keep the cookie?
                        hck.Expires = DateTime.Now.AddYears(50);
                    }
                    else
                    {
                        // jminond - option to kill cookie after certain time always
                        // jes1111
                        //                  if(ConfigurationSettings.AppSettings["CookieExpire"] != null)
                        //                  {
                        //                      int minuteAdd = int.Parse(ConfigurationSettings.AppSettings["CookieExpire"]);
                        var minuteAdd = Config.CookieExpire;

                        var time = DateTime.Now;
                        var span = new TimeSpan(0, 0, minuteAdd, 0, 0);

                        hck.Expires = time.Add(span);

                        //                  }
                    }
                }

                if (string.IsNullOrEmpty(redirectPage))
                {
                    // Redirect browser back to originating page
                    HttpContext.Current.Response.Redirect(
                        HttpContext.Current.Request.UrlReferrer != null
                            ? HttpContext.Current.Request.UrlReferrer.ToString()
                            : (HttpContext.Current.Request.ApplicationPath != null ? HttpContext.Current.Request.ApplicationPath.ToString() : "/"));

                    return(usr.Email);
                }

                HttpContext.Current.Response.Redirect(redirectPage);
            }

            return(null);
        }