forms 身份驗證(授權)詳解

2022-03-24 08:08:38 字數 1561 閱讀 5949

首先在 web.config 中設定

設定 mode="forms"

protection="all">  加密和保護

在這個裡面設定那些使用者被容許和拒絕

拒絕  ?代表匿名 *代表所有人

容許在和資料庫比較時,如果通過則寫入資料庫,方法如下,2種都可以

1  把資訊寫入cookies、false 代表cookies 不儲存

// response.redirect("login.aspx");返回指定頁面

2    system.web.security.formsauthentication.redirectfromloginpage(this.textbox1.text,true);//資訊寫入cookies並返回之前的頁面  cookies儲存

system.web.security.formsauthentication.signout();  //刪除儲存的cookies,等於退出登陸。

下面我們通過乙個示例來學習一下使用者少的情況下,不用資料庫直接驗證使用者和密碼。

比如有3個使用者,在forms 中加入紅色部分

//密碼格式有3種 md5 sha1 和

clear  clear 是明文密碼md5是md5加密 、sha1 也是加密

如果是md5或者sha1則下面密碼應該是加密後的字串,即使別人看見了也解不開的,如

在login.aspx(有2個文字框和乙個按鈕 )中驗證可以用以下方法

if(system.web.security.formsauthentication.authenticate(textbox1.text,textbox2.text))  比較使用者和密碼是否在中,如果在則返回 true

textbox1.text 和  textbox2.text 分別對應使用者和密碼

然後寫入cookes 就可以了

system.web.security.formsauthentication.setauthcookie(this.textbox1.text);這樣就完成了驗證。

由於這種方式密碼是明文的,所以很不安全,我們可以用md5 和 sha1 方法實現

首先設定  passwordformat="md

5 " password ="加密後的字串",

驗證還是  system.web.security.formsauthentication.authenticate(textbox1.text,textbox2.text) 他會自動 把textbox2.text 加密然後比較

加密mdb密碼方法:   system.web.security.formsauthentication.hashpasswordforstoringinconfigfile(textbox2.text, "md5");

加密sha1密碼方法:   system.web.security.formsauthentication.hashpasswordforstoringinconfigfile(textbox2.text, "sha1");

這樣就實現加密登陸了 。

如果想停止乙個使用者(如 bo),可以在下面把bo 加入 拒絕使用者,就可以了。雖然他可以通過登陸,但驗證時會出錯。    

Forms 身份驗證

配置應用程式使用 forms 身份驗證 如果應用程式的根目錄中有 web.config 檔案,請開啟該檔案。複製 在system.web元素中,建立乙個authentication元素,並將它的mode屬性設定為 forms,如下面的示例所示 複製 在authentication元素中,建立乙個fo...

Forms身份驗證

在進行forms身份驗證時,如果採用如下的方法配置web.config的話,則會出現所有的頁面都要進行驗證,這是使用者所不能容忍的,具體 如下 這裡我還要說明一點,很多初學者都會把上面的 寫為 結果會出現不能識別forms節點。如果要實現某個目錄下的forms驗證哪個怎麼辦呢?其實 很簡單,在新增完...

簡單Forms身份驗證

web.config login.aspx.cs using system using system.data using system.configuration using system.collections using system.web using system.web.security...