예제 #1
0
        public async Task <ActionResult> ExternalLoginCallback(string ReturnUrl)
        {
            string msgGoogle = "L'utenza non risulta abilitata.";

            var loginInfo =
                await
                Microsoft.Owin.Security.AuthenticationManagerExtensions.GetExternalLoginInfoAsync(
                    HttpContext.GetOwinContext().Authentication);

            if (loginInfo == null)
            {
                return(RedirectToAction("Login", "Account", new { returnUrl = ReturnUrl }));
            }

            if (loginInfo.ExternalIdentity.IsAuthenticated)
            {
                string email = loginInfo.Email;

                using (ModelDBISE db = new ModelDBISE())
                {
                    if (db.DIPENDENTI?.Any(a => a.ABILITATO == true && a.EMAIL == email) ?? false)
                    {
                        var dip = db.DIPENDENTI.First(a => a.ABILITATO == true && a.EMAIL == email);
                        var ua  = dip.UTENTIAUTORIZZATI;

                        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, ua.UTENTE),
                            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 = false
                        }, identity);

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

                        //"/Home/Home"
                        return(Redirect(GetRedirectUrl(ReturnUrl)));
                    }
                    else
                    {
                        return(RedirectToAction("Login", "Account", new { returnUrl = ReturnUrl, msgGoogle = msgGoogle }));
                    }
                }
            }
            else
            {
                return(RedirectToAction("Login", "Account", new { returnUrl = ReturnUrl, msgGoogle = msgGoogle }));
            }

            //return Redirect(GetRedirectUrl(returnUrl));
        }
예제 #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));
            }
        }