Ejemplo n.º 1
0
        public CaptchaContent Build()
        {
            bool isSuccess = false;

            VerifyCode codeObj = new VerifyCode();

            codeObj.FontSize = 20;
            codeObj.FWidth   = 20;
            string code = codeObj.CreateVerifyCode(4);

            CaptchaContent captchaContent = CaptchaContent.Create(KeyGenerator.GetGuidKey().ToString(), code);

            return(_cacheManager.Get(captchaContent.Key, ctx =>
            {
                ctx.Monitor(_clock.When(TimeSpan.FromMinutes(Interval)));
                ctx.Monitor(_signals.When($"{ctx.Key}-changed"));

                try
                {
                    Image img = codeObj.CreateImageCode(code);
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    //将图像保存到指定的流
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                    captchaContent.Image = "data:image/jpeg;base64," + Convert.ToBase64String(ms.GetBuffer());
                    isSuccess = true;
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, $"发送图形验证码出错,Key{captchaContent.Key},内容{ex.Message}");
                }

                if (!isSuccess)
                {
                    //短信发送失败了,更新缓存
                    _signals.Trigger($"{ctx.Key}-changed");
                }

                return captchaContent;
            }));
        }
Ejemplo n.º 2
0
 public CaptchaDialog(ICaptcha captcha, CaptchaContent captchaContent) {
     Captcha = captcha;
     CaptchaContent = captchaContent;
     InitializeComponent();
     SetupView();
 }