ASP.NET筆記之 圖庫使用權限設定的方法

來源:互聯網
上載者:User

1、通過一個執行個體來介紹圖庫許可權,其中涉及到資料庫的應用,在visual studio 2010 串連到資料庫 中建立資料集及資料表可能會出現無法遠端連線的錯誤,具體ide解決方案

可以參考 SQL Server 2008 R2:error 26 開啟遠端連線詳解

2、這個執行個體,是通過輸入使用者名稱和密碼判斷該使用者是普通使用者還是收費使用者,然後進入下載圖片列表,非使用者點擊下載是轉到跳轉頁面提示,普通使用者下載圖片是帶浮水印的

試用圖片,而收費使用者下載圖片是原始版圖片。在登陸的時候,同時設定錯誤登陸次數限制以及嘗試登陸時間間隔要求。

這個過程需要建立資料表以及資料集:建一個DAl檔案夾存放,資料集存放在APP_Date檔案夾下,以確保資料的安全性

建資料表如下:

資料庫語句如下:

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


登陸頁面: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>

登陸頁面: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的方法,返回為一條資料
//資料為0 或者或者多條,則拋出異常,把錯誤扼殺在搖籃中
var user = data.Single();

//判斷錯誤時間和錯誤次數是否為空白
//計算目前時間和和上次錯誤分鐘差
if (!user.IssErrorTimeNull() && !user.IssLastErrorTimeNull()) {
double time = (DateTime.Now - user.sLastErrorTime).TotalMinutes;
if (time <= 30 && user.sErrorTime > 5)
{

lablwarn.Text = "輸入密碼錯誤次數過多,請等待30分鐘再重新輸入";
lablwarn.Visible = true;
return;
}
}

if (user.sPassword == txtPassword.Text)
{
Session["是否登陸"] = true;
Session["登陸的ID"] = user.ID;

lablwarn.Text = "登陸成功,歡迎回來";
lablwarn.Visible = true;
//清空錯誤次數
adapter.ResertTimeById(user.ID);
Context.Response.Redirect("Pic_list.htm");
//然後Redirect到其他頁面
}
else {
adapter.IncErrorTimeById(user.ID);
lablwarn.Text = "密碼錯誤,請重新輸入";
lablwarn.Visible = true;
}
}
}

}
}
/*出現錯誤:在與 SQL Server 建立串連時出現與網路相關的或特定於執行個體的錯誤。
* 未找到或無法訪問伺服器。請驗證執行個體名稱是否正確並且 SQL Server 已配置為允許遠端連線。
* (provider: SQL Network Interfaces, error: 26 - 定位指定的伺服器/執行個體時出錯)
*
* 解決:
*/

下載列表頁面: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>

下載列表頁面:Pic_download.ashx
using System.Linq;
using System.Web;
using 圖片下載.DAL.DataSetPicTableAdapters;
using System.Web.SessionState;
using System.Drawing;

namespace 圖片下載
{
/// <summary>
/// Pic_download 的摘要說明
/// </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"];

//前序
context.Response.ContentType="image/JPEG";
string newFileName = HttpUtility.UrlEncode(fileName);
context.Response.AddHeader("Content-Disposition", "attachment:filename=" + newFileName);

//根據ID擷取資料
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))
{
//設定畫布
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitImage))
{
g.DrawString("免費使用者試用——"+user.sUserName, new System.Drawing.Font("宋體", 20),Brushes.Red, 0, 0);
}
//儲存到輸出資料流中
bitImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

}
}
else//收費使用者
{
context.Response.WriteFile("image/"+fileName);
}
}
}

public bool IsReusable
{
get
{
return false;
}
}
}
}
跳轉頁面: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>跳轉中</title>
</head>
<body>
請先登入,頁面將在5秒以後轉向登陸頁面,如果
您想立即進入登入介面,請<a href="login.aspx">點擊這裡</a>
<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>

總結:

(1、最大的問題就是遇到資料庫遠端連線的問題,不過通過瞭解才知道SQL server 2008不預設支援,需要一番設定,具體的流程:SQL Server 2008 R2:error 26 開啟遠端連線詳解
詳細出處參考:SQL Server 2008 R2:error 26 開啟遠端連線詳解
(2、擷取context.Request等需要解析IRequiresSessionState介面

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.