Пример #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SerialBuilder"/> class.
 /// </summary>
 public SerialBuilder()
 {
     InitializeComponent();
     this.Load += new EventHandler(
         delegate(object sender, EventArgs e) {
         this.pictureBox1.Image = CaptchaUtil.Generate(StringUtil.GenUniqueString());
     }
         );
 }
Пример #2
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string resp = CaptchaUtil.Login(this.txtUname.Text, this.txtPwd.Text, out USER_KEY);

            if (USER_KEY == null)
            {
                CaptchaUtil.HttpResp httpResp = JsonConvert.DeserializeObject <CaptchaUtil.HttpResp>(resp);
                MessageBox.Show(httpResp.msg);
            }
        }
Пример #3
0
 public IActionResult Index(LoginViewModel model)
 {
     if (ModelState.IsValid)
     {
         string hashCaptcha = string.Empty;
         HttpContext.Request.Cookies.TryGetValue(_cookieCaptchaKey, out hashCaptcha);
         if (!CaptchaUtil.VerifyCaptcha(model.Captcha, hashCaptcha))
         {
             //the captach is not correct
         }
     }
     return(View(model));
 }
Пример #4
0
        public void OversizeTest()
        {
            /**
             * REF: https://github.com/stulzq/DotnetCore.RSA/issues/2
             *      https://www.cnblogs.com/CreateMyself/p/9853736.html
             */
            string data   = CaptchaUtil.GetCaptcha(4096);
            string cipher = RsaUtil.Encrypt(data, RSA_PUBLIC_KEY_1024);
            string sign   = RsaUtil.Sign(data, RSA_PRIVATE_KEY_1024);

            Assert.NotEmpty(cipher);
            Assert.NotEmpty(sign);
            Assert.Equal(data, RsaUtil.Decrypt(cipher, RSA_PRIVATE_KEY_1024));
            Assert.True(RsaUtil.Verify(data, sign, RSA_PUBLIC_KEY_1024));
        }
Пример #5
0
        public IActionResult Code()
        {
            var code = CaptchaUtil.GetRandomEnDigitalText();

            var verifyCode = CaptchaUtil.GenerateCaptchaImage(code);

            RedisServer.Cache.Set($"Captcha:{verifyCode.CaptchaGUID}", verifyCode.CaptchaCode, 1800);

            JObject result = new JObject();

            result.Add("captchaCode", $"data:image/png;base64,{Convert.ToBase64String(verifyCode.CaptchaMemoryStream.ToArray())}");
            result.Add("captchaGUID", verifyCode.CaptchaGUID);

            return(toResponse(result));
        }
Пример #6
0
 public IActionResult Captcha()
 {
     try
     {
         string captcha = CaptchaUtil.GetCaptcha(4);
         byte[] _img    = CaptchaUtil.GetCaptchaImageBytes(captcha, 100, 30);
         HttpContext.Response.Cookies.Append(_cookieCaptchaKey, CaptchaUtil.HashCaptcha(captcha), new CookieOptions
         {
             Expires = DateTime.Now.AddMinutes(1)
         });
         return(File(_img, @"image/jpeg"));
     }
     catch (Exception ex)
     {
         //write log...
     }
     return(null);
 }
Пример #7
0
        public void GET(string key, RequestInfo info)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                info.ReportClientError("Missing token value");
                return;
            }
            else
            {
                string answer = null;
                lock (m_lock)
                {
                    CaptchaEntry tp;
                    m_captchas.TryGetValue(key, out tp);
                    if (tp != null && tp.Expires > DateTime.Now)
                    {
                        answer = tp.Answer;
                    }
                }

                if (string.IsNullOrWhiteSpace(answer))
                {
                    info.ReportClientError("No such entry", System.Net.HttpStatusCode.NotFound);
                    return;
                }

                using (var bmp = CaptchaUtil.CreateCaptcha(answer))
                    using (var ms = new System.IO.MemoryStream())
                    {
                        info.Response.ContentType   = "image/jpeg";
                        info.Response.ContentLength = ms.Length;
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        ms.Position = 0;

                        info.Response.ContentType   = "image/jpeg";
                        info.Response.ContentLength = ms.Length;
                        info.Response.SendHeaders();
                        ms.CopyTo(info.Response.Body);
                        info.Response.Send();
                    }
            }
        }
