ASP.NET筆記之 圖庫(kù)權(quán)限設(shè)置的方法
1、通過(guò)一個(gè)實(shí)例來(lái)介紹圖庫(kù)權(quán)限,其中涉及到數(shù)據(jù)庫(kù)的應(yīng)用,在visual studio 2010 連接到數(shù)據(jù)庫(kù) 中創(chuàng)建數(shù)據(jù)集及數(shù)據(jù)表可能會(huì)出現(xiàn)無(wú)法遠(yuǎn)程連接的錯(cuò)誤,具體ide解決方案
2、這個(gè)實(shí)例,是通過(guò)輸入用戶名和密碼判斷該用戶是普通用戶還是收費(fèi)用戶,然后進(jìn)入下載圖片列表,非用戶點(diǎn)擊下載是轉(zhuǎn)到跳轉(zhuǎn)頁(yè)面提示,普通用戶下載圖片是帶水印的
試用圖片,而收費(fèi)用戶下載圖片是原始版圖片。在登陸的時(shí)候,同時(shí)設(shè)置錯(cuò)誤登陸次數(shù)限制以及嘗試登陸時(shí)間間隔要求。
這個(gè)過(guò)程需要建立數(shù)據(jù)表以及數(shù)據(jù)集:建一個(gè)DAl文件夾存放,數(shù)據(jù)集存放在APP_Date文件夾下,以確保數(shù)據(jù)的安全性
建數(shù)據(jù)表如下:

