public HttpCookie Login(LoginViewModel loginData)
        {
            if (Membership.ValidateUser(loginData.Username, loginData.Password))
            {
                var user = (CustomMembershipUser)Membership.GetUser(loginData.Username, false);
                if (user != null)
                {
                    CustomSerializeModel userModel = new CustomSerializeModel()
                    {
                        UserId    = user.UserId,
                        FirstName = user.FirstName,
                        LastName  = user.LastName,
                        RoleNames = user.Roles.Select(r => r.RoleName)
                    };

                    string userData = JsonConvert.Serialize(userModel);
                    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                           (
                        1, loginData.Username, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData
                                                           );

                    string     enTicket = FormsAuthentication.Encrypt(authTicket);
                    HttpCookie faCookie = new HttpCookie("Cookie1", enTicket);
                    return(faCookie);
                }
            }
            return(null);
        }
Beispiel #2
0
        public ActionResult PostLogin(LoginView loginViewModel, string ReturnUrl = "")
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var userDetails = LoginUser(loginViewModel.UserName, loginViewModel.Password);

                    using (EventLog eventLog = new EventLog("Application"))
                    {
                        eventLog.Source = "Application";
                        eventLog.WriteEntry("Entering PostLogin with target " + ReturnUrl + " and username " + loginViewModel.UserName, EventLogEntryType.Information, 101, 1);
                    }

                    if (userDetails == null)
                    {
                        ModelState.AddModelError("CustomError", "Something Wrong : UserName or Password invalid ^_^ ");
                        return(View(loginViewModel));
                    }

                    if (loginViewModel.RememberLogin)
                    {
                        var userModel = new CustomSerializeModel()
                        {
                            UserName = userDetails.UserName,
                            BlabName = userDetails.BlabName,
                            RealName = userDetails.RealName
                        };

                        using (var userModelStream = new MemoryStream())
                        {
                            IFormatter formatter = new BinaryFormatter();
                            formatter.Serialize(userModelStream, userModel);
                            var faCookie =
                                new HttpCookie(COOKIE_NAME, Convert.ToBase64String(userModelStream.GetBuffer()))
                            {
                                Expires = DateTime.Now.AddDays(30)
                            };
                            Response.Cookies.Add(faCookie);
                        }
                    }

                    if (string.IsNullOrEmpty(ReturnUrl))
                    {
                        return(RedirectToAction("Feed", "Blab"));
                    }

                    /* START BAD CODE */
                    return(Redirect(ReturnUrl));
                    /* END BAD CODE */
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("CustomError", ex.Message);
            }

            return(View(loginViewModel));
        }
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl = "")
        {
            //if (!ModelState.IsValid)
            //{
            //    return View(model);
            //}

            //// This doesn't count login failures towards account lockout
            //// To enable password failures to trigger account lockout, change to shouldLockout: true
            //var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
            //switch (result)
            //{
            //    case SignInStatus.Success:
            //        return RedirectToLocal(returnUrl);
            //    case SignInStatus.LockedOut:
            //        return View("Lockout");
            //    case SignInStatus.RequiresVerification:
            //        return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            //    case SignInStatus.Failure:
            //    default:
            //        ModelState.AddModelError("", "Invalid login attempt.");
            //        return View(model);
            //}
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.Email, model.Password))
                {
                    var user = (CustomMembershipUser)Membership.GetUser(model.Email, false);
                    if (user != null)
                    {
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            UserId    = user.UserId,
                            FirstName = user.FirstName,
                            LastName  = user.LastName,
                            RoleName  = new List <String>(), // user.Roles.Select(r => r.RoleName).ToList(),
                        };
                        string userData = JsonConvert.SerializeObject(userModel);
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                            1, model.Email, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData
                            );
                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("Cookie1", enTicket);
                        faCookie.Expires = DateTime.Now.AddMinutes(5);
                        Response.Cookies.Add(faCookie);
                    }
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return(Redirect(returnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index"));
                    }
                }
            }
            ModelState.AddModelError("", "Something Wrong: Username or Password invalid ");
            return(View(model));
        }
        public ActionResult Login(LoginView loginView, string ReturnUrl = "")
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(loginView.Username, loginView.Password))
                {
                    var user = (CustomMembershipUser)Membership.GetUser(loginView.Username, false);
                    if (user.IsActive == false)
                    {
                        ModelState.AddModelError("", "Please activate your account through your email: " + user.Email);
                        return(View(loginView));
                    }

                    CustomSerializeModel userModel = new CustomSerializeModel()
                    {
                        UserId   = user.UserId,
                        Username = user.Username,
                        RoleName = user.Role.Select(r => r.RoleName).ToList()
                    };

                    HttpCookie cookie         = FormsAuthentication.GetAuthCookie(loginView.Username, loginView.RememberMe);
                    DateTime   ExpirationTime = loginView.RememberMe == true?DateTime.Now.AddMonths(1) : DateTime.Now.AddMinutes(1);

                    cookie.Name = "Cookie1";
                    if (loginView.RememberMe)
                    {
                        cookie.Expires = DateTime.Now.AddMonths(1);
                    }

                    string userData = JsonConvert.SerializeObject(userModel);
                    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                           (
                        1, loginView.Username, DateTime.Now, ExpirationTime, loginView.RememberMe, userData, FormsAuthentication.FormsCookiePath
                                                           );
                    string enTicket = FormsAuthentication.Encrypt(authTicket);

                    cookie.Value = enTicket;
                    Response.Cookies.Add(cookie);

                    if (ReturnUrl == "")
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        return(Redirect(ReturnUrl));
                    }
                }
            }
            ModelState.AddModelError("User not in database", "Username or password invalid");

            return(View(loginView));
        }
