日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET 实现登录界面(生成验证码)

發(fā)布時間:2023/12/18 asp.net 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET 实现登录界面(生成验证码) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這周末也沒干啥,真正開始ASP,做了個學籍管理系統(tǒng)的登錄界面,登錄界面主要包括用戶名、密碼、驗證碼,界面字體用了<font size="5" color="blue" font-family:"華文琥珀";></font>改變字體,生成驗證碼控件 ImageButton? (例如:<asp:ImageButton ID="ImageButton1" runat="server" />)
?還學會了圖片按鈕 HyperLink?(例如: <asp:HyperLink? ID="HyperLink1" runat="server" Text="link to Default" NavigateUrl="~/Default.aspx" ImageUrl="~/Image/1.gif"></asp:HyperLink>)
其中我覺得很難的就是驗證碼的生成,你首先在站點中添加pivture.aspx 的窗體,打開代碼頁面(即pivture.aspx.cs)并錄入下面代碼(完整的)
Code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.IO;

public partial class Picture : System.Web.UI.Page
{
??? Random ran = new Random();
??? protected void Page_Load(object sender, EventArgs e)
??? {
??????? string str = getRandomValidate(4);
??????? Session["CheckCode"] = str;//這一部是Wie了驗證碼寫入Session,進行驗證,也可以使用cookie
??????? getImageValidate(str);??
??? }
??? //得到隨機字符串,長度自定義
??? private string getRandomValidate(int len)
??? {
??????? int num;
??????? int tem;
??????? string rtuStr="";
??????? for (int i = 0; i < len;i++ )
??????? {
??????????? num = ran.Next();
??????????? tem = num % 10 + '0';//生成數(shù)字
??????????? //tem = num % 26 + 'A';//生成字符
??????????? rtuStr += Convert.ToChar(tem).ToString();

??????? }
??????? return rtuStr;
??? }
??? //生成圖像
??? private void getImageValidate(string strValue)
??? {
??????? //string str=oo00;前兩個為字母o,后兩個數(shù)為0
??????? int width = Convert.ToInt32(strValue.Length*12);
??????? Bitmap img = new Bitmap(width,23);
??????? Graphics gfc = Graphics.FromImage(img);
??????? gfc.Clear(Color.White);
??????? drawLine(gfc,img);
??????? //寫驗證碼,要定義Font
??????? Font font = new Font("arial",12,FontStyle.Bold);
??????? //Font font = new Font("宋體",12,FontStyle.Bold|FontStyle.Italic);
??????? System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0,0,img.Width,img.Height),Color.DarkOrchid,Color.Blue,1.5f,true);
??????? gfc.DrawString(strValue,font, brush ,3,2);
??????? drawPoint(img);
??????? gfc.DrawRectangle(new Pen(Color.DarkBlue),0,0,img.Width-1,img.Height-1);
??????? //將圖像添加到頁面
??????? MemoryStream ms = new MemoryStream();
??????? img.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
??????? //更改HTTP
??????? Response.ClearContent();
??????? Response.ContentType = "image/gif";
??????? Response.BinaryWrite(ms.ToArray());
??????? //Dispose
??????? gfc.Dispose();
??????? img.Dispose();
??????? Response.End();


??? }

??? private void drawLine(Graphics gfc,Bitmap img)
??? {
??????? //選擇畫10條線,也可以增加,也可以不要線,只要隨機雜點就行
??????? for (int i = 0; i < 10;i++ )
??????? {
??????????? int x1 = ran.Next(img.Width);
??????????? int y1 = ran.Next(img.Height);
??????????? int x2 = ran.Next(img.Width);
??????????? int y2 = ran.Next(img.Height);
??????????? gfc.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);//注意畫筆要淡,不然看不清
??????? }
?
??? }

??? //private void drawPoint(Bitmap img)
??? //{
???
??? //}

??? private void drawPoint(Bitmap img)
??? {
??????? int col = ran.Next();//在一次的圖片中雜點顏色相同
??????? for (int i = 0; i < 100; i++)
??????? {
??????????? int x = ran.Next(img.Width);
??????????? int y = ran.Next(img.Height);
??????????? img.SetPixel(x,y,Color.FromArgb(col));
??????? }
??? }
??
}

錄完后,需要在 登錄界面顯示驗證碼,則需要在
登錄界面的代碼文件 Page_Load 中加入? ImageButton1.ImageUrl = "~/Picture.aspx";
其登陸頁面的代碼Default.aspx.cs中的代碼為
Code
using?System;
using?System.Configuration;
using?System.Data;
using?System.Linq;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.HtmlControls;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Xml.Linq;

public?partial?class?_Default?:?System.Web.UI.Page?
{
????
protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????ImageButton1.ImageUrl?
=?"~/Picture.aspx";
????????
if?(Session["CheckCode"]?==?null)
????????????Information.Text?
=?"對不起,驗證碼生成錯誤!";
????}

????
protected?void?OK_Click(object?sender,?EventArgs?e)
????{
????????
if?(UserName.Text.Length?==?0)
????????????Information.Text?
=?"請輸入用戶名!";
????????
else
????????{
????????????
if(Password.Text.Length?==0)
????????????????Information.Text?
=?"請輸入密碼!";
????????????
else?if?(CheckCode.Text.ToString()?!=?Session["CheckCode"].ToString())
????????????{
????????????????Information.Text?
=?"對不起,驗證碼不正確,請重新輸入!";
????????????}
????????????
else?if?(UserName.Text?==?"Admin"?&&?Password.Text?==?"Admin"?&&?CheckCode.Text.ToString()?==?Session["CheckCode"].ToString())
????????????{
????????????????Information.Text?
=?"用戶:"?+?UserName.Text?+?"登錄成功!";
????????????????Response.Redirect(
"~/Default2.aspx");
????????????}
????????????
else
????????????????Information.Text?
=?"用戶不存在或密碼不正確!";
????????}
????}

????
protected?void?Cancel_Click(object?sender,?EventArgs?e)
????{
????????Information.Text?
=?"已退出登錄!"?;
????}

}

轉(zhuǎn)載于:https://www.cnblogs.com/yoyiorlee/archive/2009/09/20/1570631.html

總結(jié)

以上是生活随笔為你收集整理的ASP.NET 实现登录界面(生成验证码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。