數(shù)據(jù)庫(kù)語(yǔ)句如下:
SELECT ID, sUserName, sPassword, iLevel, sErrorTime, sLastErrorTime FROM T_userInfo
SELECT ID, iLevel, sErrorTime, sLastErrorTime, sPassword, sUserName FROM T_userInfo WHERE (ID = @ID)
SELECT ID, iLevel, sErrorTime, sLastErrorTime, sPassword, sUserName FROM T_userInfo WHERE (sUserName = @sUserName)
UPDATE T_userInfo Set sErrorTime=IsNULL(sErrorTime,0)+1,sLastErrorTime=getdate() where ID=@ID
UPDATE T_userInfo Set sErrorTime=0 where ID=@ID
登陸頁(yè)面:login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="圖片下載.login" %>
<!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">
<div>
</div>
<asp:Label ID="Label1" runat="server" Text="用戶名:"></asp:Label>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:Label ID="lablwarn" runat="server" BackColor="#FF3300"
BorderColor="#FF3300" Visible="False"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="密碼 : "></asp:Label>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" ></asp:TextBox>
<br />
<br />
<asp:Button ID="btnLogin" runat="server" onclick="btnLogin_Click" Text="登陸" />
</form>
</body>
</html>
登陸頁(yè)面:login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 圖片下載.DAL.DataSetPicTableAdapters;
namespace 圖片下載
{
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
T_userInfoTableAdapter adapter = new T_userInfoTableAdapter();
var data = adapter.GetDataByUserName(txtUserName.Text);
if (data.Count <= 0)
{
lablwarn.Text = "用戶名不存在";
lablwarn.Visible = true;
}
else {
//LinQ的single的方法,返回為一條數(shù)據(jù)
//數(shù)據(jù)為0 或者或者多條,則拋出異常,把錯(cuò)誤扼殺在搖籃中
var user = data.Single();
//判斷錯(cuò)誤時(shí)間和錯(cuò)誤次數(shù)是否為空
//計(jì)算當(dāng)前時(shí)間和和上次錯(cuò)誤分鐘差
if (!user.IssErrorTimeNull() && !user.IssLastErrorTimeNull()) {
double time = (DateTime.Now - user.sLastErrorTime).TotalMinutes;
if (time <= 30 && user.sErrorTime > 5)
{
lablwarn.Text = "輸入密碼錯(cuò)誤次數(shù)過(guò)多,請(qǐng)等待30分鐘再重新輸入";
lablwarn.Visible = true;
return;
}
}
if (user.sPassword == txtPassword.Text)
{
Session["是否登陸"] = true;
Session["登陸的ID"] = user.ID;
lablwarn.Text = "登陸成功,歡迎回來(lái)";
lablwarn.Visible = true;
//清空錯(cuò)誤次數(shù)
adapter.ResertTimeById(user.ID);
Context.Response.Redirect("Pic_list.htm");
//然后Redirect到其他頁(yè)面
}
else {
adapter.IncErrorTimeById(user.ID);
lablwarn.Text = "密碼錯(cuò)誤,請(qǐng)重新輸入";
lablwarn.Visible = true;
}
}
}
}
}
/*出現(xiàn)錯(cuò)誤:在與 SQL Server 建立連接時(shí)出現(xiàn)與網(wǎng)絡(luò)相關(guān)的或特定于實(shí)例的錯(cuò)誤。
* 未找到或無(wú)法訪問(wèn)服務(wù)器。請(qǐng)驗(yàn)證實(shí)例名稱是否正確并且 SQL Server 已配置為允許遠(yuǎn)程連接。
* (provider: SQL Network Interfaces, error: 26 - 定位指定的服務(wù)器/實(shí)例時(shí)出錯(cuò))
*
* 解決:
*/
下載列表頁(yè)面:Pic_list.htm
<a href="Pic_download.ashx?fileName=11.jpg">圖片1</a>
<a href="Pic_download.ashx?fileName=11.jpg">圖片2</a>
<a href="Pic_download.ashx?fileName=11.jpg">圖片3</a>
下載列表頁(yè)面:Pic_download.ashx
using System.Linq;
using System.Web;
using 圖片下載.DAL.DataSetPicTableAdapters;
using System.Web.SessionState;
using System.Drawing;
namespace 圖片下載
{
/// <summary>
/// Pic_download 的摘要說(shuō)明
/// </summary>
public class Pic_download : IHttpHandler,IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
if (context.Session["是否登陸"] == null)
{
context.Response.Redirect("Target.htm");
}
else {
string fileName = context.Request["fileName"];
//報(bào)頭
context.Response.ContentType="image/JPEG";
string newFileName = HttpUtility.UrlEncode(fileName);
context.Response.AddHeader("Content-Disposition", "attachment:filename=" + newFileName);
//根據(jù)ID獲取數(shù)據(jù)
int user_id = (int)context.Session["登陸的ID"];
T_userInfoTableAdapter adapter = new T_userInfoTableAdapter();
var data = adapter.GetDataById(user_id);
var user = data.Single();
if (user.iLevel == 0) //普通用戶
{
using (System.Drawing.Bitmap bitImage = new System.Drawing.Bitmap("image/" + fileName))
{
//設(shè)置畫布
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitImage))
{
g.DrawString("免費(fèi)用戶試用——"+user.sUserName, new System.Drawing.Font("宋體", 20),Brushes.Red, 0, 0);
}
//保存到輸出流中
bitImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
else//收費(fèi)用戶
{
context.Response.WriteFile("image/"+fileName);
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
跳轉(zhuǎn)頁(yè)面:Target.htm
<!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>跳轉(zhuǎn)中</title>
</head>
<body>
請(qǐng)先登錄,頁(yè)面將在5秒以后轉(zhuǎn)向登陸頁(yè)面,如果
<br/> 還剩<div id="leftDiv"></div>秒
</body>
</html>
<script type="text/javascript">
var leftSecond = 5;
setInterval(function () {
if (leftSecond <= 0) {
window.location.href = "login.aspx";
}
document.getElementById("leftDiv").innerHTML = leftSecond;
leftSecond--;
}, 1000)
</script>
總結(jié):
(1、最大的問(wèn)題就是遇到數(shù)據(jù)庫(kù)遠(yuǎn)程連接的問(wèn)題,不過(guò)通過(guò)了解才知道SQL server 2008不默認(rèn)支持,需要一番設(shè)置,具體的流程:SQL Server 2008 R2:error 26 開啟遠(yuǎn)程連接
(2、獲取context.Request等需要解析IRequiresSessionState接口
相關(guān)文章
.Net?Core應(yīng)用增強(qiáng)型跨平臺(tái)串口類庫(kù)CustomSerialPort()詳解
本文詳細(xì)講解了.Net?Core應(yīng)用增強(qiáng)型跨平臺(tái)串口類庫(kù)CustomSerialPort(),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
ASP.NET Core 1.0實(shí)現(xiàn)郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了ASP.NET Core 1.0實(shí)現(xiàn)郵件發(fā)送功能的相關(guān)資料,需要的朋友可以參考下2016-07-07
WebForm獲取checkbox選中的值(幾個(gè)簡(jiǎn)單的示例)
WebForm中用checkbox的地方挺多的,下面寫了幾個(gè)簡(jiǎn)單的例子,方便以后學(xué)習(xí)使用2014-07-07
.net?core?刪除字符串最后一個(gè)字符的七大類N種實(shí)現(xiàn)方式(總結(jié)篇)
本文詳細(xì)介紹了七大類、N種不同的方法來(lái)刪除字符串的最后一個(gè)字符,涵蓋了從簡(jiǎn)單的字符串方法到使用StringBuilder、數(shù)組操作、Linq以及正則表達(dá)式等多種技術(shù)手段,本文給大家介紹.net?core刪除字符串最后一個(gè)字符,感興趣的朋友一起看看吧2024-10-10
asp.net不同頁(yè)面間數(shù)據(jù)傳遞的多種方法
這篇文章主要介紹了asp.net不同頁(yè)面間數(shù)據(jù)傳遞的多種方法,包括使用QueryString顯式傳遞、頁(yè)面對(duì)象的屬性、cookie、Cache等9種方法2014-01-01
ASP.NET?MVC使用typeahead.js實(shí)現(xiàn)輸入智能提示功能
這篇文章介紹了ASP.NET?MVC使用typeahead.js實(shí)現(xiàn)輸入智能提示功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
asp.net(C#)生成Code39條形碼實(shí)例 條碼槍可以掃描出
這篇文章主要介紹了asp.net(C#)生成Code39條形碼實(shí)例 條碼槍可以掃描出。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02
如何使用ASP.NET MiniAPI 調(diào)試未匹配請(qǐng)求路徑
ASP.NET MiniAPI是一個(gè)輕量級(jí)的Web API框架,它可以讓我們快速地構(gòu)建和部署RESTful服務(wù),本文給大家介紹使用ASP.NET MiniAPI 調(diào)試未匹配請(qǐng)求路徑的方法,感興趣的朋友一起看看吧2024-01-01

