我们如何在Captcha MVC中使用区分大小写的字母数字验证码进行普通验证​​码

时间:2017-03-20 02:32:00

标签: c# asp.net-mvc asp.net-mvc-4 captcha

我在https://captchamvc.codeplex.com/的MVC项目中使用部分普通验证码,它工作正常,但我想用区分大小写的字母数字验证码。如何使用区分大小写的字母数字?

部分查看代码

    @model CaptchaMvc.Models.DefaultBuildInfoModel
<img id="@Model.ImageElementId" src="@Model.ImageUrl" />
@Html.Hidden(Model.TokenElementId, Model.TokenValue)
<br />
@{
    string id = Guid.NewGuid().ToString("N");
    string functionName = string.Format("______{0}________()", Guid.NewGuid().ToString("N"));
    <script type="text/javascript">

        $(function () {
            $('#@id').show();
    });


    function @functionName {
            $('#@id').hide();
        $.post("@Model.RefreshUrl", { @Model.TokenParameterName: $('#@Model.TokenElementId').val() },
            function () {
                $('#@id').show();
            });
        return false;
    }
    </script>

    <a href="#@Model.ImageElementId" id="@id" onclick="@functionName" style="display: none;">@Model.RefreshButtonText</a>
}

<br />
@Model.InputText
<br />
@if (Model.IsRequired)
{
    @Html.TextBox(Model.InputElementId, null, new Dictionary<string, object>
                                                  {
                                                      {"data-val", "true"},
                                                      {"data-val-required", Model.RequiredMessage}
                                                  })
}
else
{
    @Html.TextBox(Model.InputElementId)
}
@Html.ValidationMessage(Model.InputElementId)

查看代码:我在哪里渲染局部视图

<div class="form-group">
                            <div class="col-sm-9">                               
                                @Html.TextBoxFor(model => model.LoginID, new { autocomplete = "off", id = "txtbx_EmailID", maxlength = 100, @class = "form-control", @style = "margin-bottom: 2px;width: 232px;", placeholder = Html.DisplayNameFor(model => model.LoginID) })
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-9">
                                @Html.PasswordFor(model => model.Password, new { autocomplete = "off", id = "txtbx_Password", maxlength = 100, @class = "form-control", @style = "margin-bottom: 2px;width: 232px;", placeholder = Html.DisplayNameFor(model => model.Password) })
                            </div>
                        </div>
        <div class="form-group">
                                <div class="col-sm-9">
                                    @Html.Captcha(5, "_DefaultCaptchaView")
                                </div>
                            </div>
    <div class="form-group" style="/*padding-left: 15px; */ margin-top: 2px; margin-bottom: 0px;">
                                <input id="btnSubmit" onclick="PasswordVerification();" name="command" type="submit"
                                    value="Sign in" class="btn btn-success btn-sm  btn01" style="width: 110px;" />
                                &nbsp;
                                @Html.ActionLink("New User", "Registration", "Login", new { @class = "btn btn-default btn-sm btn01" })
                            </div>
                            <div style="color: Red; height: 8px;">                            
                                @{
                                        if (@ViewBag.LoginFailed == true)
                                        {                                
                                    <span>@ViewBag.LoginFailedMessage</span>
                                        }
                                }
                            </div>

控制器操作代码:

[HttpPost, CaptchaVerify("Captcha is not valid")]
        [ValidateAntiForgeryToken]
        public ActionResult Login(Login Lobj, string Command)
        {
            some code to validate login
            return View(Lobj);
        }

1 个答案:

答案 0 :(得分:0)

我在asp.net中使用以下代码制作了字母数字验证码,您可以将此代码用作MVC中的辅助类并制作补丁。

            Bitmap objBitmap = new Bitmap(130, 80);  
            Graphics objGraphics = Graphics.FromImage(objBitmap);  
            objGraphics.Clear(Color.White);  
            Random objRandom = new Random();  
            objGraphics.DrawLine(Pens.Black, objRandom.Next(0, 50), objRandom.Next(10, 30), objRandom.Next(0, 200), objRandom.Next(0, 50));  
            objGraphics.DrawRectangle(Pens.Blue, objRandom.Next(0, 20), objRandom.Next(0, 20), objRandom.Next(50, 80), objRandom.Next(0, 20));  
            objGraphics.DrawLine(Pens.Blue, objRandom.Next(0, 20), objRandom.Next(10, 50), objRandom.Next(100, 200), objRandom.Next(0, 80));  
            Brush objBrush =  
                default (Brush);  
            //create background style  
            HatchStyle[] aHatchStyles = new HatchStyle[]  
            {  
                HatchStyle.BackwardDiagonal, HatchStyle.Cross, HatchStyle.DashedDownwardDiagonal, HatchStyle.DashedHorizontal, HatchStyle.DashedUpwardDiagonal, HatchStyle.DashedVertical,  
                    HatchStyle.DiagonalBrick, HatchStyle.DiagonalCross, HatchStyle.Divot, HatchStyle.DottedDiamond, HatchStyle.DottedGrid, HatchStyle.ForwardDiagonal, HatchStyle.Horizontal,  
                    HatchStyle.HorizontalBrick, HatchStyle.LargeCheckerBoard, HatchStyle.LargeConfetti, HatchStyle.LargeGrid, HatchStyle.LightDownwardDiagonal, HatchStyle.LightHorizontal  
            };  
            //create rectangular area  
            RectangleF oRectangleF = new RectangleF(0, 0, 300, 300);  
            objBrush = new HatchBrush(aHatchStyles[objRandom.Next(aHatchStyles.Length - 3)], Color.FromArgb((objRandom.Next(100, 255)), (objRandom.Next(100, 255)), (objRandom.Next(100, 255))), Color.White);  
            objGraphics.FillRectangle(objBrush, oRectangleF);  
            //Generate the image for captcha  
            string captchaText = string.Format("{0:X}", objRandom.Next(1000000, 9999999));  
            //add the captcha value in session  
            Session["CaptchaVerify"] = captchaText.ToLower();  
            Font objFont = new Font("Courier New", 15, FontStyle.Bold);  
            //Draw the image for captcha  
            objGraphics.DrawString(captchaText, objFont, Brushes.Black, 20, 20);  
            objBitmap.Save(Response.OutputStream, ImageFormat.Gif); 

您可以按照article进行操作,它可以让您全面了解如何实施它。 希望这会帮助你。 感谢。