asp.net MVC 在Controller控制器中實(shí)現(xiàn)驗(yàn)證碼輸出功能
asp.net mvc項(xiàng)目使用到驗(yàn)證碼,為了讓以前的WebForm代碼能利用上代碼經(jīng)過(guò)稍微的改動(dòng)即可使用代碼如下:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace Angel.Web.Controllers
{
public class CheckCodeController : Controller
{
//
// GET: /CheckCode/
public ActionResult Index()
{
this.CreateCheckCodeImage(GenerateCheckCode());
return View();
}
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 5; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
if (code == '0' || code == 'o' || code == 'L' || code == 'I')
{
i = i - 1;
}
else
{
checkCode += code.ToString();
}
}
// Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
Session.Contents["checkcode"] = checkCode;
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成隨機(jī)生成器
Random random = new Random();
//清空?qǐng)D片背景色
g.Clear(Color.White);
//畫(huà)圖片的背景噪音線
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, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//畫(huà)圖片的前景噪音點(diǎn)
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()));
}
//畫(huà)圖片的邊框線
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
}
最后別忘了session的獲取設(shè)置,需要在Global.asax.cs文件中新增如下代碼:
/// <summary>
/// MVC為了獲取session參數(shù)
/// </summary>
public override void Init()
{
PostAuthenticateRequest += (s, e) => HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
base.Init();
}
void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
{
HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}
html頁(yè)面代碼:
html代碼
<img name="img1" id="img1" style="position:absolute;top:5px;right:36px!important;z-index:1000;" alt="單擊圖片刷新驗(yàn)證碼" src="CheckCode/Index" <br>onclick="JavaSccript:reloadImage('CheckCode/Index');" /><br><script type="text/javascript">
function reloadImage(url) {
document.getElementById("img1").src = url + '?abc=' + Math.random();
}
</script>
總結(jié)
以上所述是小編給大家介紹的asp.net MVC 在Controller控制器中實(shí)現(xiàn)驗(yàn)證碼輸出功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
asp.net中ADO SQL數(shù)據(jù)庫(kù) 筆記匯總 持續(xù)更新中
asp.net中ADO SQL數(shù)據(jù)庫(kù) 筆記匯總 持續(xù)更新中,需要的朋友可以參考下2012-07-07
分享一個(gè)取自HoverTree項(xiàng)目的.NET分頁(yè)類(lèi)
分頁(yè)顯示是一種非常常見(jiàn)的瀏覽和顯示大量數(shù)據(jù)的方法,屬于web編程中最常處理的事件之一。對(duì)于web編程的老手來(lái)說(shuō),編寫(xiě)這種代碼實(shí)在是和呼吸一樣自然,但是對(duì)于初學(xué)者來(lái)說(shuō),常常對(duì)這個(gè)問(wèn)題摸不著頭緒,今天我們給大家分享一個(gè)取自HoverTree項(xiàng)目的.NET分頁(yè)類(lèi)。2015-04-04
ASP.NET中Application全局對(duì)象用法實(shí)例淺析
這篇文章主要介紹了ASP.NET中Application全局對(duì)象用法,較為詳細(xì)的分析了ASP.NET中Application全局對(duì)象的功能、定義及使用中的相關(guān)注意事項(xiàng),需要的朋友可以參考下2015-06-06
此頁(yè)的狀態(tài)信息無(wú)效,可能已損壞 的處理辦法及原因分析
此頁(yè)的狀態(tài)信息無(wú)效,可能已損壞 的處理辦法及原因分析,需要的朋友可以參考一下2013-06-06
ASP.NET 根據(jù)漢字獲取漢字拼音的首字母(含多音字)
本文分享了一個(gè)函數(shù),這個(gè)函數(shù)可以根據(jù)漢字的字符串獲取其拼音的首字母,以便我們?cè)趯?shí)際開(kāi)發(fā)中使用。2016-04-04
注冊(cè)或者點(diǎn)擊按鈕時(shí),怎么防止用戶(hù)重復(fù)提交數(shù)據(jù)(實(shí)例講解)
這篇文章主要是對(duì)注冊(cè)或者點(diǎn)擊按鈕時(shí),怎么防止用戶(hù)重復(fù)提交數(shù)據(jù)進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12

