拋出問(wèn)題
需要2個(gè)賬號(hào),一個(gè)賬號(hào)為admin ,密碼:123
另外一個(gè)賬號(hào)為guest ,密碼:1234
不允許匿名用戶(hù),和賬號(hào)為guest的登錄
代碼實(shí)現(xiàn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<configuration> <system.web> <compilation debug= "true" targetFramework= "4.5.2" /> <httpRuntime targetFramework= "4.5.2" /> <authentication mode= "Forms" > <!--loginUrl是認(rèn)證失敗去的頁(yè)面 defaultUrl 是認(rèn)證成功訪問(wèn)的頁(yè)面 --> <forms loginUrl= "Login.aspx" defaultUrl= "/Admin/Admin.aspx" path= "/" name= ".ASPXAUTH" > <credentials passwordFormat= "Clear" > <!--賬號(hào)密碼可以看見(jiàn)--> <user name= "admin" password= "123" /> <user name= "guest" password= "1234" /> <!--認(rèn)證的用戶(hù)賬號(hào)密碼--> </credentials> </forms> </authentication> <!--禁止沒(méi)有認(rèn)證的用戶(hù)訪問(wèn)--> <authorization> <deny users= "?" /> <!--拒絕沒(méi)有登錄的匿名用戶(hù)--> <deny users= "guest" /> <!--拒絕賬戶(hù)為guest的用戶(hù)--> <allow users= "admin" /> <!--允許賬戶(hù)為admin的用戶(hù)--> </authorization> </system.web> </configuration> |
? 是沒(méi)登錄的用戶(hù)(匿名用戶(hù)) * 是所有用戶(hù)
deny 是拒絕什么樣的用戶(hù)訪問(wèn)
allow 是允許什么樣的用戶(hù)訪問(wèn)
后臺(tái)的登錄(aspx.cs)
using System.Web.Security
1
2
3
4
|
if (FormsAuthentication.Authenticate(this.TextBox1.Text, this.TextBox2.Text)) //看看配置文件里面是否有認(rèn)證用戶(hù) { FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text, true); //保存cookie 然后打開(kāi)要去的地址 } |
這樣一個(gè) 過(guò)時(shí) 的登錄就完成了
感謝觀看!
到此這篇關(guān)于ASP.NET通過(guò)Web.config實(shí)現(xiàn)驗(yàn)證賬號(hào)密碼是否正確進(jìn)行登錄的文章就介紹到這了,更多相關(guān)ASP.NET Web.config登錄內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_46874327/article/details/117259420