Beispiel #5
0
        public ActionResult PostLogin(LoginView loginViewModel, string ReturnUrl = "")
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var userDetails = LoginUser(loginViewModel.UserName, loginViewModel.Password);

                    logger.Info("Entering PostLogin with target " + ReturnUrl + " and username " + loginViewModel.UserName);

                    if (userDetails == null)
                    {
                        ModelState.AddModelError("CustomError", "Something Wrong : UserName or Password invalid ^_^ ");
                        return(View(loginViewModel));
                    }

                    if (loginViewModel.RememberLogin)
                    {
                        var userModel = new CustomSerializeModel()
                        {
                            UserName = userDetails.UserName,
                            BlabName = userDetails.BlabName,
                            RealName = userDetails.RealName
                        };

                        var serializedUserModel = JsonConvert.SerializeObject(userModel, new JsonSerializerSettings {
                            TypeNameHandling = TypeNameHandling.All
                        });

                        Response.Cookies.Append(COOKIE_NAME, Convert.ToBase64String(Encoding.UTF8.GetBytes(serializedUserModel)), new CookieOptions {
                            Expires = DateTime.Now.AddDays(30)
                        });
                    }

                    if (string.IsNullOrEmpty(ReturnUrl))
                    {
                        return(RedirectToAction("Feed", "Blab"));
                    }

                    /* START BAD CODE */
                    return(Redirect(ReturnUrl));
                    /* END BAD CODE */
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("CustomError", ex.Message);
                logger.Info(ex.Message);
            }

            return(View(loginViewModel));
        }
Beispiel #6
0
        public ActionResult Login(LoginView loginView, string ReturnUrl = "")
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(loginView.UserName, loginView.Password))
                {
                    var user = (CustomMembershipUser)Membership.GetUser(loginView.UserName, false);
                    CustomSerializeModel userModel = new CustomSerializeModel();
                    if (user != null)
                    {
                        userModel.UserId    = user.UserId;
                        userModel.FirstName = user.FirstName;
                        userModel.LastName  = user.LastName;
                        userModel.RoleName  = user.Roles.Select(r => r.RoleName).ToList();

                        string userData = JsonConvert.SerializeObject(userModel);
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                               (
                            1, loginView.UserName, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData
                                                               );

                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("Cookie1", enTicket);
                        Response.Cookies.Add(faCookie);
                    }

                    if (Url.IsLocalUrl(ReturnUrl))
                    {
                        return(Redirect(ReturnUrl));
                    }
                    else
                    {
                        if (userModel.RoleName.Contains(Enums.Roles.Patient.ToString()))
                        {
                            return(RedirectToAction("Index", "Patient"));
                        }
                        else if (userModel.RoleName.Contains(Enums.Roles.Doctor.ToString()))
                        {
                            return(RedirectToAction("Index", "Doctor"));
                        }
                        else
                        {
                            return(RedirectToAction("Login", "Account"));
                        }
                    }
                }
            }
            ModelState.AddModelError("", "Something Wrong : Username or Password invalid ^_^ ");
            return(View(loginView));
        }
