1,我們在登陸中經常會遇到訪問系統的某個頁面無法登陸,需要走登陸頁面,儘管訪問的不是登陸頁面,那麼這是怎麼做的呢?以及在登陸中有記住我以及驗證碼的輸入功能。 今天就寫了這麼一個demo關於登陸驗證 驗證碼,以及記住我功能呢 2,在這中主要用到了Session 和Cookies,Cookies 存儲客 ...
1,我們在登陸中經常會遇到訪問系統的某個頁面無法登陸,需要走登陸頁面,儘管訪問的不是登陸頁面,那麼這是怎麼做的呢?以及在登陸中有記住我以及驗證碼的輸入功能。
今天就寫了這麼一個demo關於登陸驗證 驗證碼,以及記住我功能呢
2,在這中主要用到了Session 和Cookies,Cookies 存儲客戶的登陸信息,Session存取驗證碼信息
登錄頁:Login.aspx
<script>
function checkcode() {
var vcode = document.getElementById("checkcode");
vcode.src = vcode.src + '1';
}
window.onload = function () {
var did = document.getElementById("width").style.width;
did = window.innerWidth - 100;
}
</script>
<form id="form1" runat="server" method="post">
用戶名:<input type="text" name="username" value="<%=username %>" />
<br />
<br />
密 碼:<input type="password" name="upwd" value="<%=upwd %>" />
<br />
驗證碼:<input type="text" name="Vcode" />
<img src="ValidateCode.ashx?1" id="checkcode" />
<a href="Javascript:checkcode()">看不清,換一張</a>
<br />
<br />
<div id="width">
<div style="margin: 0 auto">
記住我:<input type="checkbox" name="echeck" />
</div>
</div>
<br />
<input type="submit" value="登陸" />
</form>
登陸處理:Login.aspx.cs
public partial class Login : System.Web.UI.Page
{
protected string username { get; set; }
protected string upwd { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
string vcode=Request["Vcode"];
//頁面第一次載入,如果驗證碼為空,取緩存
if (string.IsNullOrEmpty(vcode))
{
HttpCookie cooike = Request.Cookies["jun"];
if (cooike!= null) {
string uinfo = Encoding.UTF8.GetString(Convert.FromBase64String(cooike.Value));
username = uinfo.Split('_')[0];
upwd = uinfo.Split('_')[1];
}
}
else {
if (vcode != null)
{
if (vcode.Equals(Session["vcode"].ToString(), StringComparison.CurrentCultureIgnoreCase))
{
//驗證碼正確
string sql = "select count(*) from tb_Userinfo where userName=@uname and userpwd=@upwd";
SqlParameter[] ps = new SqlParameter[] {
new SqlParameter("@uname",Request["username"]),
new SqlParameter("@upwd",Request["upwd"])
};
int count = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, ps));
//用戶信息正確
if (count > 0)
{
//如果勾中記住我
if (!string.IsNullOrEmpty(Request["echeck"]))
{
//登陸成功,存取Cookies信息
string uinfo = Request["username"] + "_" + Request["upwd"];
//轉成64位編碼
uinfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(uinfo));
HttpCookie cooike = new HttpCookie("jun", uinfo)
{
Expires = DateTime.Now.AddDays(14)
};
Response.Cookies.Add(cooike);
}
Session["userinfo"] = 1;
Response.Redirect("Index.aspx");
}
else
{
Response.Write("用戶名或密碼錯誤");
}
}
else
{
Response.Write("驗證碼錯誤");
}
}
}
}
}
成功後轉到Index頁面 轉到頁面首先走Index繼承的SessionStatus類 我們在這裡面封裝一個方法
protected void p_load(object sender, EventArgs e)
{
if (Session["userinfo"] == null)
{
Response.Redirect("Login.aspx");
}
}
在Login.aspx.cs頁面先存儲Session["userinfo"]=1 這樣就能完成非登陸頁驗證是否合法登陸了
驗證碼我主要引用一個一般處理程式:ValidateCode.ashx
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
CreateImage(CreateRandomCode(4)).Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
//產生隨機碼
private string CreateRandomCode(int iLength)
{
int rand;
char code;
string randomCode = String.Empty;
//生成一定長度的驗證碼
System.Random random = new Random();
for (int i = 0; i < iLength; i++)
{
rand = random.Next();
if (rand%3==0)
{
code = (char)('A' + (char)(rand % 26));
}
else
{
code = (char)('0'+(char)(rand%10));
}
randomCode += code.ToString();
}
Console.WriteLine("--------------------------"+HttpContext.Current.Session.SessionID);
HttpContext.Current.Session["vcode"] = randomCode;
return randomCode;
}
//創建隨機圖片
private Image CreateImage(string strVerifyCode)
{
Bitmap map;
try
{
int iRandAngle = 45; //隨機轉動角度
int iMapWidth = (int)(strVerifyCode.Length * 21);
map = new Bitmap(iMapWidth, 28); //創建圖片背景
Graphics graph = Graphics.FromImage(map);
graph.Clear(Color.AliceBlue);//清除畫面,填充背景
graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//模式
Random rand = new Random();
//背景噪點生成
Pen blackPen = new Pen(Color.LightGray, 0);
for (int i = 0; i < 50; i++)
{
int x = rand.Next(0, map.Width);
int y = rand.Next(0, map.Height);
graph.DrawRectangle(blackPen, x, y, 1, 1);
}
//驗證碼旋轉,防止機器識別
char[] chars = strVerifyCode.ToCharArray();//拆散字元串成單字元數組
//文字距中
StringFormat format = new StringFormat(StringFormatFlags.NoClip);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
//定義顏色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
//定義字體
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" };
for (int i = 0; i < chars.Length; i++)
{
int cindex = rand.Next(7);
int findex = rand.Next(5);
Font f = new System.Drawing.Font(font[findex], 13, System.Drawing.FontStyle.Bold);//字體樣式(參數2為字體大小)
Brush b = new System.Drawing.SolidBrush(c[cindex]);
Point dot = new Point(16, 16);
float angle = rand.Next(-iRandAngle, iRandAngle);//轉動的度數
graph.TranslateTransform(dot.X, dot.Y);//移動游標到指定位置
graph.RotateTransform(angle);
graph.DrawString(chars[i].ToString(), f, b, 1, 1, format);
graph.RotateTransform(-angle);//轉回去
graph.TranslateTransform(2, -dot.Y);//移動游標到指定位置
}
graph.DrawRectangle(new Pen(Color.Black, 0), -chars.Length * 18, 0, map.Width - 1, map.Height - 1);//畫一個邊框
}
catch (ArgumentException ex)
{
map = null;
throw ex;
}
return map;
}
public bool IsReusable
{
get
{
return false;
}
}