最近做一個項目的安裝部署程序,要求有安裝的驗證,安裝的授權,要輸入授權嗎才可以安裝,禁止非法安裝。
一開始看見用戶界面不錯,可是添加了用戶界面不能控制他,只能接受輸入,然后根據輸入創建數據庫,修改配置之類的東西,網上的資料也多是這類型的,我就自己寫了一個,還不是不太滿意的,這些窗體都是在安裝的過程中彈出來的,我本意是想在安裝之前就驗證這些內容,可是弄不出來,不知道大家有沒有什么好的辦法。
這是需要驗證的兩個部分,兩個自定義的窗體,代碼如下:
installer類的代碼
using
?System;
using
?System.Collections.Generic;
using
?System.ComponentModel;
using
?System.Configuration.Install;
using
?System.Windows.Forms;
namespace
?FactorySetupClassLibrary
{
????[RunInstaller(
true
)]
????
public
?
partial
?
class
?Installer1?:?Installer
????
{
????????
private
?
string
?dbServer;
????????
private
?
string
?loginUser;
????????
private
?
string
?password;
????????
private
?
string
?db;
????????
public
?Installer1()
????????
{
????????????InitializeComponent();
????????}
????????
public
?
override
?
void
?Install(System.Collections.IDictionary?stateSaver)
????????
{
????????????
base
.Install(stateSaver);
????????????
//
AuthorizeForm1?authForm?=?new?AuthorizeForm1();
????????????
//
authForm.ShowDialog();
????????????DBForm1?dbForm?
=
?
new
?DBForm1();
????????????
if
?(dbForm.ShowDialog()?
==
?DialogResult.OK)
????????????
{
????????????????
????????????}
????????????
else
????????????
{
????????????????
throw
?
new
?ApplicationException(
"
應用程序安裝失敗
"
);
????????????}
????????????AuthorizeForm1?authForm?
=
?
new
?AuthorizeForm1();
????????????
if
?(authForm.ShowDialog()?
==
?DialogResult.OK)
????????????
{
????????????}
????????????
else
????????????
{
????????????????
throw
?
new
?ApplicationException(
"
應用程序安裝失敗
"
);
????????????}
????????}
????}
}
數據庫驗證窗體的代碼
using
?System;
using
?System.Collections.Generic;
using
?System.ComponentModel;
using
?System.Data;
using
?System.Drawing;
using
?System.Text;
using
?System.Windows.Forms;
using
?System.Data.SqlClient;
namespace
?FactorySetupClassLibrary
{
????
public
?
partial
?
class
?DBForm1?:?Form
????
{
????????SqlConnection?conn
=
null
;
????????
public
?DBForm1()
????????
{
????????????InitializeComponent();
????????}
????????
private
?
void
?DBForm1_Load(
object
?sender,?EventArgs?e)
????????
{
????????????txtDB.Text?
=
?
"
KBUdiskFactoryTools
"
;
????????????txtDB.Enabled?
=
?
false
;
????????????txtDBServer.Focus();
????????}
????????
private
?
void
?btnOK_Click(
object
?sender,?EventArgs?e)
????????
{
????????????
if
?(ValidatingHelper.ValidatingTextBox(txtDBServer,?
100
,?
"
數據庫服務器
"
,?lblDBServer)?
==
?
false
)
????????????????
return
;
????????????
if
?(ValidatingHelper.ValidatingTextBox(txtLoginName,?
100
,?
"
登錄用戶名
"
,?lblLoginName)?
==
?
false
)
????????????????
return
;
????????????
if
?(ValidatingHelper.ValidatingTextBox(txtPassword,?
100
,?
"
登錄密碼
"
,?lblPassword)?
==
?
false
)
????????????????
return
;
????????????
try
????????????
{
????????????????
using
?(conn?
=
?
new
?SqlConnection(
"
server=
"
?
+
?txtDBServer.Text.Trim()?
+
?
"
;uid=
"
?
+
?txtLoginName.Text.Trim()?
+
????????????????
"
;pwd=
"
?
+
?txtPassword.Text.Trim()?
+
?
"
;database=
"
?
+
?txtDB.Text.Trim()))
????????????????
{
????????????????????conn.Open();
????????????????????conn.Close();
????????????????????
this
.DialogResult?
=
?DialogResult.OK;
????????????????}
????????????}
????????????
catch
????????????
{
????????????????
if
?(conn.State?
!=
?ConnectionState.Closed)
????????????????????conn.Close();
????????????????
this
.DialogResult?
=
?DialogResult.Cancel;
????????????}
????????????
finally
????????????
{
????????????????
if
?(conn.State?
!=
?ConnectionState.Closed)
????????????????????conn.Close();
????????????}
????????}
????????
private
?
void
?btnTestConnection_Click(
object
?sender,?EventArgs?e)
????????
{
????????????
if
?(ValidatingHelper.ValidatingTextBox(txtDBServer,?
100
,?
"
數據庫服務器
"
,?lblDBServer)?
==
?
false
)
????????????????
return
;
????????????
if
?(ValidatingHelper.ValidatingTextBox(txtLoginName,?
100
,?
"
登錄用戶名
"
,?lblLoginName)?
==
?
false
)
????????????????
return
;
????????????
if
?(ValidatingHelper.ValidatingTextBox(txtPassword,?
100
,?
"
登錄密碼
"
,?lblPassword)?
==
?
false
)
????????????????
return
;
????????????
try
????????????
{
????????????????
using
?(conn?
=
?
new
?SqlConnection(
"
server=
"
?
+
?txtDBServer.Text.Trim()?
+
?
"
;uid=
"
?
+
?txtLoginName.Text.Trim()?
+
????????????????
"
;pwd=
"
?
+
?txtPassword.Text.Trim()?
+
?
"
;database=
"
?
+
?txtDB.Text.Trim()))
????????????????
{
????????????????????conn.Open();
????????????????????conn.Close();
????????????????????MessageBox.Show(
"
連接成功,可以點擊確定按鈕繼續安裝
"
);
????????????????}
????????????}
????????????
catch
????????????
{
????????????????
if
?(conn.State?
!=
?ConnectionState.Closed)
????????????????????conn.Close();
????????????????MessageBox.Show(
"
連接失敗,請檢查連接設置之后繼續安裝
"
);
????????????}
????????????
finally
????????????
{
????????????????
if
?(conn.State?
!=
?ConnectionState.Closed)
????????????????????conn.Close();
????????????}
????????}
????????
private
?
void
?btnCancel_Click(
object
?sender,?EventArgs?e)
????????
{
????????????
this
.DialogResult?
=
?DialogResult.Cancel;
????????}
????}
}
授權碼驗證窗體代碼
using
?System;
using
?System.Collections.Generic;
using
?System.ComponentModel;
using
?System.Data;
using
?System.Drawing;
using
?System.Text;
using
?System.Windows.Forms;
namespace
?FactorySetupClassLibrary
{
????
public
?
partial
?
class
?AuthorizeForm1?:?Form
????
{
????????
private
?Hasher?_hasher?
=
?
null
;
????????
private
?
int
?_randomInt;
????????
public
?AuthorizeForm1()
????????
{
????????????InitializeComponent();
????????????_hasher?
=
?
new
?Hasher();
????????}
????????
private
?
void
?AuthorizeForm1_Load(
object
?sender,?EventArgs?e)
????????
{
????????????Random?random?
=
?
new
?Random();
????????????_randomInt?
=
?random.Next(
9999
,?
99999
);
????????????
//
this._hasher.HashText?=?this._computerInfo.GetCPUSn()?+?this._computerInfo.GetHardDriveSn()?+
????????????
//
????this._computerInfo.GetMacAddress()?+?this._computerInfo.GetMainBoardId()?+?this._randomInt.ToString();
????????????
this
._hasher.HashText?
=
?ComputerInfo.GetManchineCPU()?
+
?ComputerInfo.GetMachineHardDiskNumber()?
+
????????????????ComputerInfo.GetMachineMac()?
+
?ComputerInfo.GetMainBoardId()?
+
?
this
._randomInt.ToString();
????????????txtApplyCode.Text?
=
?
this
._hasher.MD5Hasher();
????????????txtApplyCode.Enabled?
=
?
false
;
????????}
????????
private
?
void
?btnOK_Click(
object
?sender,?EventArgs?e)
????????
{
????????????
if
?(txtAuthorizeCode.Text?
==
?
""
)
????????????
{
????????????????errorProvider1.SetError(lblAuthCode,?
"
請輸入授權碼
"
);
????????????????txtAuthorizeCode.Focus();
????????????????
return
;
????????????}
????????????
else
?
if
?(txtAuthorizeCode.TextLength?
!=
?
24
)
????????????
{
????????????????errorProvider1.SetError(lblAuthCode,?
"
請輸入24位的授權碼
"
);
????????????????txtAuthorizeCode.SelectAll();
????????????????txtAuthorizeCode.Focus();
????????????????
return
;
????????????}
????????????
else
????????????
{
????????????????errorProvider1.SetError(lblAuthCode,?
""
);
????????????}
????????????
//
this._hasher.HashText?=?txtApplyCode.Text.Trim();
????????????Hasher?hasher?
=
?
new
?Hasher();
????????????hasher.HashText?
=
?txtApplyCode.Text.Trim();
????????????
//
MessageBox.Show("申請碼:"+txtApplyCode.Text.Trim()+"授權碼:"+hasher.MD5Hasher()+"輸入的授權碼:"+txtAuthorizeCode.Text.Trim());
???????
????????????
if
?(hasher.MD5Hasher().Equals(txtAuthorizeCode.Text.Trim()))
????????????????
this
.DialogResult?
=
?DialogResult.OK;
????????????
else
????????????????
this
.DialogResult?
=
?DialogResult.Cancel;
????????}
????????
private
?
void
?btnCancel_Click(
object
?sender,?EventArgs?e)
????????
{
????????????
this
.DialogResult?
=
?DialogResult.Cancel;
????????}
????}
}
兩個幫助類,一個是加密類,一個是輸入驗證類
using
?System;
using
?System.IO;
using
?System.Security.Cryptography;
namespace
?FactorySetupClassLibrary
{
????
/**/
????
///
?
<summary>
????
///
Copyright?(C),?2004,?kwklover(鄺偉科)
????
///
File?name:Hasher.cs
????
///
Author:鄺偉科?????Version:1.0????????Date:2004年4月22日
????
///
Description:哈希(不可逆)加密通用類庫函數
????
///
?
</summary>
????
public
?
class
?Hasher
????
{
????????
private
?
byte
[]?_HashKey;???
//
哈希密鑰存儲變量
????????
private
?
string
?_HashText;??
//
待加密的字符串
????????
public
?Hasher()
????????
{
????????????
//
????????????
//
?TODO:?在此處添加構造函數邏輯
????????????
//
????????}
????????
///
?
<summary>
????????
///
?帶參數構造函數
????????
///
?
</summary>
????????
///
?
<param?name="hashText">
待加密的字符串
</param>
????????
public
?Hasher(
string
?hashText)
????????
{
????????????
this
._HashText?
=
?hashText;
????????}
????????
/**/
????????
///
?
<summary>
????????
///
?哈希密鑰
????????
///
?
</summary>
????????
public
?
byte
[]?HashKey
????????
{
????????????
set
????????????
{
????????????????_HashKey?
=
?value;
????????????}
????????????
get
????????????
{
????????????????
return
?_HashKey;
????????????}
????????}
????????
/**/
????????
///
?
<summary>
????????
///
?需要產生加密哈希的字符串
????????
///
?
</summary>
????????
public
?
string
?HashText
????????
{
????????????
set
????????????
{
????????????????_HashText?
=
?value;
????????????}
????????????
get
????????????
{
????????????????
return
?_HashText;
????????????}
????????}
????????
/**/
????????
///
?
<summary>
????????
///
?使用HMACSHA1類產生長度為?20?字節的哈希序列。需提供相應的密鑰,接受任何大小的密鑰。
????????
///
?
</summary>
????????
///
?
<returns></returns>
????????
public
?
string
?HMACSHA1Hasher()
????????
{
????????????
byte
[]?HmacKey?
=
?HashKey;
????????????
byte
[]?HmacData?
=
?System.Text.Encoding.UTF8.GetBytes(HashText);
????????????HMACSHA1?Hmac?
=
?
new
?HMACSHA1(HmacKey);
????????????CryptoStream?cs?
=
?
new
?CryptoStream(Stream.Null,?Hmac,?CryptoStreamMode.Write);
????????????cs.Write(HmacData,?
0
,?HmacData.Length);
????????????cs.Close();
????????????
byte
[]?Result?
=
?Hmac.Hash;
????????????
return
?Convert.ToBase64String(Result);??
//
返回長度為28字節字符串
????????}
????????
/**/
????????
///
?
<summary>
????????
///
?使用MACTripleDES類產生長度為?8?字節的哈希序列。需提供相應的密鑰,密鑰長度可為?8、16?或?24?字節的密鑰。
????????
///
?
</summary>
????????
///
?
<returns></returns>
????????
public
?
string
?MACTripleDESHasher()
????????
{
????????????
byte
[]?MacKey?
=
?HashKey;
????????????
byte
[]?MacData?
=
?System.Text.Encoding.UTF8.GetBytes(HashText);
????????????MACTripleDES?Mac?
=
?
new
?MACTripleDES(MacKey);
????????????
byte
[]?Result?
=
?Mac.ComputeHash(MacData);
????????????
return
?Convert.ToBase64String(Result);??
//
返回長度為12字節字符串
????????}
????????
/**/
????????
///
?
<summary>
????????
///
?使用MD5CryptoServiceProvider類產生哈希值。不需要提供密鑰。
????????
///
?
</summary>
????????
///
?
<returns></returns>
????????
public
?
string
?MD5Hasher()
????????
{
????????????
byte
[]?MD5Data?
=
?System.Text.Encoding.UTF8.GetBytes(HashText);
????????????MD5?Md5?
=
?
new
?MD5CryptoServiceProvider();
????????????
byte
[]?Result?
=
?Md5.ComputeHash(MD5Data);
????????????
return
?Convert.ToBase64String(Result);?
//
返回長度為25字節字符串
????????}
????????
/**/
????????
///
?
<summary>
????????
///
?使用SHA1Managed類產生長度為160位哈希值。不需要提供密鑰。
????????
///
?
</summary>
????????
///
?
<returns></returns>
????????
public
?
string
?SHA1ManagedHasher()
????????
{
????????????
byte
[]?SHA1Data?
=
?System.Text.Encoding.UTF8.GetBytes(HashText);
????????????SHA1Managed?Sha1?
=
?
new
?SHA1Managed();
????????????
byte
[]?Result?
=
?Sha1.ComputeHash(SHA1Data);
????????????
return
?Convert.ToBase64String(Result);?
//
返回長度為28字節的字符串
????????}
????????
/**/
????????
///
?
<summary>
????????
///
?使用SHA256Managed類產生長度為256位哈希值。不需要提供密鑰。
????????
///
?
</summary>
????????
///
?
<returns></returns>
????????
public
?
string
?SHA256ManagedHasher()
????????
{
????????????
byte
[]?SHA256Data?
=
?System.Text.Encoding.UTF8.GetBytes(HashText);
????????????SHA256Managed?Sha256?
=
?
new
?SHA256Managed();
????????????
byte
[]?Result?
=
?Sha256.ComputeHash(SHA256Data);
????????????
return
?Convert.ToBase64String(Result);??
//
返回長度為44字節的字符串
????????}
????????
/**/
????????
///
?
<summary>
????????
///
?使用SHA384Managed類產生長度為384位哈希值。不需要提供密鑰。
????????
///
?
</summary>
????????
///
?
<returns></returns>
????????
public
?
string
?SHA384ManagedHasher()
????????
{
????????????
byte
[]?SHA384Data?
=
?System.Text.Encoding.UTF8.GetBytes(HashText);
????????????SHA384Managed?Sha384?
=
?
new
?SHA384Managed();
????????????
byte
[]?Result?
=
?Sha384.ComputeHash(SHA384Data);
????????????
return
?Convert.ToBase64String(Result);??
//
返回長度為64字節的字符串
????????}
????????
/**/
????????
///
?
<summary>
????????
///
?使用SHA512Managed類產生長度為512位哈希值。不需要提供密鑰。
????????
///
?
</summary>
????????
///
?
<returns></returns>
????????
public
?
string
?SHA512ManagedHasher()
????????
{
????????????
byte
[]?SHA512Data?
=
?System.Text.Encoding.UTF8.GetBytes(HashText);
????????????SHA512Managed?Sha512?
=
?
new
?SHA512Managed();
????????????
byte
[]?Result?
=
?Sha512.ComputeHash(SHA512Data);
????????????
return
?Convert.ToBase64String(Result);??
//
返回長度為88字節的字符串
????????}
????}
}
文本框輸入內容驗證類
using
?System;
using
?System.Collections.Generic;
using
?System.Text;
using
?System.Windows.Forms;
namespace
?FactorySetupClassLibrary
{
????
///
?
<summary>
????
///
?輸入驗證幫助類
????
///
?
</summary>
???
public
??
class
?ValidatingHelper
????
{
???????
///
?
<summary>
???????
///
?驗證輸入文本框控件
???????
///
?
</summary>
???????
///
?
<param?name="validatedControl">
需要驗證的控件
</param>
???????
///
?
<param?name="length">
輸入長度
</param>
???????
///
?
<param?name="msg">
文本框的驗證內容
</param>
???????
///
?
<param?name="msgControl">
顯示錯誤信息的控件
</param>
???????
///
?
<returns></returns>
???????
public
?
static
?
bool
?ValidatingTextBox(Control?validatedControl,
int
?length,?
string
?msg,?Control?msgControl)
???????
{
???????????ErrorProvider?error?
=
?
new
?ErrorProvider();
???????????
if
?(validatedControl.Text?
==
?
""
)
???????????
{
???????????????error.SetError(msgControl,?
"
請輸入
"
?
+
?msg);
???????????????validatedControl.Focus();
???????????????
return
?
false
;
???????????}
???????????
else
?
???????????
{
???????????????error.SetError(msgControl,?
""
);
???????????????
return
?
true
;
???????????}
???????}
????}
}
一開始看見用戶界面不錯,可是添加了用戶界面不能控制他,只能接受輸入,然后根據輸入創建數據庫,修改配置之類的東西,網上的資料也多是這類型的,我就自己寫了一個,還不是不太滿意的,這些窗體都是在安裝的過程中彈出來的,我本意是想在安裝之前就驗證這些內容,可是弄不出來,不知道大家有沒有什么好的辦法。


這是需要驗證的兩個部分,兩個自定義的窗體,代碼如下:
installer類的代碼























































































































































































































兩個幫助類,一個是加密類,一個是輸入驗證類

























































































































































































文本框輸入內容驗證類







































更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
