·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> net项目总结一(1)

net项目总结一(1)

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

net项目总结一(1)

中小型新闻发布系统

代码结构:分为实体层,数据层与接口,数据工厂层,业务逻辑层,公共层,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)