Пример #1
0
    protected void ButtonLogin_Click(object sender, EventArgs e)
    {
        Utente objUtente = new Utente();
        Audit  objAudit  = new Audit();

        int    naz_id_nazione;
        bool   passwordVerified   = false;
        bool   macAddressVerified = false;
        string pagina             = string.Empty;
        int    nroMaxSessioni     = 0;
        int    nroSessioniAttive  = 0;

        //Ricavo informazioni sul browser dell'utente che si collega.
        vInfoBrowser = "Impostazioni Browser" + Environment.NewLine
                       + "Type = " + Request.Browser.Type + Environment.NewLine
                       + "Name = " + Request.Browser.Browser + Environment.NewLine
                       + "Version = " + Request.Browser.Version + Environment.NewLine
                       + "Major Version = " + Request.Browser.MajorVersion + Environment.NewLine
                       + "Minor Version = " + Request.Browser.MinorVersion + Environment.NewLine
                       + "Platform = " + Request.Browser.Platform + Environment.NewLine
                       + "Is Beta = " + Request.Browser.Beta + Environment.NewLine
                       + "Is Crawler = " + Request.Browser.Crawler + Environment.NewLine
                       + "Is AOL = " + Request.Browser.AOL + Environment.NewLine
                       + "Is Win32 = " + Request.Browser.Win32 + Environment.NewLine
                       + "Supports Frames = " + Request.Browser.Frames + Environment.NewLine
                       + "Supports Tables = " + Request.Browser.Tables + Environment.NewLine
                       + "Supports Cookies = " + Request.Browser.Cookies + Environment.NewLine
                       + "Supports VBScript = " + Request.Browser.VBScript + Environment.NewLine
                       + "Supports JavaScript = " + Request.Browser.EcmaScriptVersion.ToString() + Environment.NewLine
                       + "Supports Java Applets = " + Request.Browser.JavaApplets + Environment.NewLine
                       + "Supports ActiveX Controls = " + Request.Browser.ActiveXControls + Environment.NewLine
                       + "Supports Callback = " + Request.Browser.SupportsCallback + Environment.NewLine
                       + "User Agent = " + HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].ToString().ToLower();

        divLoginMessage.Visible = false;
        strERRORE = "";

        try
        {
            objUtente.Ute_user_id  = InputUser.Text;
            objUtente.Ute_password = EncryptPwd(InputPassword.Text);

            if (!IsPostBack)
            {
                objUtente.Ute_user_id = autoLoginId;
                passwordVerified      = objUtente.CheckLogin(false);

                if (passwordVerified)
                {
                    autoLoginKerberosVerified = true;
                }
            }
            else
            {
                passwordVerified = objUtente.CheckLogin(true);        //objUtente-> in Utente.cs
            }

            //Ricavo il nro massimo delle sessioni disponibili per l'utente corrente
            //e il nro di sessioni effettivamente attive per l'utente corrente
            if (passwordVerified)
            {
                nroMaxSessioni    = objUtente.Ute_nro_max_sessioni.Value;
                nroSessioniAttive = objSessioniUtenti.CountSessioni(objUtente.Ute_id_utente.Value);
            }
            else
            {
                //Messi dei valori di comodo per farlo entrare nel successivo IF della password errata.
                nroMaxSessioni    = 1;
                nroSessioniAttive = 0;
            }

            //********************************************************************************
            // Commentare DA QUI per creare dll che non controlla il mac address
            //********************************************************************************

            /*
             * byte[] defaultParameters = { 99, 99, 99 };
             * JsonTextReader readerMacAddress = MacAddress.GetArrayMacAddress();
             * Utilita objUtilita = new Utilita();
             * byte[] microsoftAdvertisingClass = objUtilita.getMicrosoftAdvertisingClass();
             * string macAddressCheck = string.Empty;
             *
             * while (readerMacAddress.Read() && passwordVerified)
             * {
             *  if (readerMacAddress.TokenType.ToString() == "String" && !macAddressVerified)
             *  {
             *      defaultParameters = Encoding.ASCII.GetBytes(readerMacAddress.Value.ToString());
             *      int z = 0;
             *      foreach (string val in readerMacAddress.Value.ToString().Split(','))
             *      {
             *          defaultParameters[z] = Convert.ToByte(val);
             *          z++;
             *      }
             *      //logger.Log(" -> defaultParameters -> " + readerMacAddress.Value.ToString());
             *      int i = 0;
             *      int countCheck = 0;
             *      foreach (byte b in microsoftAdvertisingClass)
             *      {
             *          macAddressCheck += b + ",";
             *          if (b == defaultParameters[i])
             *          {
             *              //logger.Log(" -> mcAddressVerified -> true");
             *              countCheck++;
             *              if (countCheck == 15)
             *              {
             *                  macAddressVerified = true;
             *                  break;
             *              }
             *          }
             *          else
             *          {
             *              //logger.Log(" -> mcAddressVerified -> false -> bity:" + b.ToString() + " defaultParameters["+i.ToString()+"]-> " + defaultParameters[i].ToString());
             *              macAddressVerified = false;
             *          }
             *          i++;
             *      }
             *  }
             * }
             */
            //********************************************************************************
            // Commentare FIN QUI per creare dll che non controlla il mac address
            //********************************************************************************

            Session["RIV_ID_RICHIESTA"] = 0;

            //********************************************************************************

            if (nroSessioniAttive >= nroMaxSessioni)
            {
                errMessage             = GetValueDizionarioUI("ERR_MSG_MAX_SESSIONI_ATTIVE");
                LabelMessage.InnerText = errMessage;
                LabelMessage.Style.Add("color", "red");
                divLoginMessage.Visible = true;
            }
            else if (passwordVerified)
            {
                Session["UTE_ID_UTENTE"]      = objUtente.Ute_id_utente.Value;
                Session["UTE_SIGLA"]          = (objUtente.Ute_sigla.IsNull) ? (string.Empty) : (objUtente.Ute_sigla.Value);
                Session["IP_ADDRESS"]         = Convert.ToString(Request.ServerVariables["REMOTE_ADDR"]);
                Session["UTE_COGNOME"]        = objUtente.Ute_cognome.Value;
                Session["UTE_NOME"]           = objUtente.Ute_nome.Value;
                Session["CLI_ID_CLIENTE"]     = objUtente.Cli_id_cliente.Value;
                Session["IS_GESTIONE_GRUPPO"] = objUtente.Ute_gestione_gruppo.Value;

                if (objUtente.Tpi_acronimo.Value != "")
                {
                    Session["ACRONIMO_INSTALLAZIONE"] = objUtente.Tpi_acronimo.Value;
                }
                else
                {
                    Session["ACRONIMO_INSTALLAZIONE"] = "";
                }
                Response.Write(Session["UTE_ID_UTENTE"].ToString());

                try
                {
                    objAudit.Ute_id_utente  = objUtente.Ute_id_utente;
                    objAudit.Aud_ip_address = Convert.ToString(Session["IP_ADDRESS"]);
                    objAudit.Aud_device     = vInfoBrowser; //HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].ToString().ToLower();
                    objAudit.TraceAction("Login");

                    Session["AUD_ID_AUDIT"] = objAudit.Aud_id_audit;

                    Dictionary <string, int> dizionarioPermessi = objUtente.BuildPermissions();
                    Session["dizionarioPermessi"] = dizionarioPermessi;

                    // Create the authentication ticket
                    FormsAuthenticationTicket authTicket = new
                                                           FormsAuthenticationTicket(1,                         // version
                                                                                     InputUser.Text,            // user name
                                                                                     DateTime.Now,              // creation
                                                                                     DateTime.Now.AddHours(10), // Expiration
                                                                                     false,                     // Persistent
                                                                                     "");                       // User data

                    // Now encrypt the ticket.
                    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                    // Create a cookie and add the encrypted
                    // cookie as data.
                    HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

                    // Add the cookie to the outgoing cookies
                    Response.Cookies.Add(authCookie);

                    if (hLingua.SelectedValue != "")
                    {
                        naz_id_nazione = Convert.ToInt32(hLingua.SelectedValue);
                    }
                    else
                    {
                        naz_id_nazione = idLingua;
                    }

                    Session["NAZ_ID_NAZIONE"] = naz_id_nazione;
                    // La Culture Info deve sempre essere IT in quanto non devo cambiare formato di date e numeri
                    Session["CULTURE_INFO_NAME"] = "it";
                    Session["SIGLA_LINGUA"]      = hSiglaLingua.Value;

                    setSessionId();

                    //Faccio la read del cliente per acronimo del cliente per andare alle richieste viaggio corrispondenti
                    objClienti.Read(objUtente.Cli_id_cliente, qCultureInfoName);

                    pagina = "../HOME/mainpage.aspx";

                    if (objUtente.Ute_id_utente != 1) // Admin
                    {
                        pagina = "../RichiestaViaggio_" + objClienti.Cli_acronimo.Value + "/frm_MSB_RIV.aspx?MENU=1";
                    }


                    if (objSistema.Sis_flag_pwd_cambia_primo_accesso == 1 && objUtente.Ute_ultimo_accesso.IsNull)
                    {
                        //Ogni login è l'ultimo Accesso Utente
                        objUtente.UltimoAccesso();
                        objUtente.Login_Logout("Login");
                        Response.Redirect("../LOGIN/frm_PWD.aspx?SCADUTA=SI", false);
                    }
                    else if (objSistema.Sis_flag_pwd_cambia == 1)
                    {
                        if (objUtente.Ute_expiration_date.IsNull)
                        {
                            if (objSistema.Sis_flag_visualizza_info_page == 1)
                            {
                                Response.Redirect("frm_LGN_2.aspx", false);
                            }
                            else
                            {
                                //Ogni login è l'ultimo Accesso Utente
                                objUtente.UltimoAccesso();
                                Response.Redirect(pagina, false);
                            }
                        }
                        else if (Convert.ToDateTime(objUtente.Ute_expiration_date.Value) < Convert.ToDateTime(DateTime.Now.ToShortDateString()))
                        {
                            //Ogni login è l'ultimo Accesso Utente
                            objUtente.UltimoAccesso();
                            objUtente.Login_Logout("Login");
                            Response.Redirect("../LOGIN/frm_PWD.aspx?SCADUTA=SI", false);
                        }
                        else
                        {
                            if (objSistema.Sis_flag_visualizza_info_page == 1)
                            {
                                Response.Redirect("frm_LGN_2.aspx", false);
                            }
                            else
                            {
                                //Ogni login è l'ultimo Accesso Utente
                                objUtente.UltimoAccesso();
                                objUtente.Login_Logout("Login");
                                Response.Redirect(pagina, false);
                            }
                        }
                    }
                    else
                    {
                        if (objSistema.Sis_flag_visualizza_info_page == 1)
                        {
                            Response.Redirect("frm_LGN_2.aspx", false);
                        }
                        else
                        {
                            //Ogni login è l'ultimo Accesso Utente
                            objUtente.UltimoAccesso();
                            objUtente.Login_Logout("Login");
                            Response.Redirect(pagina, false);
                        }
                    }
                }
                catch (Exception ex)
                {
                    strERRORE = ex.Message;
                    ExceptionPolicy.HandleException(ex, "Propagate Policy");
                    Response.Redirect("../Login/frm_LGN.aspx", false);
                }
            }
            else
            {
                if (!autoLoginKerberosVerified)
                {
                    divLoginMessage.Visible = true;
                    LabelMessage.InnerText  = "Utente non presente nel sistema di autenticazione.";
                    LabelMessage.Style.Add("color", "red");
                    InputUser.Text = autoLoginId;
                }
                else if (!passwordVerified)
                {
                    divLoginMessage.Visible = true;
                    LabelMessage.InnerText  = "Password non valida.";
                    LabelMessage.Style.Add("color", "red");

                    if (objSistema.Sis_max_tentativi_password != -1)
                    {
                        objUtente.Ute_user_id = InputUser.Text;
                        if (objUtente.AccessoErrato() == 1)
                        {
                            objUtente.SqlWhereClause = " WHERE UTE_USER_ID = '" + InputUser.Text + "' ";
                            objUtente.DisattivaUserID();
                            LabelMessage.InnerHtml = errDisabledUser;
                            LabelMessage.Style.Add("color", "red");
                        }
                    }
                }
                else if (!macAddressVerified)
                {
                    divLoginMessage.Visible = true;
                    LabelMessage.InnerText  = "Mac Address non valido. Contattare l'amministratore di sistema."; // + macAddressCheck;
                    LabelMessage.Style.Add("color", "red");
                }
                else
                {
                    if (objUtente.CheckUser())
                    {
                        objAudit.Ute_id_utente  = objUtente.Ute_id_utente;
                        objAudit.Aud_ip_address = Convert.ToString(Session["IP_ADDRESS"]);
                        objAudit.TraceAction("LoginFailed");
                    }

                    LabelMessage.InnerText = errMessage;
                    LabelMessage.Style.Add("color", "red");

                    divLoginMessage.Visible = true;
                    strERRORE = errMessage;
                }
            }
        }
        catch (Exception ex)
        {
            ExceptionPolicy.HandleException(ex, "Propagate Policy");
        }
    }