Пример #8
0
    public static object DoAuth(LoginDat _User)
    {
        //隨機緩衝 1500 ~ 25000 ms;
        Random Rnd = new Random();

        System.Threading.Thread.Sleep(Rnd.Next(500, 1000));

        if (Auth.IsCSRF)
        {
            return(null);
        }

        List <String> _Err = new List <string>();

        if (CaptchaUtil.VerifyCode(_User.Captcha) == false)
        {
            _Err.Add("圖形驗證碼錯誤");
            return(new {
                IsSuccess = false,
                Err = _Err
            });
        }

        if (_User.Id != "eri" || _User.Pwd != "12686505")
        {
            _Err.Add("帳號密碼錯誤");
        }

        //驗證有錯誤強制清除驗證碼重來
        if (_Err.Count > 0)
        {
            CaptchaUtil.Clear();
        }

        Auth.SetLogin();

        return(new {
            IsSuccess = (_Err.Count == 0),
            Err = _Err
        });
    }
Пример #9
0
        public Captcha GenerateCaptcha()
        {
            const string dicString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

            var random = new Random();
            var text   = new char[4];

            for (var i = 0; i < 4; ++i)
            {
                text[i] = dicString[random.Next(0, dicString.Length - 1)];
            }

            var captchaId = Guid.NewGuid();

            MemoryCacheUtil.SetItem(captchaId.ToString(), text);

            return(new Captcha
            {
                Id = captchaId,
                Image = Convert.ToBase64String(CaptchaUtil.CreateCaptcha(new string(text)))
            });
        }
Пример #10
0
        public void POST(string key, RequestInfo info)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                var target = info.Request.Param["target"].Value;
                if (string.IsNullOrWhiteSpace(target))
                {
                    info.ReportClientError("Missing target parameter");
                    return;
                }

                var answer = CaptchaUtil.CreateRandomAnswer(minlength: 6, maxlength: 6);
                var nonce  = Guid.NewGuid().ToString();

                string token;
                using (var ms = new System.IO.MemoryStream())
                {
                    var bytes = System.Text.Encoding.UTF8.GetBytes(answer + nonce);
                    ms.Write(bytes, 0, bytes.Length);
                    ms.Position = 0;
                    token       = Library.Utility.Utility.Base64PlainToBase64Url(Library.Utility.Utility.CalculateHash(ms));
                }

                lock (m_lock)
                {
                    var expired = m_captchas.Where(x => x.Value.Expires < DateTime.Now).Select(x => x.Key).ToArray();
                    foreach (var x in expired)
                    {
                        m_captchas.Remove(x);
                    }

                    if (m_captchas.Count > 3)
                    {
                        info.ReportClientError("Too many captchas, wait 2 minutes and try again", System.Net.HttpStatusCode.ServiceUnavailable);
                        return;
                    }

                    m_captchas[token] = new CaptchaEntry(answer, target);
                }

                info.OutputOK(new
                {
                    token = token
                });
            }
            else
            {
                var answer = info.Request.Param["answer"].Value;
                var target = info.Request.Param["target"].Value;
                if (string.IsNullOrWhiteSpace(answer))
                {
                    info.ReportClientError("Missing answer parameter");
                    return;
                }
                if (string.IsNullOrWhiteSpace(target))
                {
                    info.ReportClientError("Missing target parameter");
                    return;
                }

                if (SolvedCaptcha(key, target, answer))
                {
                    info.OutputOK();
                }
                else
                {
                    info.ReportClientError("Incorrect");
                }
            }
        }
        public void GenerateTest()
        {
            const int LEN = 10;

            Assert.Equal(LEN, CaptchaUtil.GetCaptcha(LEN).Length);
        }