MVC Captcha和会话是不同的

时间:2016-09-01 07:55:42

标签: jquery html asp.net-mvc session captcha

我想在我编写的LoginController中使用验证码验证登录页面

[HttpGet]
        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")] // This is for output cache false
        public FileResult GetCaptchaImage()
        {
            CaptchaRandomImage CI = new CaptchaRandomImage();
            this.Session["CaptchaImageText"] = CI.GetRandomString(5); // here 5 means I want to get 5 char long captcha
            //CI.GenerateImage(this.Session["CaptchaImageText"].ToString(), 300, 75);
            // Or We can use another one for get custom color Captcha Image 
            CI.GenerateImage(this.Session["CaptchaImageText"].ToString(), 300, 75, Color.DarkGray, Color.White);
            MemoryStream stream = new MemoryStream();
            CI.Image.Save(stream, ImageFormat.Png);
            stream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(stream, "image/png");
        }

在html部分我写了

 <span class="input-group-addon"style="width: 20%; background: transparent;border: none"><img src="@Url.Action("GetCaptchaImage","Login")" style="width:80px; height: 45px;margin-left: -12px" /></span>

问题是会话和图像不相等,因为登录页面加载时,首先验证码工作然后页面工作,所以始终会话存储以前的图像代码。我怎么解决这个问题? 谢谢

1 个答案:

答案 0 :(得分:0)

首先wiev开始,然后开始,你的代码是正确的但你应该在控制器中执行它,而不是在视图中,你应该在你的post动作中添加一个参数,即authcode,然后在控制器中你可以检查和做。