Ejemplo n.º 1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(Convert.ToString(Context.Items["SessionID"])))
        {
            qPtl_Sessions session = new qPtl_Sessions(Convert.ToInt32(Context.Items["SessionID"]));
            if (session.SessionID > 0)
            {
                session.StopTime = DateTime.Now;
                session.Update();
            }

            qPtl_User user = new qPtl_User(Convert.ToInt32(Context.Items["UserID"]));
            if (user.UserID > 0)
            {
                DateTime last_time = new DateTime();
                last_time = Convert.ToDateTime(user.LastTimeSeen);
                if (!String.IsNullOrEmpty(Convert.ToString(user.LastTimeSeen)))
                {
                    user.LastTimeSeen = last_time.AddMinutes(-16);
                }
                user.Update();
            }
        }

        Session.Abandon();
        FormsAuthentication.SignOut();
        Response.Redirect("~/default.aspx", true);
    }
    private void cmdLogin_ServerClick(object sender, System.EventArgs e)
    {
        if (ValidateUser(txtUserName.Value, txtUserPass.Value))
        {
            qPtl_User user = new qPtl_User(txtUserName.Value);

            qPtl_Sessions session = new qPtl_Sessions();
            session.Created      = DateTime.Now;
            session.StartTime    = DateTime.Now;
            session.LastTimeSeen = DateTime.Now;
            session.ScopeID      = user.ScopeID;
            session.UserID       = user.UserID;
            session.BrowserType  = Request.Browser.Browser;
            session.ComputerType = Request.Browser.Platform;
            session.Insert();
            int sessionID = session.SessionID;
            int scopeID   = user.ScopeID;

            var    u_roles   = qPtl_UserRole_View.GetUserRoles(user.UserID);
            string role_list = string.Empty;

            if (u_roles != null)
            {
                foreach (var u in u_roles)
                {
                    role_list += string.Format("{0},", u.RoleName, ",");
                }
            }

            role_list.TrimEnd(',');

            // see if there is a campaign code
            string reg_code        = string.Empty;
            string custom_redirect = string.Empty;
            if (!String.IsNullOrEmpty(txtSpaceCode.Text))
            {
                reg_code = txtSpaceCode.Text;

                qSoc_Campaign campaign = new qSoc_Campaign(reg_code);

                if (campaign.CampaignID > 0)
                {
                    // see if already enrolled
                    var exist_campaign = qSoc_UserCampaign.GetUserCampaign(user.UserID, campaign.CampaignID);

                    if (exist_campaign != null)
                    {
                        if (exist_campaign.UserCampaignID > 0)
                        {
                            exist_campaign.DeleteUserCampaign(campaign.CampaignID, user.UserID);
                            exist_campaign.DeleteUserCampaignActions(campaign.CampaignID, user.UserID);
                        }
                    }

                    AddUserCampaign(campaign.CampaignID, user.ScopeID, user);
                    custom_redirect = "~/social/learning/campaigns/campaign-details.aspx?campaignID=" + campaign.CampaignID;
                }
            }

            string userData = string.Format("{0};{1};{2}", sessionID, role_list, scopeID);

            FormsAuthenticationTicket tkt;
            string     cookiestr;
            HttpCookie ck;
            int        timeout_minutes          = 480;  // default shorter timeout (8 hours)
            int        timeout_extended_minutes = 1440; // default extended timeout (1 day)
            int        logout_minutes           = 0;
            if (!String.IsNullOrEmpty(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["Site_LoginTimeOut"])))
            {
                timeout_minutes = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Site_LoginTimeOut"]);
            }
            else if (!String.IsNullOrEmpty(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["Site_ExtendedLoginTimeOut"])))
            {
                timeout_extended_minutes = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Site_ExtendedLoginTimeOut"]);
            }

            if (chkLeaveLoggedIn.Checked)
            {
                logout_minutes = timeout_extended_minutes;
            }
            else
            {
                logout_minutes = timeout_minutes;
            }

            tkt       = new FormsAuthenticationTicket(1, user.UserID.ToString(), DateTime.Now, DateTime.Now.AddMinutes(logout_minutes), true, userData);
            cookiestr = FormsAuthentication.Encrypt(tkt);
            ck        = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
            //if (chkPersistCookie.Checked)
            //ck.Expires = tkt.Expiration;
            ck.Path = FormsAuthentication.FormsCookiePath;
            Response.Cookies.Add(ck);

            HttpCookie cookie2 = new HttpCookie("UserID", Convert.ToString(user.UserID));
            if (tkt.IsPersistent)
            {
                cookie2.Expires = tkt.Expiration.AddMinutes(logout_minutes + 5);
            }
            Response.Cookies.Add(cookie2);

            string strRedirect;
            strRedirect = Request["ReturnUrl"];
            if (strRedirect == null)
            {
                strRedirect = "~/utilities/manage-user-access.aspx";
            }
            if (!String.IsNullOrEmpty(custom_redirect))
            {
                strRedirect = custom_redirect;
            }
            Response.Redirect(strRedirect, true);
        }
        else
        {
            lblMsg.Text = "<br><br>Your username or password is not correct. Please try again.";
        }
    }