前台验证码后台怎么获取啊

qq_35616418 2016-07-15 06:52:48
<img alt="验证码图片" name="image" id="SecurityCode" title="看不清,点击换一张" src ="<% =Url.Content("~/User/SecurityCode") %>"/>

这个是前台的验证码图片信息

[HttpPost]
public ActionResult Register(Users user, String SecurityCode, String image)
{
//怎么在这里获取验证码的值
if (ModelState.IsValid)
{
if (userManager.selUser(user))
{
ModelState.AddModelError("doubleUser", "当前用户已经被注册");
}
else
{
if (userManager.Register(user))
{
ViewBag.msg = "注册成功";
return View("RegisterResult");
}
else
{
ViewBag.msg = "注册失败";
}
}
}

return View();
}

public ActionResult SecurityCode( ) {
String oldcode = TempData["Securitycode"] as String;
code = CreateRandomCode(5);
TempData["Securitycode"] = code;
ViewData["data"] = code;

return File(CreateValidateGraphic(code), "image/Jpeg");
}
private String CreateRandomCode(int codeCount) {
String allChar = "0,1,2,3,4,5,6,7,8,9,,Q,W,E,R,T,Y,U,I,O,P,A,S,D,F,G,H,J,K,L,Z,X,C,V,B,N,M";
String[] allCharArray = allChar.Split(',');
String randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < codeCount;i++ ) {
if(temp!=-1){
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if(temp == t){
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}

public byte[] CreateValidateGraphic(String vaildateCode) {
Bitmap image = new Bitmap((int)Math.Ceiling(vaildateCode.Length * 16.0), 27);
Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();
g.Clear(Color.White);
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2);
}
Font font = new Font("Arial", 13, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height),
Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(vaildateCode, font, brush, 3, 2);
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
return stream.ToArray();
}
finally {
g.Dispose();
image.Dispose();
}

}
}
}

求大神解答
...全文
443 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_35616418 2016-07-16
  • 打赏
  • 举报
回复
引用 1 楼 q363186 的回复:
你的验证码 是怎么生成的嘛。 打比方 你在某页面 a.aspx 生成验证码 , 生成成功后把验证码 保存起来 保存在COOKIES 里或者 session 里面。 设置个过期时间。 用的时候 判断 session['验证码'] 是否 过期。不过期 的话 就 就跟表单对比下 是否正确!
我的验证码是<img alt="验证码图片" name="image" id="SecurityCode" title="看不清,点击换一张" src ="<% =Url.Content("~/User/SecurityCode") %>"/> 调用后台的动作方法生成的一张图片,就是不知道怎么取图片里面的值
  • 打赏
  • 举报
回复
其实从你的这几行代码
            String oldcode = TempData["Securitycode"] as String;
            code = CreateRandomCode(5);
            TempData["Securitycode"] = code;
            ViewData["data"] = code;
就能看出,它已经给你写出来 asp.net 程序端如何获取(旧的)code 值了。
  • 打赏
  • 举报
回复
引用 2 楼 qq_35616418 的回复:
[quote=引用 1 楼 q363186 的回复:] 你的验证码 是怎么生成的嘛。 打比方 你在某页面 a.aspx 生成验证码 , 生成成功后把验证码 保存起来 保存在COOKIES 里或者 session 里面。 设置个过期时间。 用的时候 判断 session['验证码'] 是否 过期。不过期 的话 就 就跟表单对比下 是否正确!
我的验证码是<img alt="验证码图片" name="image" id="SecurityCode" title="看不清,点击换一张" src ="<% =Url.Content("~/User/SecurityCode") %>"/> 调用后台的动作方法生成的一张图片,就是不知道怎么取图片里面的值[/quote] 图片里边没有值,图片是跟你的 code 变量值而产生的。
楠小南 2016-07-16
  • 打赏
  • 举报
回复
引用 2 楼 qq_35616418 的回复:
[quote=引用 1 楼 q363186 的回复:] 你的验证码 是怎么生成的嘛。 打比方 你在某页面 a.aspx 生成验证码 , 生成成功后把验证码 保存起来 保存在COOKIES 里或者 session 里面。 设置个过期时间。 用的时候 判断 session['验证码'] 是否 过期。不过期 的话 就 就跟表单对比下 是否正确!
我的验证码是<img alt="验证码图片" name="image" id="SecurityCode" title="看不清,点击换一张" src ="<% =Url.Content("~/User/SecurityCode") %>"/> 调用后台的动作方法生成的一张图片,就是不知道怎么取图片里面的值[/quote] 你图片那个值都是 后台的动作方法 给打上去的。你直接 找到后台的动作方法,把验证码给保存起来啊。
楠小南 2016-07-15
  • 打赏
  • 举报
回复
你的验证码 是怎么生成的嘛。 打比方 你在某页面 a.aspx 生成验证码 , 生成成功后把验证码 保存起来 保存在COOKIES 里或者 session 里面。 设置个过期时间。 用的时候 判断 session['验证码'] 是否 过期。不过期 的话 就 就跟表单对比下 是否正确!

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