ASP.NET中使用Forms驗證身分識別驗證

來源:互聯網
上載者:User
簡介

ASP.NET中身分識別驗證有三種方式,分別是Windows、Forms和Passport,Windows驗證基於表單驗證,需要每個頁面寫上驗證身份代碼,相對靈活,但操作過於複雜;Passport使用由微軟提供的集中身分識別驗證方式,安全性較高,但實現較複雜。最適合中小型項目的就是基於Forms的身分識別驗證,它將所定義的檔案和目錄集中到一個頁面去做驗證,將使用者的身份發回寫到用戶端的Cookie,在Cookie未到期的時間段內使用者再次訪問網站,就會連同身份Cookie發送到伺服器端,服務端的授權設定可以根據不同目錄不同使用者進行控制了。它的許可權模型為使用者——角色模型,能滿足絕大多數應用情境。

實現

應用情境中所有使用者都擁有全部許可權,此時就只有一個角色,或者認為沒有角色,這樣只需要驗證cookie是否存在即可。以下將一步一步實現該應用。

首先建立網站項目,修改Web.config:

<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true">
</compilation>
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="~/login.aspx" protection="All"></forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>

這裡注意authetication段為配置身分識別驗證方式,loginUrl為預設驗證頁,直接存取其它頁會返回到此頁進行身分識別驗證。name屬性值為cookie名稱。另外它還擁有以下屬性:

cookieless:定義是否使用Cookie及Cookie的行為。UserCookies指定無論在任何裝置上都使用Cookie;UseUri指定從不使用Cookie;AutoDetect指自適應裝置使用;UseDeviceProfile指定適應瀏覽器支援Cookie。

defaultUrl:定義在身分識別驗證後重新導向的預設URL。

timeout:指定Cookie到期時間。

domain:指定Cookie所在域,要知道Cookie是不能跨域的。

Protection:設定為 All,以指定表單身分識別驗證票的保密性和完整性。這導致使用 machineKey 元素上指定的演算法對身分識別驗證票證進行加密,並且使用同樣是 machineKey 元素上指定的雜湊演算法進行簽名。

使用authorizaion元素配置使用者授權模型,

使用allow元素儲存允許訪問的使用者和角色,使用deny元素儲存拒絕訪問的使用者和角色。分別使用users、roles和verbs來控制使用者、角色及HTTP傳輸方法。如:

      <authorization>
<allow users="user1,user2,user3" roles="admin,submitter" verbs="GET,HEAD,POST,DEBUG" />
<deny users="user4" roles="guest" verbs="" />
</authorization>

表示授權user1,user2,user3訪問,不允許user4訪問,訪問角色許可權為admin和submitter,允許行為為GET四種。我們這裡使用user="?"代表所有使用者。

建立login.aspx,頁面配置如下:

驗證身份及寫入Cookie代碼如下:

//儲存角色許可權string[] userRoles=new string[]{"1","1","0"};//isAdmin,isEditor,isPublisherstring userData = userName + "," + string.Join(",", userRoles);//建立票據FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddDays(30), true,userData);//加密票據string authTicket = FormsAuthentication.Encrypt(ticket);//儲存為cookieHttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, authTicket);//cookie.Domain = "51obj.cn";cookie.Expires = ticket.Expiration;Response.Cookies.Add(cookie);Response.Redirect("index.aspx");

這樣Cookie就寫入了用戶端,使用者訪問其他頁面,如果檢測到Cookie不存在就會跳轉到login.aspx這個頁面進行驗證。

可讀取使用者名稱和角色資訊:

        HttpCookie cookie = Request.Cookies[".ASPXAUTH"];        string[] userData = ((System.Web.Security.FormsIdentity)this.Context.User.Identity).Ticket.UserData.Split(',');        string userName = userData[0];        bool isAdmin = Convert.ToBoolean(Convert.ToInt32(userData[1]));        bool isEditor = Convert.ToBoolean(Convert.ToInt32(userData[2]));        bool isPubliher = Convert.ToBoolean(Convert.ToInt32(userData[3]));

代碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.