MVC 验证码和会话是不同的
MVC Captcha and session are different
我想在我编写的 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>
问题是会话和图像不相等,因为当登录页面加载时,首先验证码工作然后页面工作,所以会话总是存储以前的图像代码。我怎么解决这个问题?
谢谢
首先启动 wiev,然后启动另一个,你的代码是正确的,但你应该在控制器中执行,而不是在视图中,你应该在你的 post 操作中添加一个参数,即 authcode,然后在控制器中你可以检查并做。
我想在我编写的 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>
问题是会话和图像不相等,因为当登录页面加载时,首先验证码工作然后页面工作,所以会话总是存储以前的图像代码。我怎么解决这个问题? 谢谢
首先启动 wiev,然后启动另一个,你的代码是正确的,但你应该在控制器中执行,而不是在视图中,你应该在你的 post 操作中添加一个参数,即 authcode,然后在控制器中你可以检查并做。