MS Captcha timeout duration in registration form in ASP.Net

老子叫甜甜 提交于 2019-12-10 21:11:35

问题


I am currently using MS Captcha in a registration form. It works perfectly if the form is submitted within a minute. But sometimes, after filling in the form the user then searches for documents to upload, and when they finally submit the form, they get server error as below:

[NullReferenceException: Object reference not set to an instance of an object.] MSCaptcha.CaptchaControl.ValidateCaptcha(String userEntry) +438

On submit button click, I call the ValidateCaptcha as below:

Captcha1.ValidateCaptcha(txtCaptcha.Text.Trim());

Can someone help me in handling this exception? Thanks in advance.


回答1:


It has been seen that if you have not set ErrorInputTooFast and ErrorInputTooSlow messages the code throws 'NullReferenceException' when the CaptchaMaxTimeout time period has passed.

I have set the following properties as well to get this working without NullReferenceException

ErrorInputTooFast="Image text was typed too quickly." ErrorInputTooSlow="Image text was typed too slowly."

my implementation is as below and i have set CaptchaMaxTimeout as 20 sec to see if this issue persists.

<uc:CaptchaControl ID="CaptchaUserControl" runat="server" Height="50px" ValidationGroup="PageValidationGroup"
                                        CustomValidatorErrorMessage="The text you entered did not match up with the image provided"
                                        Width="180px" CaptchaLength="5" FontColor="#000000" BackColor="#e6db55" NoiseColor="#26557f"
                                        CaptchaLineNoise="None" CaptchaFontWarping="Low" ImageTag="border='1'" CaptchaBackgroundNoise="Medium"
                                        ErrorInputTooFast="Image text was typed too quickly. " ErrorInputTooSlow="Image text was typed too slowly."
                                        CaptchaMaxTimeout="20" CaptchaMinTimeout="2" EnableViewState="False" />
                                   <asp:TextBox ID="CapthaTextBox" runat="server" MaxLength="10" Width="180px"  AutoCompleteType="Disabled"/>

code behind

 private void AppendValidationErrorMessage( string message)
    {
        var cv = new CustomValidator(); 
        cv.IsValid = false;
        cv.ErrorMessage = message;
        cv.ValidationGroup = "PageValidationGroup";
        this.Page.Validators.Add(cv);
    }

    protected void SubmitButtonClick(object sender, EventArgs e)
    {
        try
        {
            this.CaptchaUserControl.ValidateCaptcha(CapthaTextBox.Text.GetTrimValue());
            if (!this.CaptchaUserControl.UserValidated)
            {
                this.AppendValidationErrorMessage(this.CaptchaUserControl.CustomValidatorErrorMessage);
            }
        }
        catch (Exception)
        {
            this.AppendValidationErrorMessage(
                "Captcha expired please please reload the page.Note: please copy the data before refreshing data");
        }
        this.CapthaTextBox.Text = string.Empty;
        if (this.Page.IsValid) //&& this.CaptchaUserControl.UserValidated
        {
            //do something

        }
    }


来源:https://stackoverflow.com/questions/17232819/ms-captcha-timeout-duration-in-registration-form-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!