Beispiel #7
0
        // GET: Account
        public ActionResult Index()
        {
            CustomSerializeModel user = new CustomSerializeModel();

            user = (CustomSerializeModel)Session["user"];
            if (user != null)
            {
                ViewBag.idUser   = user.UserId;
                ViewBag.nameUser = user.FirstName;
                ViewBag.roleUser = user.RoleName[0];
                return(View());
            }
            return(RedirectToAction("Login"));
        }
Beispiel #8
0
        public ActionResult Login(LoginView loginView, string ReturnUrl = "")
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (Membership.ValidateUser(loginView.UserName, loginView.Password))
                    {
                        var user = (CustomMembershipUser)Membership.GetUser(loginView.UserName, false);
                        if (user != null)
                        {
                            CustomSerializeModel userModel = new CustomSerializeModel()
                            {
                                UserId    = user.UserId,
                                FirstName = user.FirstName,
                                LastName  = user.LastName,
                                RoleName  = user.Roles.Select(r => r.RoleName).ToList()
                            };

                            string userData = JsonConvert.SerializeObject(userModel);
                            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                                   (
                                1, loginView.UserName, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData
                                                                   );

                            string     enTicket = FormsAuthentication.Encrypt(authTicket);
                            HttpCookie faCookie = new HttpCookie("Cookie1", enTicket);
                            faCookie.Expires = DateTime.Now.AddMinutes(2);
                            Response.Cookies.Add(faCookie);
                        }

                        if (Url.IsLocalUrl(ReturnUrl))
                        {
                            return(Redirect(ReturnUrl));
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                }
                ModelState.AddModelError("", "Username or Password invalid");
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            }
            return(View(loginView));
        }
Beispiel #9
0
        public ActionResult Index(LOGIN Login)
        {
            if (!ModelState.IsValid)
            {
                return(View(Login));
            }
            if (Membership.ValidateUser(Login.email, MyEndCode.mahoa(Login.password)))
            {
                var user = (CustomMemberShipUser)Membership.GetUser(Login.email, false);
                if (!user.ACTIVE)
                {
                    ViewBag.message = "Tài khoản chưa được kích hoạt. Vui lòng liên hệ admin để được kích hoạt";
                }
                else
                {
                    if (user != null)
                    {
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            ID       = user.ID,
                            EMAIL    = user.EMAIL,
                            FULLNAME = user.FULLNAME,
                            ACTIVE   = user.ACTIVE,
                            PICTURE  = user.PICTURE,
                            PHONE    = user.PHONE,
                            ADDRESS  = user.ADDRESS,
                            ROLE     = user.ROLE
                        };

                        string userData = JsonConvert.SerializeObject(userModel);
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                               (
                            1, Login.email, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData
                                                               );

                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("Cookie1", enTicket);
                        Response.Cookies.Add(faCookie);
                    }
                    return(RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                ViewBag.message = "Email hoặc mật khẩu không đúng";
            }
            return(View(Login));
        }
Beispiel #10
0
        public ActionResult Login(Models.LoginView loginView, string ReturnUrl = "")
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(loginView.UserName, loginView.Password))
                {
                    var user = (CustomMembershipUser)Membership.GetUser(loginView.UserName, false);
                    if (user != null)
                    {
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            UserId = user.UserId,

                            FirstName = user.FirstName,
                            Email     = user.Email,
                            LastName  = user.LastName,
                            UserName  = user.UserName,
                            RoleName  = user.Roles.Select(r => r.RoleName).ToList()
                        };
                        Session["userid"]    = user.UserId;
                        Session["useremail"] = user.Email;
                        Session["username"]  = user.Email;

                        string userData = JsonConvert.SerializeObject(userModel);
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                               (
                            1, loginView.UserName, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData
                                                               );

                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("Cookie1", enTicket);
                        Response.Cookies.Add(faCookie);
                    }

                    if (Url.IsLocalUrl(ReturnUrl))
                    {
                        return(Redirect(ReturnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Chat"));
                    }
                }
            }
            ModelState.AddModelError("", "Something Wrong : Username or Password invalid ^_^ ");
            return(RedirectToAction("../Chat"));
        }
