1. Introduce the permissions of the image library through an instance, which involves database applications. When you connect to the database in visual studio 2010, creating a dataset and a data table may cause an error that cannot be remotely connected. The specific ide Solution
For details, refer to SQL Server 2008 R2: error 26 to enable remote connection.
2. For this instance, you can enter the user name and password to determine whether the user is a common user or a paid user, and then enter the download image list. If the user does not click Download, the system will go to the jump page prompt, normal users download images with watermarks
Trial images, whereas paid users download images in the original version. At the same time, set the limit on the number of wrong logins and the interval between attempts to log on.
In this process, you need to create a data table and a dataset: Create a DAl folder to store the dataset in the APP_Date folder to ensure data security.
Create a data table as follows:
The database statement is as follows:
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
Logon page: login. aspx
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "login. aspx. cs" Inherits = "image download. 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 = "username:"> </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 = "Password:"> </asp: Label>
<Asp: TextBox ID = "txtPassword" runat = "server" TextMode = "Password"> </asp: TextBox>
<Br/>
<Br/>
<Asp: Button ID = "btnLogin" runat = "server" onclick = "btnLogin_Click" Text = "login"/>
</Form>
</Body>
</Html>
Logon page: login. aspx. cs
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using Image download. DAL. DataSetPicTableAdapters;
Namespace image download
{
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 = "the user name does not exist ";
Lablwarn. Visible = true;
}
Else {
// Returns a data record for the single method of LinQ.
// If the data is zero or multiple, an exception is thrown and the error is killed in the cradle.
Var user = data. Single ();
// Determine whether the error time and number of errors are empty
// Calculate the difference between the current time and the last error minute
If (! User. IssErrorTimeNull ()&&! User. IssLastErrorTimeNull ()){
Double time = (DateTime. Now-user. sLastErrorTime). TotalMinutes;
If (time <= 30 & user. sErrorTime> 5)
{
Lablwarn. Text = "too many times of incorrect password input. Please wait 30 minutes and try again ";
Lablwarn. Visible = true;
Return;
}
}
If (user. sPassword = txtPassword. Text)
{
Session ["login"] = true;
Session ["Login ID"] = user. ID;
Lablwarn. Text = "Login successful, welcome back ";
Lablwarn. Visible = true;
// Clear error count
Adapter. ResertTimeById (user. ID );
Context. Response. Redirect ("Pic_list.htm ");
// Then Redirect to other pages
}
Else {
Adapter. IncErrorTimeById (user. ID );
Lablwarn. Text = "Incorrect password. Please try again ";
Lablwarn. Visible = true;
}
}
}
}
}
/* Error: Network-related or instance-specific errors occur when you establish a connection with SQL Server.
* The server is not found or cannot be accessed. Verify that the Instance name is correct and SQL Server is configured to allow remote connection.
* (Provider: SQL Network Interfaces, error: 26-An error occurred while locating the specified server/instance)
*
* Solution:
*/
Download list page: Pic_list.htm
<A href = "Pic_download.ashx? FileName=11.jpg "> image 1 </a>
<A href = "Pic_download.ashx? FileName=11.jpg "> Image 2 </a>
<A href = "Pic_download.ashx? FileName=11.jpg "> Image 3 </a>
Download list page: Pic_download.ashx
Using System. Linq;
Using System. Web;
Using Image download. DAL. DataSetPicTableAdapters;
Using System. Web. SessionState;
Using System. Drawing;
Namespace image download
{
/// <Summary>
/// Summary of Pic_download
/// </Summary>
Public class Pic_download: IHttpHandler, IRequiresSessionState
{
Public void ProcessRequest (HttpContext context)
{
If (context. Session ["login?"] = null)
{
Context. Response. Redirect ("Target.htm ");
}
Else {
String fileName = context. Request ["fileName"];
// Header
Context. Response. ContentType = "image/JPEG ";
String newFileName = HttpUtility. UrlEncode (fileName );
Context. Response. AddHeader ("Content-Disposition", "attachment: filename =" + newFileName );
// Obtain data by ID
Int user_id = (int) context. Session ["Login ID"];
T_userInfoTableAdapter adapter = new T_userInfoTableAdapter ();
Var data = adapter. GetDataById (user_id );
Var user = data. Single ();
If (user. iLevel = 0) // normal user
{
Using (System. Drawing. Bitmap bitImage = new System. Drawing. Bitmap ("image/" + fileName ))
{
// Set the canvas
Using (System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (bitImage ))
{
G. DrawString ("free user trial --" + user. sUserName, new System. Drawing. Font ("", 20), Brushes. Red, 0, 0 );
}
// Save it to the output stream
BitImage. Save (context. Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );
}
}
Else // billable user
{
Context. Response. WriteFile ("image/" + fileName );
}
}
}
Public bool IsReusable
{
Get
{
Return false;
}
}
}
}
Jump page: Target.htm
Copy codeThe Code is as follows:
<! 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> jump in progress </title>
</Head>
<Body>
Please log on first. The page will go to the login page after 5 seconds. If
To enter the logon page immediately, click <a href = "login. aspx"> here </a>
<Br/> remaining <div id = "leftDiv"> </div> seconds
</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>
Summary:
(1. The biggest problem is the problem of remote database connection. However, I learned that SQL server 2008 is not supported by default and requires some settings. The specific process is: SQL Server 2008 R2: error 26 enable remote connection
For details about how to enable remote connection, see SQL Server 2008 R2: error 26.
(2) obtain the IRequiresSessionState interface, such as context. Request