有人说asp.net很简单,其实呢这只是表面现象。要把asp.net学的很深真的是一件不容易的事情,从开发的角度来说asp.net要方便一些,只是移植性差了一些。学过java之后再来学习就会觉得很简单,很快就可以上手。
下面就接着前面的博客写一个权限控制和内置对象相结合的小例子,这个例子在学校里可谓经典。所以我就借这个小例子来弄下。
1.开发一个用户登录表单,这里只有登录后的用户才能进入下载页面
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="downloadfile._Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>用户登录</title>
- </head>
- <body>
- <form id="form1" runat="server" action="ashx/ToLogin.ashx">
- 用户名:<input type="text" name="fname"/><br /><br/>
- 密码:<input type="password" name="password"/><br /><br/>
- <input type="submit" value="登录"/>
- <input type="reset" value="取消"/>
- </form>
- </body>
- </html>
2.新建一个数据库文件,并创建一个数据表
3.添加一个数据集,后面会使用类型化dataset来查询数据库
4.编写一般处理程序处理用户登录
- using System;
- using System.Collections;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.Xml.Linq;
- using System.Web.SessionState;
- using downloadfile.DataSetTeacherTableAdapters;
- namespace downloadfile.ashx
- {
- /// <summary>
- /// $codebehindclassname$ 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- public class ToLogin : IHttpHandler, IRequiresSessionState
- {
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
- string fname = context.Request["fname"];
- string password = context.Request["password"];
- m_teacherTableAdapter adpater = new m_teacherTableAdapter();
- var data = adpater.GetDataByFname(fname);
- var single = data.Single();
- if (data.Count <= 0)
- {
- context.Response.Write("用户名不存在");
- }
- else {
- //判断数据的唯一性,防止出现未知错误
- //如果用户输入的密码和数据库的相同,则可以登录成功
- if (single.fpassword.Equals(password))
- {
- context.Session["status"] = "login";
- context.Session["username"] = fname;
- context.Session["password"] = password;
- context.Session["userid"] = single.fid;
- //context.Response.Write("登录成功");
- context.Response.Redirect("../manage/allpicture.htm");
- }else{
- context.Response.Redirect("../Default.aspx");
- }
- }
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
- }
5.编写图片的下载页面
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head>
- <title>图片下载页面</title>
- </head>
- <body>
- <center>
- <div>
- <a href="../ashx/Download.ashx?filename=1.jpg">1.jpg</a>
- <a href="../ashx/Download.ashx?filename=2.jpg">2.jpg</a>
- <a href="../ashx/Download.ashx?filename=3.jpg">3.jpg</a>
- <a href="../ashx/Download.ashx?filename=4.jpg">4.jpg</a>
- </div>
- </center>
- </body>
- </html>
6.编写下载的处理程序,如果是普通用户则下载的图片加水印
- using System;
- using System.Collections;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.Xml.Linq;
- using downloadfile.DataSetTeacherTableAdapters;
- using System.Web.SessionState;
- using System.Drawing;
- using System.Drawing.Imaging;
- namespace downloadfile.ashx
- {
- /// <summary>
- /// $codebehindclassname$ 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- public class Download : IHttpHandler,IRequiresSessionState
- {
- public void ProcessRequest(HttpContext context)
- {
- //如果用户已经
- string status = (string)context.Session["status"];
- if (status!=null)
- {
- //取得文件名称
- string filename=context.Request["filename"];
- //设置消息头用来下载文件
- context.Response.ContentType="application/octet-stream";
- context.Response.AddHeader("Content-Disposition", string.Format("p_w_upload;filename=\"{0}\"", filename));
- //从session取得登录用户的fid
- int fid=(int)context.Session["userid"];
- //根据用户id查询该用户的级别,根据级别获得不同的下载
- //数据集
- //通过用户的id查询用户的级别
- m_teacherTableAdapter adpater = new m_teacherTableAdapter();
- var data = adpater.GetDataById(fid);
- var single = data.Single();
- if (single.level == 1)
- {
- //context.Response.WriteFile("../p_w_picpath/" + filename);
- using (Bitmap bitmap = new Bitmap(context.Server.MapPath("../p_w_picpath/" + filename)))
- {
- using (Graphics gra = Graphics.FromImage(bitmap))
- {
- gra.DrawString("免费下载", new Font("宋体", 20), Brushes.Red, 0, 0);
- }
- bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
- }
- }
- else {
- context.Response.WriteFile("../p_w_picpath/" + filename);
- }
- }
- else
- {
- context.Response.Write(status);
- //context.Response.Write("请先登录");
- }
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
- }
7.程序的运行效果