Beispiel #11
0
        protected void Application_PostAuthenticateRequest()
        {
            HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

            if (authCookie != null)
            {
                FormsAuthenticationTicket authTicket     = FormsAuthentication.Decrypt(authCookie.Value);
                CustomSerializeModel      userFromTicket = JsonConvert.DeserializeObject <CustomSerializeModel>(authTicket.UserData);
                Context.User = Thread.CurrentPrincipal = new CustomPrincipal(User.Identity.Name)
                {
                    UserID    = userFromTicket.UserID,
                    FirstName = userFromTicket.FirstName,
                    LastName  = userFromTicket.LastName,
                    Role      = userFromTicket.Role
                };
            }
        }
Beispiel #12
0
        public ActionResult Login(LoginVM login, string ReturnUrl = "")
        {
            if (ModelState.IsValid)
            {
                var db   = new MassDataEducationEntities();
                var data = db.Person.ToList();
                if (Membership.ValidateUser(login.Id, login.Password))
                {
                    var user     = (CustomMembershipUser)Membership.GetUser(login.Id, false);
                    var userdata = db.Person.Find(user.Id);
                    //var registration = db.Person.Find(user.Id);

                    if (user != null)
                    {
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            Id          = userdata.Id,
                            Name        = userdata.Name,
                            Email       = userdata.Email,
                            Image       = userdata.Image,
                            InstituteID = Convert.ToInt32(userdata.InstituteID),
                            UserType    = userdata.Type
                        };

                        string userData = JsonConvert.SerializeObject(userModel);
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, login.Id, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData);

                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("Cookie1", enTicket);
                        Response.Cookies.Add(faCookie);
                    }

                    if (Url.IsLocalUrl(ReturnUrl))
                    {
                        return(Redirect(ReturnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
            }
            ModelState.AddModelError("", "Invalid Username or Password");
            return(View(login));
        }
        public ActionResult PostLogin(LoginView loginViewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var userDetails = LoginUser(loginViewModel.UserName, loginViewModel.Password);


                    if (userDetails == null)
                    {
                        ModelState.AddModelError("CustomError", "Something Wrong : UserName or Password invalid ^_^ ");
                        return(View(loginViewModel));
                    }

                    if (loginViewModel.RememberLogin)
                    {
                        var userModel = new CustomSerializeModel()
                        {
                            UserName = userDetails.UserName,
                            BlabName = userDetails.BlabName,
                            RealName = userDetails.RealName
                        };

                        var faCookie =
                            new HttpCookie(COOKIE_NAME, JsonConvert.SerializeObject(userModel, Formatting.None))
                        {
                            Expires = DateTime.Now.AddDays(30)
                        };

                        Response.Cookies.Add(faCookie);
                    }

                    return(RedirectToAction("Feed", "Blab"));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("CustomError", ex.Message);
            }

            return(View(loginViewModel));
        }
Beispiel #14
0
        public ActionResult Login(LoginView loginView, string ReturnUrl = "")
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(loginView.UserName, loginView.Password))
                {
                    var user = (CustomMembershipUser)Membership.GetUser(loginView.UserName, false);
                    if (user != null)
                    {
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            UserId    = user.UserId,
                            FirstName = user.FirstName,
                            LastName  = user.LastName,
                            RoleName  = user.Roles.Select(r => r.RoleName).ToList()
                        };

                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                               (
                            1, loginView.UserName, DateTime.Now, DateTime.Now.AddMinutes(60), loginView.RememberMe, JsonConvert.SerializeObject(userModel)
                                                               );

                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("authCookie", enTicket)
                        {
                            Expires = authTicket.Expiration
                        };
                        Response.Cookies.Add(faCookie);
                    }

                    if (Url.IsLocalUrl(ReturnUrl))
                    {
                        return(Redirect(ReturnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "BTC"));
                    }
                }
            }
            ModelState.AddModelError("", "Something Wrong : Username or Password invalid");
            return(View(loginView));
        }
