基于#的在线图片库应用示例
MSDE 2000 Walkthrough: Build a Data-Driven Website Using Visual C# .NET and Visual Studio .NET 2003
原:Brian A. Randell
应用于:Microsoft? ASP.NET 1.1
Microsoft SQL Server? 2000 Desktop Engine (MSDE)
Microsoft Visual C# .NET
Microsoft Visual Studio .NET 2003
要求:下载示例代码CSVSSupport.exe(http://download.microsoft.com/download/7/0/f/70f286f1-eeae-4901-a23e-5b3e45009069/CSVSSupport.exe)
下载示例数据库文件:http://download.microsoft.com/download/b/f/2/bf2c691b-5a4d-4375-bc40-19570740bd6e/CSVSSetup.exe
内容:
概要介绍:
在这一节,你将学会建立一个基于MSDE数据库的图片显示的WEB应用程序。本系统将根据用户的角色而显示相关的图片信息,如果用户身份通过验证,既可获得该身份可以查看的信息。
前提条件:
要完成本节示例,确保您的系统安装了以下组件:
Microsoft Visual Studio 2003.NET (or later)
Microsoft SQL Server 2000 Desktop Engine (MSDE) Release A
The sample Pics2Share database. See the Building the Pics2Share Sample Database.rtf file for instructions, which is available in the downloadable code sample listed at the start of this article.
您下载的示例文件解压缩后的codecsvs.txt 包含源代码及其他数据库安装指南。
开始:
建立一个ASP.NET Web 应用项目步骤:
1. 启动Visual Studio.NET,打开“文件”菜单,选择“新建”,点击“项目”。
2. 在“项目属生”窗口,选择“Visula C#项目”。
3. 在“模板”面板中,选择“ASP.NET Web Application.
4. 给Location名为:http://localhost/mypics.
5. 点击“确定”。
6. 此外,你也可以打开“AssemblyInfo.cs”文件进行相关的设置,以适合你的应用项目,比如:Assembly Version.
添加一个默认样式文件
为了使项目美观,你可以使用一个css文件:
1. 在Visual Studio.NET中,右击“解决方案资源管理器”选择添加添加新项。
2. 在“添加新项”对话框中,选择“样式表”,并取名为Style.css。
3. 如果你还没有完成该样式文件的话,可以打开“codecsvs.txt”文件。
4. 从文件“codecsvs.txt”中复制标识为“Item 1”的整块内容。
5. 在Style.css文件里,用复制内容替换Body高亮度区域。
建立数据访问层
在这一部分,为了日后方便使用和维护,你要建立一个类,该类要完成应用访问数据的所有功能,且起到了隔离数据访问代码之功效;首先你需要在web.config文件中添加一个“数据连接字符串”。
添加数据连接字符串
1. 打开项目下的web.config配置文件。
2. 定位到<configuration>节,就在其下添加以下XML(可从codecsvs.txt中“Item 2”获得)
3. <appSettings>
<add key="ConnectionString" value="Server=localhost;Database=Pics2Share;Trusted_Connection=True;Connection Timeout=60;Pooling=True;Min Pool Size=1;Max Pool Size=5"/>
</appSettings>
注意:假定您安装的示例数据库:Pics2Share为MSDE2000(或SQL Server 2000)的默认实例,位于同一台机子(你的WEB服务器)。如果不是上述情况,你需要修改连接字符串,也有可能要对“安全设置”进行设置。
4. 保存并关闭web.config文件。
建立数据访问类
1. 添加一个新类文件,并取名为SSDAL
2. 右击“解决方案资源管理器”,选择添加添加类。
3. 在新类的顶部,添加以下引用
4. using System.Configuration;
5. using System.Data;
6. using System.Data.SqlClient;
7. using System.Web;
8. 添加以下静态函数到新类SSDAL中(从codecsvs.txt中Item 3复制)
public static int AddImage(string ImageName, string ImageDesc,
string ImagePath, string ImageThumb,
int UserId, int MinRole, int ImageGroupId)
{
int retVal = -1;
SqlConnection mcon=null;
SqlCommand mcmd=null;
string conString;
conString = ConfigurationSettings.AppSettings["ConnectionString"];
using (mcon = new SqlConnection(conString))
{
mcmd = new SqlCommand("AddImageMetaData", mcon);
mcmd.CommandType = CommandType.StoredProcedure;
SqlParameter prm;
prm = new SqlParameter("@ImageName", SqlDbType.VarChar, 255);
prm.Value = ImageName;
mcmd.Parameters.Add(prm);
prm = new SqlParameter("@ImageDesc", SqlDbType.VarChar, 255);
prm.Value = ImageDesc;
mcmd.Parameters.Add(prm);
prm = new SqlParameter("@ImagePath", SqlDbType.VarChar, 255);
prm.Value = ImagePath;
mcmd.Parameters.Add(prm);
prm = new SqlParameter("@ImageThumb", SqlDbType.VarChar, 255);
prm.IsNullable = true;
if (ImageThumb == null)
prm.Value = DBNull.Value;
else
prm.Value = ImageThumb;
mcmd.Parameters.Add(prm);
prm = new SqlParameter("@UserId", SqlDbType.Int);
prm.Value = UserId;
mcmd.Parameters.Add(prm);
prm = new SqlParameter("@MinRole", SqlDbType.Int);
prm.Value = MinRole;
mcmd.Parameters.Add(prm);
prm = new SqlParameter("@ImageGroupId", SqlDbType.Int);
prm.Value = ImageGroupId;
mcmd.Parameters.Add(prm);
prm = new SqlParameter("RETURN_VALUE", SqlDbType.Int);
prm.Direction = ParameterDirection.ReturnValue;
mcmd.Parameters.Add(prm);
mcon.Open();
mcmd.ExecuteNonQuery();
retVal = (int)mcmd.Parameters["RETURN_VALUE"].Value;
return retVal;
}
}
9. 添加以下静态属性到类中(Item 4)
public static DataTable ImageGroups
{
get
{
DataTable dt;
string conString =
ConfigurationSettings.AppSettings["ConnectionString"];
using (SqlConnection conn = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("GetAllImageGroups", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "AllImageGroups");
dt = ds.Tables[0];
return dt;
}
}
}
10. 最后,添加静态属性到类中(Item 5)
public static DataView UserRoles
{
get
{
DataView retVal = null;
string conString =
ConfigurationSettings.AppSettings["ConnectionString"];
using (SqlConnection conn = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("GetAllUserRoles", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet("UserRoles");
da.Fill(ds, "AllUserRoles");
if (ds.Tables[0].Rows.Count > 0)
retVal = ds.Tables[0].DefaultView;
return retVal;
}
}
}
建立图片上载功能
在这一部分,你将要建立上载图片的功能到你的WEB应用中。
建立上载表单
1. 把WebForm1.aspx改名为NewImage.aspx
2. 打开NewImage.aspx文件
3. 在方案资源管理窗口,选中Styles.css并拖拽文件到页面设计器,这一行为将对当前页面生效。
4. 将NewImage的title属性改为Add a New Image.
5. 将pageLayout属性改为FlowLayout.
6. 切换到HTML视图中,复制codecsvs.txt中Item 6部分内容替换默认的<form></form>代码。
7. 保存并返回设计视图。
8. 双击“Upload Now”按钮,添加Click事件。
9. 此时你将看到Code-Behind源文件NewImage.aspx.cs,在文件头部,注意到Visual Studio已经给类命名为WebForm1,现在把它改为NewImage,使之更好地和文件相匹配。
10. 在文件顶部,添在以下引用:
using System.Drawing.Imaging;
11.接着添加两个方法到类中,以组装两个DropDownList控件(Item 7)
void LoadImageGroups()
{
DataView dv = new DataView(SSDAL.ImageGroups);
// Perform Data Binding
if (dv != null)
{
cboImageGroups.DataSource = dv;
cboImageGroups.DataValueField = "ImageGroupId";
cboImageGroups.DataTextField = "ImageGroup";
cboImageGroups.DataBind();
}
}
void LoadRoles()
{
DataView dv = SSDAL.UserRoles;
// Perform Data Binding
if (dv != null)
{
cboMinRole.DataSource = dv;
cboMinRole.DataValueField = "RoleId";
cboMinRole.DataTextField = "RoleName";
cboMinRole.DataBind();
}
}
12. 现在,在Page_Load方法中添加调用以上两个方法的代码片断:(Item 8)
if (!Page.IsPostBack)
{
LoadImageGroups();
LoadRoles();
}
13. 在文件顶部添加以下引用并保存文件。
Using System.IO;
14. 添加新类AppGlobals。
15. 添加以下静态成员到AppGlobals类中(Item 9)
public const string pathUploads = "Uploads";
public const string fileDenied = "images/denied.gif";
public const string fileNotFound = "images/noimage.gif";
16. 回到NewImage.aspx.cs文件中,定位到btnUpload_Click方法,添加以下逻辑(Item 10)。
hlinkViewImage.Visible = false;
string strUploadFileName = Upfile.PostedFile.FileName;
string strFileNameOnly = Path.GetFileName(strUploadFileName);
string strServerPath = Server.MapPath(AppGlobals.pathUploads);
if ( !strServerPath.EndsWith("\\") )
strServerPath += "\\";
string strServerFileName = strServerPath + strFileNameOnly;
try
{
// Save the file to disk
Upfile.PostedFile.SaveAs(strServerFileName);
// Generate the thumbnail
string strThumbFile = "Thmb" + strFileNameOnly;
string strFullThumbFile = strServerPath + strThumbFile;
// TODO -- Generate Thumbnail
// TODO -- Once security is enabled,
// provide the correct user id
int intImageId = SSDAL.AddImage(strFileNameOnly,
txtImageDesc.Text,
strServerPath, strThumbFile, 1,
Convert.ToInt32(cboMinRole.SelectedValue),
Convert.ToInt32(cboImageGroups.SelectedValue));
if (intImageId > 0)
{
// TODO -- Add Encryption
hlinkViewImage.NavigateUrl =
string.Format("ShowImage.aspx?{0}",
"Path=" + strServerPath + strFileNameOnly);
hlinkViewImage.Visible = true;
}
}
catch (Exception ex)
{
lblMsg.Text = ex.Message;
}
finally
{
if (lblMsg.Text.Length > 0)
lblMsg.Visible = true;
}
17. 在“方案资源管理器”窗口中,新建一个文件夹,并取名为Uploads.
注意:如果你是在Windows XP或以前的windows版本运行本示例的话,你要找寻ASP.NET 帐号(aspnet_wp).如果你是运行在Windows Server 2003下,请使用NETWORK SERVICE 帐号。
A. 定位到本地目录c:\Inetpub\wwwroot\mypics\uploads.
B. 右击“uploads”目录,选择“属性”命令。
C. 选择“安全”项,赋予”修改”权限许可
D. 点击确定
18. 重复该方法,建立Images目录,复制两个图片文件:denied.gif 和noimage.gif到新建Images目录中。
19. 在方案管理窗口,右击“NewImage.aspx”,选择“设为起始页”命令。
20. 至此,可以按F5运行应用,你将可以上传图片了。
生成缩略图
在本节,你将为每一上载的图片添加缩略图的功能。
建立一个Image Utility 类
1. 建立新类文件为ImageUtil.cs
2. 由于此类仅作用于项目装配建立时,改变”public”为“internal”
3. 在文件头部添下以下引用:
Using System.Drawing;
4. 添加静态函数GenerateThumb(Item 11)到类中。
public static Bitmap GenerateThumb(string FilePath)
{
// We've selected 120 pixels as the arbitrary height
// for the thumbnails. The code preserves the size ratio,
// given this height. If you want larger thumbnails,
// you can modify this value.
const int THUMBNAIL_HEIGHT = 120;
Bitmap bmp = null;
try
{
bmp = new Bitmap(FilePath);
Decimal decRatio = ((Decimal)bmp.Width / bmp.Height);
int intWidth = (int)(decRatio * THUMBNAIL_HEIGHT);
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
Image img = bmp.GetThumbnailImage(
intWidth, THUMBNAIL_HEIGHT, myCallback, IntPtr.Zero);
return (Bitmap)img;
}
catch (Exception)
{
return null;
}
finally
{
if (bmp != null)
{
bmp.Dispose();
}
}
}
5. 添加GetThumbnailImage方法(Item 12)
private static bool ThumbnailCallback()
{
// You have to supply this delegate, even though the thumbnail
// retrieval doesn't actually use it. See the documentation
// for more information.
return false;
}
6. 现在,转到NewImage.aspx.cs文件中,定位到TODO -- Generate Thumbnail注释 位于btnUpload_Click 中,在该注释后添加以下代码片断:
Bitmap bmp = null;
try
{
if (!File.Exists(strFullThumbFile))
{
bmp = ImageUtil.GenerateThumb(strServerFileName);
if (bmp != null)
bmp.Save(strFullThumbFile, ImageFormat.Jpeg);
else
strFullThumbFile = null;
}
}
catch (Exception)
{
strFullThumbFile = null;
}
finally
{
if (bmp != null)
bmp.Dispose();
}
7. 重新编译并运行你的程序,上载另一幅图片文件扣,检查位于Uploads目录下的文件,即以”Thmb”前缀打头的。
建立首页面
在这一部分,你将建立一张Default页,该页用于每次显示5张图片。
添加Default.aspx
既然你已经完成了上载图片的功能,现在是该建立主显示页的时候了。
1. 在你的项目中添加Default.aspx,并打开之。
2. 拖拽Styles.css到Default.aspx中
3. 将title属性设为My Pictures
4. 将pageLayout属性设为FlowLayout.
5. 切换到HTML视图下,复制codesvsvs.txt中的Item14内容替换<form></form>.
6. 保存,并切换到设计视图。
7. 在为页面添加显示数据逻辑前,你应添加一个方法到数据访问层,以便返回所有图片信息。打开SSDAL.cs文件,添加以下只读静态属性到该类中(Item15)
public static DataTable AllImages
{
get
{
string conString =
ConfigurationSettings.AppSettings["ConnectionString"];
using (SqlConnection conn = new SqlConnection(conString))
{
SqlCommand cmd =
new SqlCommand("GetAllImageData", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet("Images");
da.Fill(ds, "AllImages");
return ds.Tables[0];
}
}
}
8.现在打开Code-Behind文件Default.aspx.cs,添加一个新的方法到类中(Item16)
private void LoadGridData()
{
DataView dv = new DataView(SSDAL.AllImages);
dv.RowFilter = "ImageGroupId = " + cboImageGroups.SelectedValue;
grdImages.DataSource = dv;
grdImages.DataBind();
}
9.再添加一个方法到_Default类中,以装配到Image groups DropDownList(Item 17)
private void LoadImageGroups()
{
DataView dv = new DataView(SSDAL.ImageGroups);
// Perform Data Binding
if ( dv != null)
{
cboImageGroups.DataSource = dv;
cboImageGroups.DataValueField = "ImageGroupId";
cboImageGroups.DataTextField = "ImageGroup";
cboImageGroups.DataBind();
cboImageGroups.SelectedIndex = 0;
}
}
11. 在Page_Load方法中,添加LoadGridData到其中。
if ( !Page.IsPostBack )
{
LoadImageGroups();
LoadGridData();
LoadCboPages();
}
12.最后,再回到Default.aspx设计方式中,双击cboImageGroups下拉列表添加SelectedIndexChanged事件,修改SelectedIndexChanged事件,加以下代码:
LoadGridData();
13.保存,并运行应用程序。
添加简单的分页
1. 在页中为grdImages控件添加PageIndexChanged事件,设置grdImages的CurrentPageIndex属性为DataGridPageChangedEventArgs的NewPageIndex属性值,然后调用LoadGridData方法。
2. grdImages.CurrentPageIndex = e.NewPageIndex;
LoadGridData();
3. 在cboImageGroups_SelectedIndexChanged事件中,调用LoadGridData之前,要把DataGrid的CurrentPageIndex属性设置为0
即:grdImages.CurrentPageIndex = 0;
4. 现在你可以重新运行你的程序,检查一下分页功能是否正确地运行。
添加跳转页的功能
除了支持每次浏览多张图片外,你或许想对其增加跳转到特定组内的某一页的功能。
1.在Default.aspx页中,为cboGridPages 下拉列表添加过程:
,转入到Default.aspx.cs里,添加以下代码:
private void LoadCboPages()
{
DataView dv = (DataView)grdImages.DataSource;
int intRowCount = dv.Count;
int intPageSize = 5;
int intRemainder = intRowCount % intPageSize;
int intPages = ((intRowCount - intRemainder) / intPageSize);
if ( intRemainder > 0 )
intPages += 1;
if (intPages == 0)
intPages = 1; // deal with lower bound case
string[] pages = new string[intPages];
for (int i=0; i<intPages; i++)
pages[i] = "Page " + (i+1).ToString();
cboGridPages.DataSource = pages;
cboGridPages.DataBind();
}
2.当没有发生PostBack时,在Page_Load事件中末放置一个LoadCboPages事件,此时,你的Page_Load应该和下面相似:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if ( !Page.IsPostBack )
{
LoadImageGroups();
LoadGridData();
LoadCboPages();
}
}
3.在cboImageGroups_SelectedIndexChanged事件末中添加LoadCboPages方法,它应该像这样:
private void cboImageGroups_SelectedIndexChanged
(object sender, System.EventArgs e)
{
grdImages.CurrentPageIndex = 0;
LoadGridData();
LoadCboPages();
}
5. 在页面设计窗口中,在“cboGridPages”控件上双击“SelectedIndexChanged”双击,新增一事件。
6. 在SelectedIndexChanged事件中,设置grdImages DataGrid的CurrentPageIndex属性值,代码如下:
string strSelected = cboGridPages.SelectedValue;
grdImages.CurrentPageIndex =
(Convert.ToInt32(strSelected.Substring(5)) - 1);
LoadGridData();
7. 保存,并运行应用程序,检查一下跳转页是否正常运行。
允许缩略图显示
为了使得缩略图和原始图均能呈现给用户,你打算要使用自定义HttpHander.该HttpHander将通过映射ShowImage.axd(AXD is a preregistered extension in IIS for ASP.NET)。
1. 添加一个新类,取名为StreamImage.cs
2. 在StreamImage.cs的头部添加以下引用:
3. using System.Collections.Specialized;
4. using System.IO;
5. using System.Web;
using System.Web.SessionState;
6. 你的类需要实现IhttpHander和IreadOnlySessionState接口。在类中添加这两个接口定义语句,此时你的类和以下相似:
7. using System;
using System.Collections.Specialized;
using System.IO;
using System.Web;
using System.Web.SessionState;
namespace mypics
{
public class StreamImage : IHttpHandler, IReadOnlySessionState
{
}
}
8. 添加以下实现属性到类中:
public bool IsReusable { get { return true; } }
9. 添加以下方法到类中(Item19)
private void WriteImage(HttpContext ctx, string FileName)
{
string strContentType = "image/JPEG";
string ext = Path.GetExtension(FileName);
if (ext == ".gif")
strContentType = "image/GIF";
ctx.Response.ContentType = strContentType;
ctx.Response.WriteFile(FileName);
}
Note:该实现仅支持JPEG和GIF格式,欲支持更多的,请添加之。
10. 最后,为接口IhttpHander的ProcessRequest方法添加以下实现(Item20)
public void ProcessRequest(HttpContext ctx)
{
string strPath = ctx.Request.Params["Path"];
if (strPath != null)
{
// TODO -- Add role check
if (!File.Exists(strPath))
{
strPath = ctx.Server.MapPath(AppGlobals.fileNotFound);
}
WriteImage(ctx, strPath);
}
}
11. 保存文件,要使ShowImage.axd工作,你需要让ASP.NET runtime认识它:)。打开web.config文件,定位到第二配置节<system.web>,在其下添加以下XML(Item21)
<httpHandlers>
<add verb="GET" path="ShowImage.axd" type="mypics.StreamImage, mypics" />
</httpHandlers>
12. 现在,回到Default.aspx文件中来,你要准备加入呈现缩略图逻辑的功能。以HTML方式打开Default.aspx。
13. 搜索字符串“<asp:Image id=”imgThumbnail””。为其Image控件添加ImageUrl属性,代码如下(Item22):
ImageUrl='<%# GetImageUrl(Container.DataItem, true) %>'
14. 完成的代码应和以下相像:
<asp:Image id="imgThumbnail" runat="server" ImageAlign="Middle" ImageUrl="<%# GetImageUrl(Container.DataItem, true) %>"></asp:Image>
15.保存更改,打开default.aspx.cs,添加以下方法到你的_Default类中,以支持数据绑定到你添加到页中的表达式(Item23)
protected string GetImageUrl(object dataItem, bool isThumbnail)
{
string imageUrl;
string qstring;
if (isThumbnail)
{
qstring = string.Format("Path={0}&MinRole={1}",
DataBinder.Eval(dataItem, "FullImageThumbPath"),
DataBinder.Eval(dataItem, "MinRole"));
imageUrl = "ShowImage.axd?" + qstring;
}
else
{
qstring = string.Format("Path={0}&MinRole={1}",
DataBinder.Eval(dataItem, "FullImagePath"),
DataBinder.Eval(dataItem, "MinRole"));
imageUrl = "ShowImage.aspx?" + qstring;
}
return imageUrl;
}
16.现在你可以重新运行你的程序,看看缩略图是否呈现在DataGrid中。
允许全图显示
1. 在项目中添加ShowImage.aspx。
2. 拖拽Styles.css到ShowImage.aspx的设计器中。
3. 改title为View Full Size Image
4. 改pageLayout为FlowLayout
5. 切换到HTML视图下,复制codecsvs.txt(Item24)中的代码替换缺省的<form></form>块。
6. 保存,并切换到设计视图。
7. 打开ShowImage.aspx的Code-Behind文件(ShowImage.aspx.cs),在Page_Load事件中添中以下代码(Item25)
string strQstring = String.Empty;
int idx = Request.RawUrl.IndexOf("?");
if (idx > 0)
strQstring = Request.RawUrl.Substring(idx + 1);
// pass along the query string
imgFullImage.ImageUrl = "ShowImage.axd?" + strQstring;
8. 允许Default.aspx上的链接显示全图功能,以HTML方式下打开default.aspx,定位到lnkDisplayImage超链接,添加以下属性(Item26)
NavigateUrl='<%# GetImageUrl(Container.DataItem, false) %>'
9. 完成的HTML应和以下相似:
<asp:HyperLink id="lnkDisplayImage" NavigateUrl='<%# GetImageUrl(Container.DataItem, False) %>' runat="server">Display Image</asp:HyperLink>
10. 保存你所做的工作,运行程序。
添加安全
在这一节,将给站点添加安全的功能,使得仅当用户登录才能上载图片。除此之外,查看图片将受到用户的登录ID和角色。
建立安全类
1. 在项目中添加新类WebSecurity
2. 在新类顶部添加以下引用:
3. using System.Security.Cryptography;
4. using System.Text;
5. using System.Web.Security;
6. 添加以下私有成员(注意最后一个字符是数字1)
private const string DefCryptoAlg = "sha1";
8. 你的类应是这样的:
9. using System;
10.using System.Security.Cryptography;
11.using System.Text;
12.using System.Web.Security;
13.
14.namespace mypics
15.{
16. public class WebSecurity
17. {
18. private const string DefCryptoAlg = "sha1";
19. }
20.}
21.添加以下方法(Item27)到类中,它用于存储数据库中的hashed和salted密文。
public static void HashWithSalt(
string plaintext, ref string salt, out string hash)
{
const int SALT_BYTE_COUNT = 16;
if (salt == null || salt == "")
{
byte[] saltBuf = new byte[SALT_BYTE_COUNT];
RNGCryptoServiceProvider rng =
new RNGCryptoServiceProvider();
rng.GetBytes(saltBuf);
StringBuilder sb =
new StringBuilder(saltBuf.Length);
for (int i=0; i<saltBuf.Length; i++)
sb.Append(string.Format("{0:X2}", saltBuf[i]));
salt = sb.ToString();
}
hash = FormsAuthentication.
HashPasswordForStoringInConfigFile(
salt+plaintext, DefCryptoAlg);
}
21.你也需要添加以下两个方法来支持查询字符串的加密功能tx 第一个代码断:(Item28)
public static string Encrypt(string plaintext)
{
/* Although designed to encrypt time-stamped tickets,
* using FormsAuthentication.Encrypt is by far the simplest
* way to encrypt strings. It does incur a small amount
* of additional space to store two date-time values and
* the size of the FormsAuthenticationTicket itself.
* The other advantage of this technique is that the
* encryption key is auto-generated and stored as an
* LSA secret for you. Be aware that the key is
* server-specific, and if you need to scale the
* application to a web farm you should set the
* decryption key in machine.config on all machines
* in the farm so that cross-machine
* encryption/decryption works properly */
FormsAuthenticationTicket ticket;
ticket = new FormsAuthenticationTicket(1, "", DateTime.Now,
DateTime.Now, false, plaintext, "");
return FormsAuthentication.Encrypt(ticket);
}
22.第二个代码段:(Item29)
public static string Decrypt(string ciphertext)
{
FormsAuthenticationTicket ticket;
ticket = FormsAuthentication.Decrypt(ciphertext);
return ticket.UserData;
}
23.保存文件。
增加数据访问层的功能
接下来,你需要为数据访问层添加新的方法以实现用户的交互功能。
1. 打开SSDAL.cs文件,添加以下方法用于验证存储在数据库中的用户身份信息(Item30)
public static bool ValidateUser(
string UserAlias, string UserPassword, ref int UserId, ref int RoleId)
{
bool intRetVal = false;
string strHash = null;
string conString =
ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn;
SqlCommand cmd;
SqlParameter prm;
using (conn = new SqlConnection(conString))
{
cmd = new SqlCommand("ws_validateUser", conn);
cmd.CommandType = CommandType.StoredProcedure;
prm = cmd.Parameters.Add("@UserAlias", SqlDbType.VarChar, 255);
prm.Value = UserAlias;
prm = cmd.Parameters.Add("@UserId", SqlDbType.Int);
prm.Direction = ParameterDirection.Output;
prm = cmd.Parameters.Add("@UserHash", SqlDbType.VarChar, 50);
prm.Direction = ParameterDirection.Output;
prm = cmd.Parameters.Add("@UserSalt", SqlDbType.VarChar, 50);
prm.Direction = ParameterDirection.Output;
prm = cmd.Parameters.Add("@RoleId", SqlDbType.Int);
prm.Direction = ParameterDirection.Output;
conn.Open();
int intQRetVal = cmd.ExecuteNonQuery();
string strDBHash = cmd.Parameters["@UserHash"].Value.ToString();
string strDBSalt = cmd.Parameters["@UserSalt"].Value.ToString();
WebSecurity.HashWithSalt(UserPassword, ref strDBSalt, out strHash);
if (strDBHash == strHash)
{
UserId = (int)cmd.Parameters["@UserId"].Value;
RoleId = (int)cmd.Parameters["@RoleId"].Value;
intRetVal = true;
}
else
{
UserId = -1;
RoleId = -1;
}
return intRetVal;
}
}
2. 此外,你想使应用能够实现根据组和角色身份的不同而获得的图片而变化时,添加以下存储过程(Item31)
public static DataTable GetImagesByImageGroupId(
int GroupId, int MinRole)
{
DataTable dt;
string conString = ConfigurationSettings.AppSettings["ConnectionString"];
using (SqlConnection conn = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("GetImagesByImageGroupId", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter prm = new SqlParameter("@ImageGroupId", SqlDbType.Int);
prm.Value = GroupId;
cmd.Parameters.Add(prm);
prm = new SqlParameter("@MinRoleId", SqlDbType.Int);
prm.Value = MinRole;
cmd.Parameters.Add(prm);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ImagesByImageGroupId");
dt = ds.Tables[0];
return dt;
}
}
3. 保存文件。
待续。。。。
0 评论:
发表评论