62,046
社区成员
发帖
与我相关
我的任务
分享
<img src="/register/GetValidateCode?type=mail&abc=0.23153452345" />
<img src="/register/GetValidateCode?type=phone&abc=0.4567457648678" />
public ActionResult GetValidateCode(string type)
{
if (type == "mail")
{
mengma.AppCode.ValidateCode v = new mengma.AppCode.ValidateCode();
1 string code = v.CreateVerifyCode();
2 Response.Cookies["MailChkCode"].Value = code.ToLower();
byte[] bytes = v.CreateImageOnPage(code);
return File(bytes, @"image/jpeg");
}
if (type == "phone")
{
mengma.AppCode.ValidateCode v = new mengma.AppCode.ValidateCode();
3 string code = v.CreateVerifyCode();
4 Response.Cookies["PhoneChkCode"].Value = code.ToLower();
byte[] bytes = v.CreateImageOnPage(code);
return File(bytes, @"image/jpeg");
}
return null;
}
public string CreateVerifyCode(int codeLen)
{
if (codeLen == 0)
{
codeLen = Length;
}
string[] arr = CodeSerial.Split(',');
string code = "";
int randValue = -1;
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < codeLen; i++)
{
randValue = rand.Next(0, arr.Length - 1);
code += arr[randValue];
}
return code;
}
$(function () {
$("#code").attr("src", "/register/GetValidateCode?type=mail&abc=" + Math.random());
console.log(Math.random());
$("#phonecode").attr("src", "/register/GetValidateCode?type=phone&abc=" + Math.random());
console.log(Math.random());
});
public ActionResult GetValidateCodeByMail(string type)
{
mengma.AppCode.ValidateCodeByMail v = new mengma.AppCode.ValidateCodeByMail(); //把类复制成了两个类,ByMail和ByPhone
string code = v.CreateVerifyCode();
Response.Cookies["MailChkCode"].Value = code.ToLower();
byte[] bytes = v.CreateImageOnPage(code);
return File(bytes, @"image/jpeg");
}
public ActionResult GetValidateCodeByPhone(string type)
{
mengma.AppCode.ValidateCodeByPhone v = new mengma.AppCode.ValidateCodeByPhone(); //把类复制成了两个类,ByMail和ByPhone
string code = v.CreateVerifyCode();
Response.Cookies["PhoneChkCode"].Value = code.ToLower();
byte[] bytes = v.CreateImageOnPage(code);
return File(bytes, @"image/jpeg");
}
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
public ActionResult GetValidateCode(string type)
{
......
}
这样不行吗?public ActionResult GetValidateCode(string type)
{
string CodeSerial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
if (type == "mail")
{
mengma.AppCode.ValidateCode v = new mengma.AppCode.ValidateCode();
string[] arr = CodeSerial.Split(',');
string code = "";
int randValue = -1;
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < 4; i++)
{
randValue = rand.Next(0, arr.Length - 1);
code += arr[randValue];
}
Response.Cookies["MailChkCode"].Value = code.ToLower();
byte[] bytes = v.CreateImageOnPage(code);
return File(bytes, @"image/jpeg");
}
if (type == "phone")
{
mengma.AppCode.ValidateCode v = new mengma.AppCode.ValidateCode();
string[] arr = CodeSerial.Split(',');
string code = "";
int randValue = -1;
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < 4; i++)
{
randValue = rand.Next(0, arr.Length - 1);
code += arr[randValue];
}
Response.Cookies["PhoneChkCode"].Value = code.ToLower();
byte[] bytes = v.CreateImageOnPage(code);
return File(bytes, @"image/jpeg");
}
return null;
}