Beispiel #15
0
        public ActionResult Login(LoginViewModel loginView, string ReturnUrl = "")
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(loginView.Username, Helpers.EncryptData(loginView.Password)))
                {
                    var user = (CustomMembershipUser)Membership.GetUser(loginView.Username, false);
                    if (user != null)
                    {
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            UserId   = user.UserId,
                            Username = user.Username,
                            RoleName = ((eRole)user.RoleID).ToString()
                        };

                        string userData = JsonConvert.SerializeObject(userModel);
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                               (
                            1, loginView.Username, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData
                                                               );

                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("Cookie1", enTicket);
                        Response.Cookies.Add(faCookie);
                    }

                    switch (user.RoleID)
                    {
                    case (int)eRole.Employee:
                        return(RedirectToAction("Reservations", "Home"));

                    case (int)eRole.Visitor:
                        return(RedirectToAction("ReserveTable", "Home"));

                    default:
                        break;
                    }
                }
            }
            ModelState.AddModelError("", "Username or Password invalid");
            return(View(loginView));
        }
Beispiel #16
0
        public ActionResult Login(LoginView loginView, string ReturnUrl = "")
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(loginView.Email, loginView.Password))
                {
                    var user = (CustomMembershipUser)Membership.GetUser(loginView.Email, false);
                    if (user != null)
                    {
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            UserId    = user.UserId,
                            FirstName = user.FirstName,
                            LastName  = user.LastName,
                            RoleName  = user.Roles.Select(r => r.TUSR_DES_TYPE).ToList()
                        };
                        Session["user"] = userModel;
                        string userData = JsonConvert.SerializeObject(userModel);
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                               (
                            1, loginView.Email, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData
                                                               );

                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("Cookie1", enTicket);
                        Response.Cookies.Add(faCookie);
                    }

                    if (Url.IsLocalUrl(ReturnUrl))
                    {
                        return(Redirect(ReturnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index"));
                    }
                }
            }
            ModelState.AddModelError("", "Algo salió mal: El Usuario y la Contraseña son inválidos.");
            return(View(loginView));
        }
