예제 #1
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            CaptchaViewModel captchaViewModel = (CaptchaViewModel)controllerContext.HttpContext.Session[sessionKey];

            if (captchaViewModel == null)
            {
                captchaViewModel = new CaptchaViewModel();
                controllerContext.HttpContext.Session[sessionKey] = captchaViewModel;
            }
            return(captchaViewModel);
        }
        public static CaptchaViewModel GenerateCaptcha(int charCount = 5)
        {
            CaptchaViewModel captchaViewModel = new CaptchaViewModel();
            var captchaCode  = GenerateCaptchaCode(charCount);
            var captchaImage = GenerateCaptchaImage(captchaCode);

            captchaViewModel.CaptchaId    = Guid.NewGuid().ToString();
            captchaViewModel.CaptchaCode  = captchaCode;
            captchaViewModel.CaptchaImage = String.Format("data:image/png;base64,{0}", Convert.ToBase64String(BitmapToBytes(captchaImage)));
            captchaViewModel.CaptchaToken = GetEncryptString($"{captchaViewModel.CaptchaId}_{captchaViewModel.CaptchaCode}");
            return(captchaViewModel);
        }
 public async Task Initialize()
 {
     await Application.Current.Dispatcher.InvokeAsync(() =>
     {
         window = new CaptchaWindow
         {
             Owner = Application.Current.MainWindow
         };
         window.Closing           += WindowClosing;
         viewModel                 = new CaptchaViewModel();
         viewModel.CaptchaEntered += CaptchaEntered;
         window.DataContext        = viewModel;
     });
 }
        public ActionResult Index(CaptchaViewModel model)
        {
            string msg = null;

            if (!ModelState.IsValid)
            {
                msg = string.Join(";", ModelState.Values.SelectMany(e => e.Errors).Select(gh => gh.ErrorMessage));
            }
            else
            {
                msg = "驗證成功!";
                //重啟Captcha
                MvcCaptcha.ResetCaptcha("ExampleCaptcha");
            }
            TempData["msg"] = msg;

            return(RedirectToAction(nameof(this.Index)));
        }
예제 #5
0
        public ActionResult GenerateCaptcha()
        {
            System.Drawing.FontFamily family = new System.Drawing.FontFamily("Arial");
            CaptchaImage img  = new CaptchaImage(150, 50, family);
            string       text = img.CreateRandomText(4) + " " + img.CreateRandomText(3);

            img.SetText(text);
            img.GenerateImage();
            img.Image.Save(Server.MapPath("~") +
                           "\\" +
                           ConfigurationManager.AppSettings["CaptchaFolder"] + "\\" +
                           this.Session.SessionID.ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
            Session["Captcha"] = new CaptchaViewModel
            {
                CaptchaText = text
            };
            return(Json(this.Session.SessionID.ToString() + ".png?t=" + DateTime.Now.Ticks, JsonRequestBehavior.AllowGet));
        }
예제 #6
0
        /// <summary>
        /// html view model
        /// </summary>
        /// <param name="captcha"></param>
        /// <param name="imagePath">Get pictures using HTTP</param>
        /// <param name="format">Get pictures using Base64</param>
        /// <returns></returns>
        public static CaptchaViewModel ToViewModel(this CaptchaImage captcha, string validatePath, Action <CaptchaViewModel> imagePath = null, bool hasBackground = false, IImageFormat format = null)
        {
            if (captcha == null)
            {
                return(new CaptchaViewModel());
            }
            var model = new CaptchaViewModel();

            model.Extension = captcha.Extension;
            model.Index     = JsonSerializer.Serialize(captcha.Index);
            model.Change    = JsonSerializer.Serialize(captcha.Change);
            model.Width     = captcha.Backgorund.Width;
            model.Height    = captcha.Backgorund.Height;
            model.Row       = captcha.Row;
            model.Col       = captcha.Col;
            model.X         = captcha.Points.Count;
            model.validate  = validatePath;
            model.Tips      = captcha.Tips;
            if (imagePath != null)
            {
                model.IsAction = true;
                imagePath(model);
            }
            else
            {
                model.IsAction = false;
                if (format == null)
                {
                    format = JpegFormat.Instance;
                }
                model.Gap   = captcha.Gap?.ToBase64String(PngFormat.Instance);
                model.BgGap = captcha.GapBackground?.ToBase64String(model.Height != captcha.GapBackground.Height?PngFormat.Instance:format);
                if (hasBackground)
                {
                    model.Full = captcha.Backgorund?.ToBase64String(format);
                }
            }

            model.Type = captcha.Type;
            model.Tk   = captcha.Token;
            model.Name = captcha.Name;
            return(model);
        }
예제 #7
0
        public ActionResult Register(RegisterModel model, CaptchaViewModel captchaViewModel)
        {
            if (model.CaptchaInput.Trim().ToLower() != captchaViewModel.CaptchaText.Trim().ToLower())
            {
                ModelState.AddModelError("Wrong captcha, please try again", new MissingFieldException());
            }
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(
                        model.UserName,
                        model.Password,
                        new
                    {
                        FullName     = model.FullName,
                        EmailAddress = model.EmailAddress,
                        Phone        = model.Phone,
                        Activated    = false
                    },
                        false);
                    string activationCode = _encryptionEngine.DESEncrypt(
                        model.UserName + separator.ToString() + model.EmailAddress);
                    _smtpEmail.SendActivationEmail(
                        activationCode,
                        model.FullName,
                        model.EmailAddress);
                    ViewBag.Message = "An activation email sent to your email address";
                    return(View("Message"));
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
예제 #8
0
        public CaptchaViewModel GetOrGenerateCaptcha(Guid challengeGuid)
        {
            var captcha = _services.WorkContext.HttpContext.Session[CaptchaServiceConstants.SESSION_KEY_PREFIX + challengeGuid] as CaptchaViewModel;

            if (captcha == null)
            {
                var settings = GetSettings();

                captcha = new CaptchaViewModel()
                {
                    Guid = challengeGuid,
                    Src  = _urlHelper.Action("GetCaptchaImage", "CaptchaImage",
                                             new { area = "MainBit.Captcha", challengeGuid, height = settings.ImageHeight, width = settings.ImageWidth }),
                    Width  = settings.ImageWidth,
                    Height = settings.ImageHeight,
                    Value  = MakeRandomSolution(settings)
                };

                _services.WorkContext.HttpContext.Session[CaptchaServiceConstants.SESSION_KEY_PREFIX + challengeGuid] = captcha;
            }

            return(captcha);
        }
 public ActionResult Setting(CaptchaViewModel model)
 {
     //重啟Captcha
     MvcCaptcha.ResetCaptcha("ExampleCaptcha");
     return(RedirectToAction(nameof(this.JQueryValidation)));
 }
예제 #10
0
 private void CaptchaView_OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
 {
     _viewModel = (CaptchaViewModel)e.NewValue;
     _viewModel.PropertyChanged += VmOnPropertyChanged;
 }
예제 #11
0
 public CaptChaControl()
 {
     InitializeComponent();
     this.viewModel = new CaptchaViewModel();
     this.Loaded   += (s, e) => { DataContext = this.viewModel; };
 }