示例#1
0
        public DipendenteRest GetDipendenteRest(string matricola)
        {
            DipendenteRest dr = new DipendenteRest();

            try
            {
                var client = new RestSharp.RestClient("http://bell.ice.it:5000");
                var req    = new RestSharp.RestRequest("api/dipendente", RestSharp.Method.GET);
                req.RequestFormat = RestSharp.DataFormat.Json;
                req.AddParameter("matricola", matricola);

                RestSharp.IRestResponse <RetDipendenteJson> resp = client.Execute <RetDipendenteJson>(req);

                if (resp.StatusCode == HttpStatusCode.BadRequest || resp.StatusCode == HttpStatusCode.InternalServerError || resp.StatusCode == HttpStatusCode.NotFound)
                {
                    return(dr);
                }

                RestSharp.Deserializers.JsonDeserializer deserial = new RestSharp.Deserializers.JsonDeserializer();

                RetDipendenteJson retDip = deserial.Deserialize <RetDipendenteJson>(resp);

                if (resp.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    if (retDip.success == true)
                    {
                        if (retDip.items != null)
                        {
                            using (dtAccount dta = new dtAccount())
                            {
                                dr.matricola      = retDip.items.matricola;
                                dr.cognome        = retDip.items.cognome;
                                dr.nome           = retDip.items.nome;
                                dr.cdf            = retDip.items.cdf;
                                dr.dataAssunzione = retDip.items.dataAssunzione;
                                dr.dataCessazione = retDip.items.dataCessazione;
                                dr.indirizzo      = retDip.items.indirizzo;
                                dr.cap            = retDip.items.cap;
                                dr.citta          = retDip.items.citta;
                                dr.provincia      = retDip.items.provincia;
                                dr.livello        = retDip.items.livello;
                                dr.cdc            = retDip.items.cdc;
                                dr.email          = retDip.items.email;
                                dr.disabilitato   = retDip.items.disabilitato;
                                dr.password       = retDip.items.password;
                            }
                        }
                    }
                }

                return(dr);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#2
0
        public ActionResult Login(loginModel account, string returnUrl)
        {
            //RetDipendenteJson rj = new RetDipendenteJson();
            AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
            //sAdmin sad = new sAdmin();
            //sUtenteNormale utentiNormali = new sUtenteNormale();

            try
            {
                if (!ModelState.IsValid)
                {
                    ViewBag.ModelStateCount = 1;
                    ModelState.AddModelError("", "L'username e la password sono obbligatori.");
                    return(View(account));
                }

                #region Code comment
                //using (Config cfg = new Config())
                //{
                //    sad = cfg.SuperAmministratore();
                //    if (sad.s_admin.Count > 0)
                //    {
                //        var lutsa = sad.s_admin.Where(a => a.username == account.username);

                //        if (lutsa.Count() > 0)
                //        {
                //            var utsa = lutsa.First();

                //            if (utsa != null)
                //            {
                //                if (utsa.username == account.username)
                //                {
                //                    if (utsa.password == account.password)
                //                    {
                //                        using (dtAccount dta = new dtAccount())
                //                        {
                //                            if (dta.VerificaAccesso(account.username))
                //                            {
                //                                UtenteAutorizzatoModel uam = new UtenteAutorizzatoModel();

                //                                uam = dta.PrelevaUtenteLoggato(account.username);
                //                                using (dtDipendenti dtd = new dtDipendenti())
                //                                {
                //                                    if (uam.HasValue())
                //                                    {
                //                                        uam.Dipendenti = dtd.GetDipendenteByID(uam.idDipendente);
                //                                    }
                //                                }

                //                                Claim[] identityClaims;

                //                                if (uam.HasValue())
                //                                {
                //                                    identityClaims = new Claim[]
                //                                    {
                //                                        new Claim(ClaimTypes.NameIdentifier,
                //                                            uam.idDipendente.ToString()),
                //                                        new Claim(ClaimTypes.Role,
                //                                            Convert.ToString((decimal) uam.idRuoloUtente)),
                //                                        new Claim(ClaimTypes.GivenName, utsa.username),
                //                                        new Claim(ClaimTypes.Name, utsa.nome),
                //                                        new Claim(ClaimTypes.Surname, utsa.cognome),
                //                                        new Claim(ClaimTypes.PostalCode, uam.Dipendenti.cap),
                //                                        new Claim(ClaimTypes.Country, uam.Dipendenti.citta),
                //                                        new Claim(ClaimTypes.StateOrProvince, uam.Dipendenti.provincia),
                //                                        new Claim(ClaimTypes.StreetAddress, uam.Dipendenti.indirizzo),
                //                                        new Claim(ClaimTypes.Email, utsa.email),
                //                                    };
                //                                }
                //                                else
                //                                {
                //                                    identityClaims = new Claim[]
                //                                    {
                //                                        new Claim(ClaimTypes.NameIdentifier,
                //                                            uam.idDipendente.ToString()),
                //                                        new Claim(ClaimTypes.Role,
                //                                            Convert.ToString((decimal) uam.idRuoloUtente)),
                //                                        new Claim(ClaimTypes.GivenName, utsa.username),
                //                                        new Claim(ClaimTypes.Name, utsa.nome),
                //                                        new Claim(ClaimTypes.Surname, utsa.cognome),
                //                                        new Claim(ClaimTypes.PostalCode, ""),
                //                                        new Claim(ClaimTypes.Country, ""),
                //                                        new Claim(ClaimTypes.StateOrProvince, ""),
                //                                        new Claim(ClaimTypes.StreetAddress, ""),
                //                                        new Claim(ClaimTypes.Email, utsa.email),
                //                                    };
                //                                }


                //                                ClaimsIdentity identity = new ClaimsIdentity(identityClaims,
                //                                    DefaultAuthenticationTypes.ApplicationCookie,
                //                                    ClaimTypes.NameIdentifier, ClaimTypes.Role);

                //                                Authentication.SignIn(new AuthenticationProperties
                //                                {
                //                                    IsPersistent = account.ricordati
                //                                }, identity);

                //                                using (objAccesso accesso = new objAccesso())
                //                                {
                //                                    accesso.Accesso(uam.idDipendente);
                //                                }

                //                                //"/Home/Home"
                //                                return Redirect(GetRedirectUrl(returnUrl));
                //                            }
                //                            else
                //                            {
                //                                ViewBag.ModelStateCount = 1;
                //                                ModelState.AddModelError("",
                //                                    "Le credenziali del super amministratore sono errate.");
                //                                return View(account);
                //                            }
                //                        }
                //                    }
                //                    else
                //                    {
                //                        ViewBag.ModelStateCount = 1;
                //                        ModelState.AddModelError("",
                //                            "Le credenziali del super amministratore sono errate.");
                //                        return View(account);
                //                    }
                //                }
                //            }
                //        }
                //    }

                //}
                #endregion

                bool   test     = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["Ambiente"]);
                string userTest = System.Configuration.ConfigurationManager.AppSettings["userTest"].ToString();

                using (ModelDBISE db = new ModelDBISE())
                {
                    if (test || account.username == userTest)
                    {
                        if (db.UTENTIAUTORIZZATI?.Any(a => a.UTENTE == account.username && a.PSW == account.password) ?? false)
                        {
                            var ua =
                                db.UTENTIAUTORIZZATI.Where(
                                    a => a.UTENTE == account.username && a.PSW == account.password).First();

                            var dip = ua.DIPENDENTI;

                            if (dip.ABILITATO == true)
                            {
                                Claim[] identityClaims;
                                identityClaims = new Claim[]
                                {
                                    new Claim(ClaimTypes.NameIdentifier,
                                              ua.IDDIPENDENTE.ToString()),
                                    new Claim(ClaimTypes.Role,
                                              Convert.ToString((decimal)ua.IDRUOLOUTENTE)),
                                    new Claim(ClaimTypes.GivenName, account.username),
                                    new Claim(ClaimTypes.Name, dip.NOME),
                                    new Claim(ClaimTypes.Surname, dip.COGNOME),
                                    new Claim(ClaimTypes.PostalCode, dip.CAP),
                                    new Claim(ClaimTypes.Country, dip.CITTA),
                                    new Claim(ClaimTypes.StateOrProvince, dip.PROVINCIA),
                                    new Claim(ClaimTypes.StreetAddress, dip.INDIRIZZO),
                                    new Claim(ClaimTypes.Email, dip.EMAIL),
                                };

                                ClaimsIdentity identity = new ClaimsIdentity(identityClaims,
                                                                             DefaultAuthenticationTypes.ApplicationCookie,
                                                                             ClaimTypes.NameIdentifier, ClaimTypes.Role);

                                Authentication.SignIn(new AuthenticationProperties
                                {
                                    IsPersistent = account.ricordati
                                }, identity);

                                using (objAccesso accesso = new objAccesso())
                                {
                                    accesso.Accesso(ua.IDDIPENDENTE);
                                }

                                //"/Home/Home"
                                return(Redirect(GetRedirectUrl(returnUrl)));
                            }
                            else
                            {
                                string msgGoogle = "";
                                ViewData["msgGoogle"]   = msgGoogle;
                                ViewBag.ModelStateCount = 1;
                                ModelState.AddModelError("", "L'utente non è abilitato all'accesso.");
                                return(View(account));
                            }
                        }
                        else
                        {
                            string msgGoogle = "";
                            ViewData["msgGoogle"]   = msgGoogle;
                            ViewBag.ModelStateCount = 1;
                            ModelState.AddModelError("", "L'utente non è autorizzato per l'accesso.");
                            return(View(account));
                        }
                    }
                    else
                    {
                        var client = new RestSharp.RestClient("https://indennita-estera-ws.ice.it");
                        var req    = new RestSharp.RestRequest("api/login", RestSharp.Method.POST);
                        req.RequestFormat = RestSharp.DataFormat.Json;
                        req.AddParameter("username", account.username);
                        req.AddParameter("password", account.password);

                        RestSharp.IRestResponse <RetDipendenteJson> resp = client.Execute <RetDipendenteJson>(req);

                        RestSharp.Deserializers.JsonDeserializer deserial = new RestSharp.Deserializers.JsonDeserializer();

                        RetDipendenteJson retDip = deserial.Deserialize <RetDipendenteJson>(resp);

                        if (resp.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            if (retDip.success == true)
                            {
                                if (retDip.items != null)
                                {
                                    var ua =
                                        db.UTENTIAUTORIZZATI.Where(
                                            a => a.UTENTE == account.username).First();

                                    var dip = ua.DIPENDENTI;


                                    Claim[] identityClaims = new Claim[]
                                    {
                                        new Claim(ClaimTypes.NameIdentifier, dip.IDDIPENDENTE.ToString()),
                                        new Claim(ClaimTypes.Role, Convert.ToString((decimal)ua.IDRUOLOUTENTE)),
                                        new Claim(ClaimTypes.GivenName, retDip.items.matricola),
                                        new Claim(ClaimTypes.Name, retDip.items.nome),
                                        new Claim(ClaimTypes.Surname, retDip.items.cognome),
                                        new Claim(ClaimTypes.PostalCode, retDip.items.cap),
                                        new Claim(ClaimTypes.Country, retDip.items.citta),
                                        new Claim(ClaimTypes.StateOrProvince, retDip.items.provincia),
                                        new Claim(ClaimTypes.StreetAddress, retDip.items.indirizzo),
                                        new Claim(ClaimTypes.Email, retDip.items.email),
                                    };

                                    ClaimsIdentity identity = new ClaimsIdentity(identityClaims,
                                                                                 DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.NameIdentifier,
                                                                                 ClaimTypes.Role);

                                    Authentication.SignIn(new AuthenticationProperties
                                    {
                                        IsPersistent = account.ricordati
                                    }, identity);

                                    using (objAccesso accesso = new objAccesso())
                                    {
                                        accesso.Accesso(ua.IDDIPENDENTE);
                                    }

                                    //"/Home/Home"
                                    return(Redirect(GetRedirectUrl(returnUrl)));
                                }
                                else
                                {
                                    ViewBag.ModelStateCount = 1;
                                    ModelState.AddModelError("", retDip.message);
                                    return(View(account));
                                }
                            }
                            else
                            {
                                ViewBag.ModelStateCount = 1;
                                ModelState.AddModelError("", retDip.message);
                                return(View(account));
                            }
                        }
                        else
                        {
                            ViewBag.ModelStateCount = 1;
                            ModelState.AddModelError("", resp.StatusDescription);
                            return(View(account));
                        }
                    }
                }


                #region Code comment
                //if (test)
                //{
                //    using (dtDipendenti dtdip = new dtDipendenti())
                //    {
                //        using (dtAccount dta = new dtAccount())
                //        {
                //            UtenteAutorizzatoModel uam = new UtenteAutorizzatoModel();

                //            if (dta.VerificaAccesso(account.username, out uam))
                //            {
                //                DipendentiModel dipm = new DipendentiModel();

                //                dipm = dtdip.GetDipendenteByID(uam.idDipendente);

                //                using (Config cfg = new Config())
                //                {
                //                    utentiNormali = cfg.UtentiNormali();

                //                    var lutsa = utentiNormali.s_utente.Where(a => a.username == account.username);

                //                    if (lutsa.Count() > 0)
                //                    {
                //                        var utsa = lutsa.First();

                //                        if (utsa.username == account.username)
                //                        {
                //                            if (utsa.password == account.password)
                //                            {
                //                                Claim[] identityClaims;
                //                                identityClaims = new Claim[]
                //                                {
                //                                    new Claim(ClaimTypes.NameIdentifier,
                //                                        uam.idDipendente.ToString()),
                //                                    new Claim(ClaimTypes.Role,
                //                                        Convert.ToString((decimal) uam.idRuoloUtente)),
                //                                    new Claim(ClaimTypes.GivenName, utsa.username),
                //                                    new Claim(ClaimTypes.Name, utsa.nome),
                //                                    new Claim(ClaimTypes.Surname, utsa.cognome),
                //                                    new Claim(ClaimTypes.PostalCode, dipm.cap),
                //                                    new Claim(ClaimTypes.Country, dipm.citta),
                //                                    new Claim(ClaimTypes.StateOrProvince, dipm.provincia),
                //                                    new Claim(ClaimTypes.StreetAddress, dipm.indirizzo),
                //                                    new Claim(ClaimTypes.Email, utsa.email),
                //                                };

                //                                ClaimsIdentity identity = new ClaimsIdentity(identityClaims,
                //                                    DefaultAuthenticationTypes.ApplicationCookie,
                //                                    ClaimTypes.NameIdentifier, ClaimTypes.Role);

                //                                Authentication.SignIn(new AuthenticationProperties
                //                                {
                //                                    IsPersistent = account.ricordati
                //                                }, identity);

                //                                using (objAccesso accesso = new objAccesso())
                //                                {
                //                                    accesso.Accesso(uam.idDipendente);
                //                                }

                //                                //"/Home/Home"
                //                                return Redirect(GetRedirectUrl(returnUrl));
                //                            }
                //                            else
                //                            {
                //                                ViewBag.ModelStateCount = 1;
                //                                ModelState.AddModelError("", "Le credenziali sono errate.");
                //                                return View(account);
                //                            }
                //                        }
                //                        else
                //                        {
                //                            ViewBag.ModelStateCount = 1;
                //                            ModelState.AddModelError("", "Le credenziali sono errate.");
                //                            return View(account);
                //                        }


                //                    }
                //                    else
                //                    {
                //                        ViewBag.ModelStateCount = 1;
                //                        ModelState.AddModelError("", "Le credenziali sono errate.");
                //                        return View(account);
                //                    }
                //                }
                //            }
                //            else
                //            {
                //                ViewBag.ModelStateCount = 1;
                //                ModelState.AddModelError("", "L'utente non è autorizzato per l'accesso.");
                //                return View(account);
                //            }

                //        }



                //    }
                //}
                //else
                //{
                //    var client = new RestSharp.RestClient("http://balau.ice.it:82");
                //    var req = new RestSharp.RestRequest("api/login", RestSharp.Method.POST);
                //    req.RequestFormat = RestSharp.DataFormat.Json;
                //    req.AddParameter("username", account.username);
                //    req.AddParameter("password", account.password);

                //    RestSharp.IRestResponse<RetDipendenteJson> resp = client.Execute<RetDipendenteJson>(req);

                //    RestSharp.Deserializers.JsonDeserializer deserial = new RestSharp.Deserializers.JsonDeserializer();

                //    RetDipendenteJson retDip = deserial.Deserialize<RetDipendenteJson>(resp);

                //    if (resp.StatusCode == System.Net.HttpStatusCode.OK)
                //    {
                //        if (retDip.success == true)
                //        {
                //            if (retDip.items != null)
                //            {
                //                using (dtAccount dta = new dtAccount())
                //                {
                //                    if (dta.VerificaAccesso(account.username))
                //                    {
                //                        UtenteAutorizzatoModel uam = new UtenteAutorizzatoModel();

                //                        uam = dta.PrelevaUtenteLoggato(account.username);

                //                        Claim[] identityClaims = new Claim[]
                //                        {
                //                        new Claim(ClaimTypes.NameIdentifier, uam.idDipendente.ToString()),
                //                        new Claim(ClaimTypes.Role, Convert.ToString((decimal) uam.idRuoloUtente)),
                //                        new Claim(ClaimTypes.GivenName, retDip.items.matricola),
                //                        new Claim(ClaimTypes.Name, retDip.items.nome),
                //                        new Claim(ClaimTypes.Surname, retDip.items.cognome),
                //                        new Claim(ClaimTypes.PostalCode, retDip.items.cap),
                //                        new Claim(ClaimTypes.Country, retDip.items.citta),
                //                        new Claim(ClaimTypes.StateOrProvince, retDip.items.provincia),
                //                        new Claim(ClaimTypes.StreetAddress, retDip.items.indirizzo),
                //                        new Claim(ClaimTypes.Email, retDip.items.email),
                //                        };

                //                        ClaimsIdentity identity = new ClaimsIdentity(identityClaims,
                //                            DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.NameIdentifier,
                //                            ClaimTypes.Role);

                //                        Authentication.SignIn(new AuthenticationProperties
                //                        {
                //                            IsPersistent = account.ricordati
                //                        }, identity);

                //                        using (objAccesso accesso = new objAccesso())
                //                        {
                //                            accesso.Accesso(uam.idDipendente);
                //                        }

                //                        //"/Home/Home"
                //                        return Redirect(GetRedirectUrl(returnUrl));
                //                    }
                //                    else
                //                    {
                //                        ViewBag.ModelStateCount = 1;
                //                        ModelState.AddModelError("", "Le credenziali sono errate.");
                //                        return View(account);
                //                    }
                //                }
                //            }
                //            else
                //            {
                //                ViewBag.ModelStateCount = 1;
                //                ModelState.AddModelError("", retDip.message);
                //                return View(account);
                //            }
                //        }
                //        else
                //        {
                //            ViewBag.ModelStateCount = 1;
                //            ModelState.AddModelError("", retDip.message);
                //            return View(account);
                //        }
                //    }
                //    else
                //    {
                //        throw new Exception(resp.StatusDescription);
                //    }
                //}
                #endregion
            }
            catch (Exception ex)
            {
                HandleErrorInfo her = new HandleErrorInfo(ex, "Account", "Login");
                return(View("Error", her));
            }
        }