Beispiel #17
0
        public ActionResult Login(Login loginView, string ReturnUrl = "")
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(loginView.UserName, loginView.Password))
                {
                    var user = (CustomMembershipUser)Membership.GetUser((object)loginView.UserName, false);
                    if (user != null)
                    {
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            UserId    = user.UserId,
                            FirstName = user.FirstName,
                            LastName  = user.LastName,
                            RoleName  = user.Rol.Nombre
                        };

                        string userData = JsonConvert.SerializeObject(userModel);
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, loginView.UserName,
                                                                                             DateTime.Now, DateTime.Now.AddMinutes(30), false, userData);

                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("Cookie1", enTicket);
                        Response.Cookies.Add(faCookie);
                    }

                    if (Url.IsLocalUrl(ReturnUrl))
                    {
                        return(Redirect(ReturnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
            }
            ModelState.AddModelError("", "El usuario o la contraseña son incorrectos.");
            return(View(loginView));
        }
Beispiel #18
0
        public ActionResult Login(UserProfileModel model)
        {
            if (ModelState.IsValid)
            {
                var userId = _userService.Authenticate(model.UserName, model.Password);
                if (userId != null)
                {
                    var user = _userService.GetById(userId);
                    var c    = user.UserId;
                    if (user != null)
                    {
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            UserId    = user.UserId,
                            FirstName = user.FirstName,
                            LastName  = user.LastName,
                            RoleId    = user.RoleId,
                            RoleName  = user.RoleName
                        };

                        string userData = JsonConvert.SerializeObject(userModel);
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                               (
                            1, model.UserName, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData
                                                               );

                        var userIdentity = (ClaimsIdentity)System.Web.HttpContext.Current.User.Identity;
                        userIdentity.AddClaim(new Claim("roleid", user.RoleId.ToString()));

                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("cs_cookie", enTicket);
                        Response.Cookies.Add(faCookie);
                    }
                    return(RedirectToAction("Dashboard", "Dashboard"));
                }
            }
            ModelState.AddModelError("", "Username or Password invalid.");
            return(View(model));
        }
        public JsonResult CheckLogin(string username, string password, bool remember)
        {
            try
            {
                var rs = AccountDAO.CheckLogin(username, password);
                if (rs)
                {
                    Account acc = AccountDAO.GetAccountByUsername(username);
                    Session["User"] = acc;

                    if (remember)
                    {
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            UserId   = acc.ID,
                            FullName = acc.Fullname,
                            Email    = acc.Email,
                        };

                        string userData = JsonConvert.SerializeObject(userModel);
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddDays(1), remember, userData);
                        string     encTicket             = FormsAuthentication.Encrypt(ticket);
                        HttpCookie faCookie = new HttpCookie("login_form_cre", encTicket)
                        {
                            Expires = ticket.Expiration
                        };
                        Response.Cookies.Add(faCookie);
                    }

                    return(Json("Success"));
                }
            }
            catch (Exception ex)
            {
                return(Json("Fail"));
            }
            return(Json("Fail"));
        }
Beispiel #20
0
        public ActionResult Login(LoginModel model, string ReturnUrl = "")
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    var user = (CustomMembershipUser)Membership.GetUser(model.UserName, false);
                    if (user != null)
                    {
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            UserID = user.UserId,
                            Login  = user.UserName,
                            Roles  = user.Roles.Select(r => r.Name).ToList()
                        };

                        string userData = JsonConvert.SerializeObject(userModel);

                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, model.UserName, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData);
                        string enTicket = FormsAuthentication.Encrypt(authTicket);

                        HttpCookie faCookie = new HttpCookie("SecurityCookie", enTicket);
                        Response.Cookies.Add(faCookie);
                    }

                    if (Url.IsLocalUrl(ReturnUrl))
                    {
                        return(Redirect(ReturnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
            }
            ModelState.AddModelError(string.Empty, "Username or Password invalid");
            return(View(model));
        }
        public ActionResult Login(LoginView loginView)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(loginView.UserName, loginView.Password))
                {
                    var user = (CustomMembershipUser)Membership.GetUser(loginView.UserName, false);
                    if (user != null)
                    {
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            Id       = user.Id,
                            UserName = user.UserName,
                            RoleName = user.Roles.Select(r => r.Name).ToList()
                        };

                        string userData = JsonConvert.SerializeObject(userModel);
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                               (
                            1, loginView.UserName, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData
                                                               );

                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("CookieTest", enTicket);
                        Response.Cookies.Add(faCookie);
                    }

                    if (user.Roles.Where(s => s.Name.Contains("MANAGER")).SingleOrDefault() != null)
                    {
                        return(RedirectToAction("List", "Application"));
                    }

                    return(RedirectToAction("Create", "Application"));
                }
            }
            ModelState.AddModelError("", "УПС! Поль-ля с такими именем и паролем - нет.");
            return(View(loginView));
        }
