コード例 #1
0
        public ActionResult Login(string email, string pass, string returnView)
        {
            unescoEntities db = new unescoEntities();

            var L2EQuery = db.usuarios.Where(s => s.username == email);
            usuario logged_user = L2EQuery.FirstOrDefault();

            if (logged_user != null)
            {

                if (isvalidLogin(logged_user, pass))
                {

                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(email, true, 30);

                    //' Encrypt the ticket.
                    //Dim encTicket As String = FormsAuthentication.Encrypt(ticket)
                    string encTicket = FormsAuthentication.Encrypt(ticket);

                    //Dim ck As New HttpCookie(FormsAuthentication.FormsCookieName, encTicket)

                    HttpCookie ck = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                    //' Create the cookie.
                    Response.Cookies.Add(ck);
                    if (returnView == "")
                    {
                        return Redirect("/Home/Index");
                    }
                    else
                    {
                        return Redirect(returnView);
                    }

                }
               else
                {
                    ModelState.AddModelError("", "Nombre de usuario o contraseña no válido.");

                    ViewData["user"] = email;

                    return RedirectToAction("Login", "AccountManager", new { user = email });

                }
            }
            else
            {
                ModelState.AddModelError("", "Nombre de usuario no válido.");

                ViewData["user"] = email;

                return RedirectToAction("Login", "AccountManager", new { user = email });

            }
        }
コード例 #2
0
        public static usuario getLoggedUser()
        {
            try
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies["gaslogin"];

                FormsAuthenticationTicket tk = FormsAuthentication.Decrypt(cookie.Value);

                string logeduser = tk.Name;

                //gasnaturalHelper helper = new gasnaturalHelper()
                unescoEntities db = new unescoEntities();

                usuario user = db.usuarios.Where(u => u.username == logeduser).First();

                return user;
            }
            catch
            {
                return null;
            }
        }