Beispiel #1
0
        public IActionResult Login()
        {
            var ip         = _accessor.HttpContext.Connection.RemoteIpAddress?.ToString();
            var MacAddress = GetClientData.GetMACAddress();

            ViewBag.MacAddress = MacAddress;
            var userAgent = Request.Headers["User-Agent"];

            UserAgent.UserAgent ua = new UserAgent.UserAgent(userAgent);



            HttpContext.Session.SetString("Macaddress", MacAddress);
            HttpContext.Session.SetString("OS", ua.OS.Name);
            HttpContext.Session.SetString("OSVersion", ua.OS.Version);
            HttpContext.Session.SetString("Browser", ua.Browser.Name);
            HttpContext.Session.SetString("BrowserVersion", ua.Browser.Version);
            HttpContext.Session.SetString("IPAddress", ip);
            HttpContext.Session.SetString("LoginDate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));



            if (Request.Cookies["user"] != null)
            {
                ViewBag.user     = Request.Cookies["user"];
                ViewBag.pass     = Request.Cookies["pass"];
                ViewBag.remember = Request.Cookies["remember"];
            }



            return(View(ua));
        }
Beispiel #2
0
        public IActionResult Login(string username, string password, bool remember, string Latitude, string Longitude)
        {
            if (username != null && password != null)
            {
                string hash = EncryptionHelper.Encrypt(password);


                //IQueryable<User> query = _context.Users;
                var login = _context.Logins
                            .Include(p => p.Users)
                            .Include(p => p.Users.TitleOfUsers)
                            .Include(p => p.TypeOfUsers)
                            .Include(p => p.Permisions)
                            .Include(p => p.CheckUsers)
                            .FirstOrDefault(p => p.Username.Equals(username) && p.Password.Equals(hash));


                if (login == null)
                {
                    Alert("รหัสผ่านผิดพลาด", NotificationType.error);
                    //TempData["SweetAlertTempdata"] = "ทดสอบ";
                    return(View());
                    //return RedirectToAction("Login", "Accounts");
                }
                else
                {
                    if (login.CheckUsers.CheckUserId == 1)
                    {
                        Alert("กรุณาติดต่อ Admin เพื่อยืนยันสิทธิ์การใช้งาน", NotificationType.info);
                        //ViewBag.error = "กรุณาติดต่อ Admin เพื่อยืนยันสิทธิ์การใช้งาน";
                        return(View());
                    }
                    else if (login.CheckUsers.CheckUserId == 3)
                    {
                        Alert("สิทธิ์การใช้งานของท่านถูกระงับ", NotificationType.error);
                        return(View());
                    }
                    else
                    {
                        var token = BuildToken(login);
                        HttpContext.Session.SetInt32("Userid", login.UserId);
                        HttpContext.Session.SetString("Username", login.Username);
                        HttpContext.Session.SetString("Firstname", login.Users.Firstname);
                        HttpContext.Session.SetString("Lastname", login.Users.Lastname);
                        HttpContext.Session.SetString("Pic", login.Users.Pic);
                        HttpContext.Session.SetString("TypeOfUserId", login.TypeOfUserId.ToString());
                        HttpContext.Session.SetString("TitleOfUserName", login.Users.TitleOfUsers.TitleOfUserName);
                        HttpContext.Session.SetString("TypeOfUserName", login.TypeOfUsers.TypeOfUserName);
                        HttpContext.Session.SetString("CheckUserName", login.CheckUsers.CheckUserName);
                        HttpContext.Session.SetString("PermisionName", login.Permisions.PermisionName);
                        HttpContext.Session.SetString("PermisionAction", login.Permisions.PermisionAction);
                        HttpContext.Session.SetString("JWToken", token);

                        CookieOptions option     = new CookieOptions();
                        int?          expireTime = 600000;

                        if (expireTime.HasValue)
                        {
                            option.Expires = DateTime.Now.AddMinutes(expireTime.Value);
                        }
                        else
                        {
                            option.Expires = DateTime.Now.AddMilliseconds(600000);
                        }

                        if (remember == true)
                        {
                            Response.Cookies.Append("user", username, option);
                            Response.Cookies.Append("pass", password, option);
                            Response.Cookies.Append("remember", "true", option);
                        }
                        else
                        {
                            Response.Cookies.Delete("user");
                            Response.Cookies.Delete("pass");
                            Response.Cookies.Delete("remember");
                        }



                        var ip                 = _accessor.HttpContext.Connection.RemoteIpAddress?.ToString();
                        var MacAddress         = GetClientData.GetMACAddress();
                        var userAgent          = Request.Headers["User-Agent"];
                        UserAgent.UserAgent ua = new UserAgent.UserAgent(userAgent);


                        Monitor Monitors = new Monitor();
                        Monitors.Date           = DateTime.Now;
                        Monitors.IP             = ip;
                        Monitors.Mac            = MacAddress;
                        Monitors.OS             = ua.OS.Name;
                        Monitors.OSVersion      = ua.OS.Version;
                        Monitors.Browser        = ua.Browser.Name;
                        Monitors.BrowserVersion = ua.Browser.Version;
                        Monitors.Latitude       = Latitude;
                        Monitors.Longitude      = Longitude;
                        Monitors.Username       = username;

                        _context.Add(Monitors);
                        _context.SaveChanges();


                        //LineAlert.LineNotify(username+" เข้าระบบเมื่อ  :" +DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                        Alert("Login", NotificationType.success);
                    }

                    return(RedirectToAction("Index", "Home"));
                    //return Redirect("Home/Index");
                }
            }
            else
            {
                Alert("Invalid Account", NotificationType.error);
                return(View());;
            }
        }