Beispiel #22
0
        public void LoginWithActivationCode(string code)
        {
            var userByCode = unitOfWork.Repository <User>().Find(x => x.ActivationCode.ToString() == code).First();

            if (userByCode != null)
            {
                CustomSerializeModel userModel = new CustomSerializeModel()
                {
                    UserId   = userByCode.UserId,
                    Username = userByCode.Username,
                    RoleName = userByCode.Role.Select(r => r.RoleName).ToList()
                };

                string userData = JsonConvert.SerializeObject(userModel);
                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                       (
                    1, userByCode.Username, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData
                                                       );

                string     enTicket = FormsAuthentication.Encrypt(authTicket);
                HttpCookie faCookie = new HttpCookie("Cookie1", enTicket);
                Response.Cookies.Add(faCookie);
            }
        }
        public ActionResult Login(LoginView loginView, string ReturnUrl = "")
        {
            if (ModelState.IsValid)
            {
                loginView.ChangedPSW = false;
                loginView.UserName   = loginView.UserName.TrimEnd();

                if (Membership.ValidateUser(loginView.UserName, loginView.Password))
                {
                    var user = (CustomMembershipUser)Membership.GetUser(loginView.UserName, false);
                    if (user == null)
                    {
                        ModelState.AddModelError("Error", "Something Wrong : Username or Password invalid!");
                        ViewBag.UserName = loginView.UserName;
                        ViewBag.PSW      = loginView.Password;
                        return(View(loginView));
                    }
                    string oldPassword = loginView.Password.ToString();



                    if (loginView.NewPassword != null)
                    {
                        string newPassword = loginView.NewPassword.ToString();
                        if (loginView.ConfirmPassword != newPassword)
                        {
                            ModelState.AddModelError("Error", "Something Wrong : Confirm Password doesn't match with new password!");
                            return(View(loginView));
                        }

                        if (oldPassword == newPassword)
                        {
                            ModelState.AddModelError("Error", "Something Wrong : New Password can't be the same as the old one!");
                            //return View(loginView);
                        }



                        if (newPassword.Length < 8)
                        {
                            ModelState.AddModelError("Error", "New password lenght must be 8 or more.");
                            //return View(loginView);
                        }
                        if (Regex.IsMatch(newPassword, @"^(?=.*[a-z])(?=.*[A-Z]).+$", RegexOptions.ECMAScript) == false) //both, lower and upper case
                        {
                            ModelState.AddModelError("Error", "New password must contains at least 1 Uppercase character (A-Z) and 1 Lowercase(a-z) character");
                            //return View(loginView);
                        }
                        if (Regex.IsMatch(newPassword, @"[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]", RegexOptions.ECMAScript) == false) //^[A-Z]+$
                        {
                            ModelState.AddModelError("Error", "New password must contain at least 1 special character");
                            //return View(loginView);
                        }
                        if (ModelState.IsValid == false)
                        {
                            ViewBag.UserName = loginView.UserName;
                            ViewBag.PSW      = loginView.Password;

                            return(View(loginView));
                        }
                        user.ChangePassword(oldPassword, newPassword);
                        //var userp = dbContext.Users.Where(x => x.Username == loginView.UserName).FirstOrDefault();
                        //userp.Password = newPassword;
                        //dbContext.Entry(user).State = EntityState.Modified;
                        //dbContext.SaveChanges();
                        //loginView.ChangedPSW = true;
                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            UserId    = user.UserId,
                            FirstName = user.FirstName,
                            LastName  = user.LastName,
                            RoleName  = user.Roles.Select(r => r.RoleName).ToList(),
                            DOI       = user.DOI
                        };

                        string   userData = JsonConvert.SerializeObject(userModel);
                        bool     remember = false;
                        DateTime scadenza = DateTime.Now.AddMinutes(15);
                        if (loginView.RememberMe == true)
                        {
                            remember = true;
                            scadenza.AddYears(1);
                        }
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                               (

                            1, loginView.UserName, DateTime.Now, scadenza, remember, userData //v1.7 : per rimanere connessi
                                                               );

                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("edi_auth", enTicket);
                        Response.Cookies.Add(faCookie);
                        ViewBag.Changed = true;
                        return(View(loginView));


                        //aggiungere pagina conferma cambio psw
                    }



                    if (user != null)
                    {
                        //if (loginView.NewPassword.ToString() != " " && loginView.ConfirmPassword.ToString() != " ")
                        //{
                        //    Membership.ChangePassword(loginView.UserName, loginView.NewPassword, loginView.ConfirmPassword);

                        //}

                        CustomSerializeModel userModel = new CustomSerializeModel()
                        {
                            UserId    = user.UserId,
                            FirstName = user.FirstName,
                            LastName  = user.LastName,
                            RoleName  = user.Roles.Select(r => r.RoleName).ToList(),
                            DOI       = user.DOI
                        };

                        string   userData = JsonConvert.SerializeObject(userModel);
                        bool     remember = false;
                        DateTime scadenza = DateTime.Now.AddMinutes(15);
                        if (loginView.RememberMe == true)
                        {
                            remember = true;
                            scadenza.AddYears(1);
                        }
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                               (

                            1, loginView.UserName, DateTime.Now, scadenza, remember, userData //v1.7 : per rimanere connessi
                                                               );

                        string     enTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie = new HttpCookie("edi_auth", enTicket);
                        Response.Cookies.Add(faCookie);
                        if (Url.IsLocalUrl(ReturnUrl))
                        {
                            return(Redirect(ReturnUrl));
                        }
                        else
                        {
                            return(RedirectToAction("Index"));
                        }
                    }
                }
            }
            ModelState.AddModelError("Error", "Something Wrong : Username or Password invalid");
            return(View(loginView));
        }
        private long getCurrentUserPID(CustomSerializeModel userModel)
        {
            ManageAdminHelper MAHelper = new ManageAdminHelper();

            return(MAHelper.getCurrentUserPID(userModel.UserCode));
        }
