這篇文章引用到了microsoft .net類(lèi)庫(kù)中的以下名空間:
    system.data.sqlclient
     system.web.security
-------------------------------
   任務(wù):
    摘要: 
  1.要求
    2.用visual c#.net 創(chuàng)建一個(gè)asp.net 應(yīng)用程序
  3.在web.config文件里配置安全設(shè)置
  4.創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)表樣例來(lái)存放用戶資料
  5.創(chuàng)建logon.aspx頁(yè)面
  6.編寫(xiě)事件處理代碼來(lái)驗(yàn)證用戶身份
  7.創(chuàng)建一個(gè)default.aspx頁(yè)面
  8.附加提示
摘要
 這篇文章示范了如何實(shí)現(xiàn)通過(guò)數(shù)據(jù)庫(kù)存儲(chǔ)用戶信息來(lái)實(shí)現(xiàn)基于表單的驗(yàn)證.
(一)要求
 需要以下工具來(lái)實(shí)現(xiàn)
1.microsoft visual studio.net
2.microsoft internet information services(iis) version 5.0 或者更新
3.microsoft sql server
(二)用c#.net創(chuàng)建asp.net應(yīng)用程序
1.打開(kāi)visual studio.net
2.建立一個(gè)新的asp.net web應(yīng)用程序,并且指定名稱和路徑.
(三)在web.config文件里配置安全設(shè)置
這一節(jié)示范了如何通過(guò)添加和修改<authentication>和<authorization>節(jié)點(diǎn)來(lái)配置asp.net應(yīng)用程序以實(shí)現(xiàn)基于表單的驗(yàn)證.
1.在解決方案窗口里,打開(kāi)web.config文件.
2.把a(bǔ)uthentication模式改為forms(注:默認(rèn)為windows)
3.插入<forms>標(biāo)簽,并且填入適當(dāng)?shù)膶傩裕ㄕ?qǐng)鏈接到在文章最后列出的msdn文檔或者quickstart文檔來(lái)查看這些屬性)先復(fù)制下面的代碼,接著再把它粘貼到<authentication>節(jié):
<authentication mode="forms">
<form name=".aspxformsdemo" loginurl="logon.aspx" protection="all" path="/" timeout="30"/>
</authentication>
(注:如果不指定loginurl,默認(rèn)為default.aspx)
4.通過(guò)加入以下節(jié)點(diǎn)實(shí)現(xiàn)拒絕匿名訪問(wèn):
<authentication>
<deny users="?"/>
<allow users="*"/>
</authentication>
(四)創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)表樣例來(lái)存放用戶資料
這一節(jié)示范了如何創(chuàng)建一個(gè)示例數(shù)據(jù)庫(kù)來(lái)存放用戶名,密碼,和用戶角色.如果你想要實(shí)現(xiàn)基于角色的安全就有必要在數(shù)據(jù)庫(kù)中添加一個(gè)存放用戶角色的字段.
1.打開(kāi)記事本。
2.把下面這段腳本復(fù)制到記事本然后保存:
if exists (select * from sysobjects where id = 
object_id(n'[dbo].[users]') and objectproperty(id, n'isusertable') = 1)
drop table [dbo].[users]
go
create table [dbo].[users] (
   [uname] [varchar] (15) not null ,
   [pwd] [varchar] (25) not null ,
   [userrole] [varchar] (25) not null ,
) on [primary]
go
alter table [dbo].[users] with nocheck add 
   constraint [pk_users] primary key  nonclustered 
   (
      [uname]
   )  on [primary] 
go
insert into users values('user1','user1','manager')
insert into users values('user2','user2','admin')
insert into users values('user3','user3','user')
go
3.打開(kāi)microsoft sql server,打開(kāi)查詢分析器,在數(shù)據(jù)庫(kù)列表里選擇pubs數(shù)據(jù)庫(kù),然后把上面的腳本粘貼過(guò)來(lái),運(yùn)行。這時(shí)會(huì)在pubs數(shù)據(jù)庫(kù)里創(chuàng)建一個(gè)將會(huì)在這個(gè)示例程序中用到的示例用戶表。
新聞熱點(diǎn)
疑難解答
圖片精選