Example #1
0
        public RedirectToActionResult Login(string username, string password)
        {
            try
            {
                string browserInfo = "Unable to determine";
                string deviceInfo  = "Unable to determine";

                try
                {
                    UserAgentHelper.SetUserAgent(Request.Headers["User-Agent"]);
                    browserInfo = UserAgentHelper.Browser.Name + " " + UserAgentHelper.Browser.Version + " " + UserAgentHelper.Browser.Major;
                    deviceInfo  = UserAgentHelper.OS.Name + " " + UserAgentHelper.OS.Version;
                }
                catch {  }

                var userModel = WebApiCaller.PostAsync <UserModel>("WebApi:Authenticate:Login", new LoginRequestModel
                {
                    Username = username,
                    Password = password,
                    Browser  = browserInfo,
                    Device   = deviceInfo
                });

                if (userModel.Username != null)
                {
                    if (userModel.IsAuthenticated)
                    {
                        CookieHelper.SignIn(userModel);
                        CookieHelper.SetCookie("LoggedInUser", JsonConvert.SerializeObject(userModel)); // put the encrypted version of the api session token
                        return(RedirectToAction("Index", "Orders", new { area = "Orders", userModel.Username }));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Authenticate", new AuthenticateViewModel {
                            Username = username, ErrorMessage = "Invalid password. Please try again."
                        }));
                    }
                }
                else
                {
                    return(RedirectToAction("Index", "Authenticate", new AuthenticateViewModel {
                        ErrorMessage = "Username does not exist."
                    }));
                }
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Error", "Home", new { area = "Home", IsError = "True", ex.Message, BaseMessage = ex.GetBaseException().Message }));
            }
        }
Example #2
0
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            _logger.Log(string.Format("Login request received for user : {0}", model.UserName),
                        LogCategory.Information, GetUserIdentifiableString(model.UserName));

            if (ModelState.IsValid)
            {
                bool result = this._activeDirectoryService.VerifyLoggedInUser(model.UserName, model.Password);
                if (result)
                {
                    string role = this._activeDirectoryService.VerifyGroupPolicy(model.UserName);


                    if (role == Constants.AdminRole)
                    {
                        _cookieHelper.SetCookie("userid", model.UserName, new TimeSpan(30, 0, 0));
                        _cookieHelper.SetCookie("adminMenuVisibility", "YES", new TimeSpan(30, 0, 0));
                        return(RedirectToLocal(returnUrl, Constants.AdminRole));
                    }
                    // check if user is member of that user group
                    else if (role == Constants.UserRole)
                    {
                        _cookieHelper.SetCookie("userid", model.UserName, new TimeSpan(30, 0, 0));
                        _cookieHelper.SetCookie("adminMenuVisibility", "NO", new TimeSpan(30, 0, 0));
                        return(RedirectToLocal(returnUrl, Constants.UserRole));
                    }
                    else
                    {
                        _cookieHelper.SetCookie("userid", string.Empty, new TimeSpan(30, 0, 0));
                        // user does not belong to active directory
                        _logger.Log(string.Format("User is not found in the active directory: {0}", model.UserName), LogCategory.Warning, GetUserIdentifiableString(model.UserName));
                    }
                }
                else
                {
                    _logger.Log(string.Format("Failed login attempt for user : {0}", model.UserName), LogCategory.Information, GetUserIdentifiableString(model.UserName));
                }
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");

            return(View(model));
        }
Example #3
0
        public IActionResult AddToCart(AddToCartViewModel model)
        {
            List <CartServiceViewModel> cartServiceViewModels = new List <CartServiceViewModel>();
            var cartCookie = _cookieHelper.GetCookie("_cart");

            if (cartCookie != null)
            {
                cartServiceViewModels = (List <CartServiceViewModel>)cartCookie;
            }
            if (cartServiceViewModels.Count > 100)
            {
                return(new JsonResult(new ApiResultViewModel {
                    Data = null, Message = "سبد بیش از حد مجاز پر شده است", Status = false
                }));
            }
            if (cartServiceViewModels.Any(x => x.Id == model.Id && x.TypeOfService == model.TypeOfService))
            {
                cartServiceViewModels.Where(x => x.Id == model.Id && x.TypeOfService == model.TypeOfService).Select(x => { x.Count = x.Count + 1; return(x); }).ToList();
            }
            else
            {
                dynamic service = null;
                if (model.TypeOfService == TypeOfServiceEnum.AdvertisementService)
                {
                    service = _advertisementPlanService.GetAdvertisementService(model.Id);
                }
                if (model.TypeOfService == TypeOfServiceEnum.GraphicService)
                {
                    service = _graphicDesigningPlanService.GetGraphicDesigningPlan(model.Id);
                }
                if (service != null)
                {
                    cartServiceViewModels.Add(new CartServiceViewModel()
                    {
                        Count = 1, Id = model.Id, TypeOfService = model.TypeOfService, Title = service.Title, Price = service.Price.ToString()
                    });
                }
            }
            _cookieHelper.SetCookie("_cart", cartServiceViewModels, 7);
            return(new JsonResult(new ApiResultViewModel {
                Data = null, Message = "سبد خرید بروزرسانی شد", Status = true
            }));
        }
Example #4
0
        public JsonResult Login(Student student)
        {
            ResultState resultState = new ResultState();

            if (!StudentNameExits(student.Name))
            {
                resultState.Message = "该用户不存在";
                return(new JsonResult(resultState));
            }
            var stuInDb = _context.students.Where(x =>
                                                  x.StudentID == student.StudentID)
                          .FirstOrDefault();

            if (student.Name != stuInDb.Name)
            {
                resultState.Message = "用户名错误";
                return(new JsonResult(resultState));
            }
            if (student.Password == stuInDb.Password)
            {
                resultState.Success = true;
                if (stuInDb.isAdmin == 1)
                {
                    resultState.Code    = 2;//Code=2表示学生管理员
                    resultState.Message = "学生管理员登录成功";
                }
                else
                {
                    resultState.Code    = 1;//Code=1表示非学生管理员登陆成功
                    resultState.Message = "学生登录成功";
                }
                resultState.value = stuInDb;
                _helper.SetCookie("token",
                                  stuInDb.StudentID + "," +
                                  stuInDb.Name + "," +
                                  stuInDb.Enrollment + "," +
                                  stuInDb.Department + "," +
                                  stuInDb.isAdmin);
            }
            return(new JsonResult(resultState));
        }
 public void Get()
 {
     _helper.SetCookie("cookieHelperKey", "cookieHelperValue");
     // 设置过期时间
     _helper.SetCookie("cookieHelperExpiresKey", "cookieHelperExpitesValue", 10);
 }