拋出問題
需要2個賬號,一個賬號為admin ,密碼:123
另外一個賬號為guest ,密碼:1234
不允許匿名用戶,和賬號為guest的登錄
代碼實現
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是認證失敗去的頁面 defaultUrl 是認證成功訪問的頁面 --> <forms loginUrl= "Login.aspx" defaultUrl= "/Admin/Admin.aspx" path= "/" name= ".ASPXAUTH" > <credentials passwordFormat= "Clear" > <!--賬號密碼可以看見--> <user name= "admin" password= "123" /> <user name= "guest" password= "1234" /> <!--認證的用戶賬號密碼--> </credentials> </forms> </authentication> <!--禁止沒有認證的用戶訪問--> <authorization> <deny users= "?" /> <!--拒絕沒有登錄的匿名用戶--> <deny users= "guest" /> <!--拒絕賬戶為guest的用戶--> <allow users= "admin" /> <!--允許賬戶為admin的用戶--> </authorization> </system.web> </configuration> |
? 是沒登錄的用戶(匿名用戶) * 是所有用戶
deny 是拒絕什么樣的用戶訪問
allow 是允許什么樣的用戶訪問
后臺的登錄(aspx.cs)
using System.Web.Security
1
2
3
4
|
if (FormsAuthentication.Authenticate(this.TextBox1.Text, this.TextBox2.Text)) //看看配置文件里面是否有認證用戶 { FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text, true); //保存cookie 然后打開要去的地址 } |
這樣一個 過時 的登錄就完成了
感謝觀看!
到此這篇關于ASP.NET通過Web.config實現驗證賬號密碼是否正確進行登錄的文章就介紹到這了,更多相關ASP.NET Web.config登錄內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/qq_46874327/article/details/117259420