Beispiel #25
0
        public ActionResult Login(LoginViewModel loginViewModel, string ReturnUrl = "")
        {
            var userip = ToolHelper.GetIp();

            if (ModelState.IsValid)
            {
                bool isActive = false;
                if (membership.ValidateUser(loginViewModel.Username, loginViewModel.Password, out isActive))
                {
                    if (isActive)
                    {
                        var user = membership.GetUser(loginViewModel.Username, false);
                        if (user.IsActive)
                        {
                            if (user != null)
                            {
                                AurthenticateSupport authenSupport = new AurthenticateSupport(user);
                                CustomSerializeModel userModel     = new CustomSerializeModel()
                                {
                                    FullName       = user.FullName,
                                    Image          = user.Image,
                                    UserId         = user.Id,
                                    Email          = user.Username,
                                    RoleName       = user.Roles.Select(r => r.RoleName).ToList(),
                                    IsRoot         = authenSupport.IsRoot,
                                    PermissionList = authenSupport.PermissionList.Select(p => p.PermissionCode).ToList()
                                };

                                string userData = JsonConvert.SerializeObject(userModel);
                                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                                       (
                                    1, loginViewModel.Username, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData
                                                                       );
                                string enTicket = FormsAuthentication.Encrypt(authTicket);
                                if (loginViewModel.IsRemember)
                                {
                                    HttpCookie faCookie = new HttpCookie(keyCookie, enTicket);
                                    FormsAuthentication.SetAuthCookie(loginViewModel.Username, loginViewModel.IsRemember);
                                    Response.Cookies.Add(faCookie);
                                }
                                else
                                {
                                    HttpCookie cookie = Request.Cookies[keyCookie];
                                    if (cookie != null)
                                    {
                                        cookie.Expires = DateTime.Now.AddHours(48);
                                    }
                                    else
                                    {
                                        cookie         = new HttpCookie(keyCookie, enTicket);
                                        cookie.Expires = DateTime.Now.AddHours(48);
                                    }
                                    Response.Cookies.Add(cookie);
                                }
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("", "Tài khoản của bạn đang bị khóa !");
                            return(View(loginViewModel));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Tài khoản của bạn chưa được cấp quyền !");
                        return(View(loginViewModel));
                    }

                    if (Url.IsLocalUrl(ReturnUrl))
                    {
                        return(Redirect(ReturnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
            }
            ModelState.AddModelError("", "Thông tin đăng nhập bị sai !");
            return(View(loginViewModel));
        }