前台
$('#codeimage').attr("src", "@CommonAddress.SecurityCode?date=" + new Date().toISOString());
后台:
[System.Web.Http.HttpGet]
public void SecurityCode([FromUri]DateTime date)
{
try
{
string code = VeryCodeHelper.GetInstance().CreateRandomCode(4); //验证码的字符为4个
HttpContext.Current.Session[ConstKey.SessionKey] = code; //验证码存放在TempData中
HttpContext.Current.Response.BinaryWrite(VeryCodeHelper.GetInstance().CreateValidateGraphic(code));
HttpContext.Current.Response.End();
}
catch (Exception e)
{
LogError(e);
}
}