要采用窗體驗證,先要在應用程序根目錄中的web.config中做相應的設置:
<authentication mode="forms">
<forms name=".aspxauth " loginurl="/login.aspx" timeout="30" path= "/">
</forms>
</authentication>
其中<authentication mode= "forms"> 表示本應用程序采用forms驗證方式。
(1)<forms>標簽中的name表示指定要用于身份驗證的 http cookie。默認情況下,name 的值是 .aspxauth。采用此種方式驗證用戶后,以此用戶的信息建立一個formsauthenticationticket類型的身份驗證票,再加密序列化為一個字符串,最后將這個字符串寫到客戶端的name指定名字的cookie中。一旦這個cookie寫到客戶端后,此用戶再次訪問這個web應用時會將連同cookie一起發(fā)送到服務端,服務端將會知道此用戶是已經(jīng)驗證過的。
再看一下身份驗證票都包含哪些信息呢,我們看一下formsauthenticationticket類:
cookiepath: 返回發(fā)出 cookie 的路徑。注意,窗體的路徑設置為 /。由于窗體區(qū)分大小寫,這是為了防止站點中的 url 的大小寫不一致而采取的一種保護措施。這在刷新 cookie 時使用。
expiration: 獲取 cookie 過期的日期/時間。
ispersistent: 如果已發(fā)出持久的 cookie,則返回 true。否則,身份驗證 cookie 將限制在瀏覽器生命周期范圍內。
issuedate: 獲取最初發(fā)出 cookie 的日期/時間。
name: 獲取與身份驗證 cookie 關聯(lián)的用戶名。
userdata :獲取存儲在 cookie 中的應用程序定義字符串。
version: 返回字節(jié)版本號供將來使用。
(2) <forms>標簽中的loginurl指定如果沒有找到任何有效的身份驗證cookie,為登錄將請求重定向到的 url。默認值為 login.aspx。loginurl指定的頁面就是用來驗證用戶身份的,一般此頁面提供用戶輸入用戶名和密碼,用戶提交后由程序來根據(jù)自己的需要來驗證用戶的合法性(大多情況是將用戶輸入信息同數(shù)據(jù)庫中的用戶表進行比較),如果驗證用戶有效,則生成同此用戶對應的身份驗證票,寫到客戶端的cookie,最后將瀏覽器重定向到用戶初試請求的頁面,一般是用formsauthentication.redirectfromloginpage 方法來完成生成身份驗證票,寫回客戶端,瀏覽器重定向等一系列的動作。redirectfromloginpage 方法含有3個參數(shù),函數(shù)定義如下:
public static void redirectfromloginpage( string username, bool createpersistentcookie, string strcookiepath )
其中:
username: 就是此用戶的標示,用來標志此用戶的唯一標示,不一定要映射到用戶賬戶名稱;
createpersistentcookie: 標示是否發(fā)出持久的 cookie。若不是持久cookie,cookie的有效期expiration屬性有當前時間加上web.config中timeout的時間,每次請求頁面時,在驗證身份過程中,會判斷是否過了有效期的一半,要是的話更新一次cookie的有效期;若是持久cookie,expiration屬性無意義,這時身份驗證票的有效期有cookie的expires決定,redirectfromloginpage方法給expires屬性設定的是50年有效期;
strcookiepath: 標示將生成的cookie的寫到客戶端的路徑,身份驗證票中保存這個路徑是在刷新身份驗證票cookie時使用(這也是生成cookie的path),若沒有strcookiepath 參數(shù),則使用web.config中 path屬性的設置。
這里可以看到,此方法參數(shù)只有三個,而身份驗證票的屬性有七個,不足的四個參數(shù)是這么來的:
issuedate: cookie發(fā)出時間由當前時間得出;
expiration:過期時間由當前時間和下面要說的<forms>標簽中timeout參數(shù)算出。此參數(shù)對非持久性cookie有意義;
userdata: 這個屬性可以用應用程序寫入一些用戶定義的數(shù)據(jù),此方法沒有用到這個屬性,只是簡單的將此屬性置為空字符串,請注意此屬性,在后面我們將要使用到這個屬性;
version: 版本號由系統(tǒng)自動提供。
redirectfromloginpage方法生成生成身份驗證票后,會調用formsauthentication.encrypt 方法,將身份驗證票加密為字符串,這個字符串將會是以.aspxauth為名字的一個cookie的值。這個cookie的其它屬性的生成:domain,path屬性為確省值,expires視createpersistentcookie參數(shù)而定,若是持久cookie,expires設為50年以后過期;若是非持久cookie,expires屬性不設置。
生成身份驗證cookie后,將此cookie加入到response.cookies中,等待發(fā)送到客戶端。
最后redirectfromloginpage方法調用formsauthentication.getredirecturl 方法獲取到用戶原先請求的頁面,重定向到這個頁面。
(3) <forms>標簽中的timeout和path,是提供了身份驗證票寫入到cookie過期時間和默認路徑。
經(jīng)過上面的幾步,就完成了基于窗體身份驗證的過程。基于窗體的身份驗證使用的時是非常靈活的 ,在實際應用中可以根據(jù)用戶身份進行授權管理,包括基于角色的用戶權限管理等。下面一節(jié)中,我們來介紹基于windows的身份驗證。
下面這個實例演示了如何利用窗體驗證來實現(xiàn)asp.net的安全控制的。實例是在我們前面已經(jīng)建立好的myfirst應用程序中完成的,該web應用程序包含兩個web窗體,一個是index.aspx,代表默認主頁,一個是login.aspx,代表用戶登錄系統(tǒng)頁面。
首先是web.config配置文件,如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation defaultlanguage="vb" debug="true" />
<customerrors mode="remoteonly" />
<authentication mode="forms">
<forms name=".aspxauth" loginurl="login.aspx" timeout="30"></forms>
</authentication>
<authorization>
<deny users="admin2" />
<deny users="?" />
</authorization>
<trace enabled="false" requestlimit="10" pageoutput="false" tracemode="sortbytime" localonly="true" />
<sessionstate
mode="inproc"
stateconnectionstring="tcpip=127.0.0.1:42424"
sqlconnectionstring="data source=127.0.0.1;trusted_connection=yes"
cookieless="false"
timeout="20"
/>
<globalization requestencoding="utf-8" responseencoding="utf-8" />
</system.web>
</configuration>
index.aspx的html代碼如下:
<%@ page language="vb" autoeventwireup="false" codebehind="index.aspx.vb" inherits="myfirst.webform2"%>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>基于web窗體的身份驗證實例</title>
<meta content="microsoft visual studio .net 7.1" name="generator">
<meta content="visual basic .net 7.1" name="code_language">
<meta content="javascript" name="vs_defaultclientscript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">
</head>
<body ms_positioning="gridlayout">
<form id="form1" method="post" runat="server">
<font face="宋體">
<asp:button id="button1" runat="server"
text="刪除cookies"></asp:button></font></form>
</body>
</html>
新聞熱點
疑難解答
圖片精選