利用 IHttpModule 將每個輸出的檔案 Header 加入 Expires 資訊。
打開 web.config 進行編輯,在 <system.webServer> 下的 <modules> 裡加入以下:
<add name="CustomHeaderModule" type="專案名稱.CustomHeaderModule" />
以上名稱 CustomHeaderModule 可以自行更換。
網站目錄下新增一個類別檔案,名稱為 CustomHeaderModule.cs 程式碼如下:
using System;
using System.Web;
namespace XXX
{
public class CustomHeaderModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += OnPreSendRequestHeaders;
}
public void Dispose() { }
void OnPreSendRequestHeaders(object sender, EventArgs e)
{
HttpContext.Current.Response.Expires = 60 * 24; //保留一天
}
}
}HTTP Header 中可以發現有 Server 資訊,但是假如該版本 Server 含有漏洞,就很容易被駭客鎖定,所以就必須將 Server 資訊藏起來會較為安全。
可以利用 IHttpModule 即可達到效果。
打開 web.config 進行編輯,在 <system.webServer> 下的 <modules> 裡加入以下:
<add name="CustomHeaderModule" type="專案名稱.CustomHeaderModule" />
.Net 可以使用 DirectoryEntry 連接 Active Directory ,假如有個管理後台網頁,此時就可以透過 Active Directory 認證登入後台。
範例程式碼如下:
string ActiveDirectoryName = "AD網址或者IP";
string UserName = "帳號";
string UserPwd = "密碼";
DirectoryEntry de = new DirectoryEntry("LDAP://" + ActiveDirectoryName, UserName, UserPwd);
PropertyCollection pc = de.Properties;
if (pc == null)
{
Response.Write("登入失敗");
}
else
{
Response.Write("登入成功<br><br>");
//列出相關參數
foreach (string k in pc.PropertyNames)
{
try
{
Response.Write(k + " = " + pc[k][0] + "<br>");
}
catch { }
}
}網頁常常被其他人嵌入 iframe 嗎?除了可以用 javascript 判斷外,也可以利用 Http Header 即可防止此問題。
只需將 Header 加入 X-Frame-Options,並且設為 SAMEORIGIN。
打開 web.config 進行編輯,在 <system.webServer> 下的 <httpProtocol> 下的 <customHeaders> 裡加入以下:
<add name="X-Frame-Options" value="SAMEORIGIN" />如果想要隱藏 或 變更 X-Powered-By 資訊,可以利用以下方法。
打開 web.config 進行編輯,在 <system.webServer> 下的 <httpProtocol> 下的 <customHeaders> 裡加入以下:
<remove name="X-Powered-By" />
<add name="X-Powered-By" value="CSCWORM" />本篇利用 BitmapData 檢查圖片是否為空白!
※定義:全黑或全白就算是空白。
//檢查圖片是否擷取成功
bool NotNULL = false;
using (Bitmap b = (Bitmap)Bitmap.FromFile(ScreenshotRoute))
{
BitmapData bData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* p = (byte*)bData.Scan0.ToPointer();
for (int y = 0; y < b.Height; y++)
{
for (int x = 0; x < b.Width; x++)
{
if ((p[0] != 255 && p[0] != 0) || (p[1] != 255 && p[1] != 0) || (p[2] != 255 && p[2] != 0))
{
NotNULL = true;
break;
}
p += 3;
}
if (NotNULL) break;
}
b.UnlockBits(bData);
}
}
Console.WriteLine(NotNULL ? "不是空白" : "空白");
本篇利用 FtpWebRequest 下載 FTP 檔案:
private static string FtpDownLoad(string url, string strUserName, string strPassword)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(url));
request.Method = WebRequestMethods.Ftp.DownloadFile;
if (!String.IsNullOrEmpty(strUserName) && !String.IsNullOrEmpty(strPassword)) request.Credentials = new NetworkCredential(strUserName, strPassword);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.Default);
return reader.ReadToEnd();
}取得檔案的MD5值,可以用來判斷檔案是否已變更!
/// <summary>
/// 取的檔案指紋
/// </summary>
/// <param name="FileRoute">檔案路徑</param>
/// <returns>回傳MD5</returns>
public string GetFileMD5(string FileRoute)
{
if (File.Exists(FileRoute))
{
string str = string.Empty;
using (FileStream FS = new FileStream(FileRoute, FileMode.Open, FileAccess.Read))
{
byte[] bytes = (new MD5CryptoServiceProvider()).ComputeHash(FS);
str = BitConverter.ToString(bytes).Replace("-", string.Empty);
FS.Close();
}
return str;
}
else
return string.Empty;
}首先必須下載 MySQL Connector Net 程式:
官方下載網址:http://dev.mysql.com/downloads/connector/net/
本地下載:mysql-connector-net-6.5.4-noinstall
讀取資料表:
using MySql.Data.MySqlClient;
MySqlConnection con = new MySqlConnection(String.Format("Server={0};Port={1};Database={2};Uid={3};Pwd={4};connection timeout={5};use compression={6};pooling={7};min pool size={8};max pool size={9};", "127.0.0.1", "3306", "Database", "UserID", "Password", "5", "True", "False", "5", "100"));
con.Open();
DataSet DS = new DataSet();
MySqlDataAdapter DT = new MySqlDataAdapter("SELECT * FROM XXXX", con);
DT.SelectCommand.CommandTimeout = 60;
DT.Fill(DS);
DT.Dispose();
con.Close();
con.Dispose();
con = null;新增和修改就不寫瞜!只需要把 SQL 指令改掉即可!
本篇利用 FTP Class 連線到 FTP,並且將檔案列出來!
FtpConnection connection = new FtpConnection();
connection.MessageReceived += new FtpConnectionEventHandler(Ftp.connection_MessageReceived);
connection.Host = "ftp://xxx.xxx.xxx/";
connection.UserName = "xxx";
connection.Password = "xxxxxx";
connection.RemoteDirectory = "";
//列出資料夾內的檔案及資料夾
List<string> files = connection.ListDirectory();
foreach (string file in files)
{
Response.Write(file + "<br>");
}範例檔案:Ftp_List
