中小型新聞發布系統
代碼結構:分為實體層,數據層與接口,數據工廠層,業務邏輯層,公共層,UI層(由于圖片上傳實在麻煩,所以只上傳少量而已),項目中用到了工廠模式,解耦BLL層和DLL層

1、登錄功能,記住三天功能,basepage中統一驗證
1、做驗證碼,利用自定義一般處理程序類來實現
2、利用cookie實現記住三天狀態的功能,實現免登錄功能(如果在公共環境不建議使用)
3、統一登錄驗證
4、實現統一錯誤頁面處理 ,全局應用程序文件中的application_Error()中實現
2、引入必要的js
3、
登陸頁面中取消按鈕的制作:
<td>
<input type="button" value="登錄" onclick="login()" />
<input type="button" value="取消" onclick="resetfm()" />
</td>
//4.0 負責清除當前表單中的所有帶有name屬性的控件值
function resetfm() {
document.getElementById("form1").reset();
}
4、建立驗證碼
4.1實現驗證碼邏輯:
/// <summary>
/// 實現驗證碼生成的一般處理程序類,記得在web.config中按照iis集成和經典模式做相應的配置
/// 由于此類中要使用session,所以必須實現接口IRequiresSessionState
/// </summary>
public class Vcode : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public bool IsReusable
{
get { return false; }
}
public void PRocessRequest(HttpContext context)
{
//實現驗證碼功能
//1.0 產生驗證碼字符串
string vcode = GetVcode(4);
//2.0 將驗證碼字符串存入session
context.Session[Keys.Vcode] = vcode;
//3.0 將驗證碼字符串以圖片的形式響應給瀏覽器
using (Image img = new Bitmap(65, 25))
{
//3.0.1 定義一個畫家
using (Graphics g = Graphics.FromImage(img))
{
//3.0.2 利用畫家對象在圖片上將驗證碼字符串畫上去
g.Clear(Color.White);
g.DrawRectangle(Pens.Red, 0, 0, img.Width - 1, img.Height - 1);
g.DrawString(vcode, new Font("黑體", 16, FontStyle.Bold | FontStyle.Strikeout), new SolidBrush(Color.Blue), 4, 4);
}
//3.0.3 利用圖片的save方法將圖片流保存到outputstream中
img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
Random r = new Random();
private string GetVcode(int num)
{
string[] arrcode = { "a", "b", "c", "d", "4", "2", "3" };
int arrLeng = arrcode.Length;
string res = string.Empty;
for (int i = 0; i < num; i++)
{
res += arrcode[r.Next(arrLeng)];
}
return res;
}
}
4.2寫好了驗證碼的類,那么需要在配置文件中添加配置
<system.webServer>
<handlers>
<add name="vcode" path="*.vcode" verb="*" type="EMS12.Site.admin.Vcode" />
</handlers>
</system.webServer>
4.3驗證碼調用示例
<td>
<input type="text" id="vcode" name="vcode" />
<img id="imgvcode" style="cursor: pointer" src="vcode.vcode" alt="驗證碼" height="25px" width="65px" />
</td>
4.4驗證碼點擊替換(注意此時的math.random的用法)
//2.0 點擊驗證碼圖片的時候進行新的請求
$("#imgvcode").click(function () {
reflushvcode();
});
function reflushvcode() {
//this.src = "vcode.vcode?rid=" + Math.random(); //js的寫法
$("#imgvcode").attr("src", "vcode.vcode?rid=" + Math.random()); //JQ的寫法
}
5、利用Ajax進行登陸驗證。
5.1 //3.0 利用jquery ajax實現登錄處理過程
function login() {
//1.0 獲取表單form1中的所有帶有name屬性的參數的鍵值對
var parms = $("#form1").serialize(); //uname=?&pwd=?&vcode=?
//2.0 利用$.post方法將parms參數提交給 /actions/admin/login.ashx
$.post("/actions/admin/login.ashx", parms, function (ajaxobj) {
if (ajaxobj.status == "1") {
msgbox.showMsgErr(ajaxobj.msg, function () {
// 刷新頁面
//window.location = window.location;
//刷新驗證碼
reflushvcode();
});
} else {
//登錄成功
msgbox.showMsgOk(ajaxobj.msg, function () {
window.location = "index.aspx";
});
}
}, "json")
}
5.2接著開始撰寫登陸驗證一般處理程序(處理步驟是什么?)
using EMS12.BusinessLogicLayer;
using EMS12.Entity;
using EMS12.Common;//需要的是里面的key,一段加密驗證
/// <summary>
/// login 的摘要說明
/// </summary>
public class login : BaseHandler, System.Web.SessionState.IRequiresSessionState //繼承兩個接口,第二個接口來實現session
{
public override void SubPR()
{
Response.ContentType = "text/plain";
try
{
//開始登錄邏輯編寫
//1.0 接收參數
string uname = Request.Form["uname"];
string pwd = Request.Form["pwd"];
string vcode = Request.Form["vcode"];
//2.0 驗證碼的合法性驗證
string vcodeFromSession = string.Empty;
if (Session[Keys.Vcode] != null)
{
vcodeFromSession = Session[Keys.Vcode].ToString();
}
if (string.IsNullOrEmpty(vcode)
|| vcode.Equals(vcodeFromSession, StringComparison.OrdinalIgnoreCase) == false)
{
WriteError("驗證碼錯誤");
// Response.End(); //此時會將當前處理現場強制終止,一定會拋出一個異常
return;
}
//3.0 驗證用戶名和密碼的合法性
if (string.IsNullOrEmpty(uname) || string.IsNullOrEmpty(pwd))
{
WriteError("用戶名或者密碼不能為空");
return;
}
string md5pwd = Kits.MD5Entry(pwd);
UserInfoEntity entity = UserInfo_BLLSub.Login(uname, md5pwd);
if (entity == null)
{
WriteError("用戶名或者密碼錯誤");
return;
}
//4.0 將用戶實體存入session[uinfo]
Session[Keys.uinfo] = entity;
//5.0 將ajaxobj對象序列化成json字符串返回
WriteSuncess("登錄成功,正在跳轉到首頁....");
}
catch (Exception ex)
{
WriteError(ex.Message);
}
}
}
6.附加的kits。就是common下的加密驗證
using System.Text.RegularExpressions;
/// <summary>
/// 幫助類
/// </summary>
public class Kits
{
/// <summary>
/// 判斷當前字符串是否為一個數字
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsInt(string str)
{
int res = 0;
return int.TryParse(str, out res);
}
/// <summary>
/// 判斷當前字符串是否為一個數字
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsNumber(string str)
新聞熱點
疑難解答