经过上面的几步,就完成了基于窗体身份验证的过程。基于窗体的身份验证使用的时是非常灵活的 ,在实际应用中可以根据用户身份进行授权管理,包括基于角色的用户权限管理等。下面一节中,我们来介绍基于Windows的身份验证。
下面这个实例演示了如何利用窗体验证来实现ASP.NET的安全控制的。实例是在我们前面已经建立好的MyFirst应用程序中完成的,该Web应用程序包含两个Web窗体,一个是index.aspx,代表默认主页,一个是Login.aspx,代表用户登录系统页面。
首先是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" style="Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 72px" runat="server"
Text="删除Cookies"></asp:Button></FONT></form>
</body>
</HTML>
Index.aspx.vb逻辑代码如下:
Imports System.Web
Imports System.Web.Security
Public Class WebForm2
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'此处省略了窗体设计器身成的代码
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Response.Write("欢迎访问本页面,你已经成功登录系统!")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'清除Cookies信息
FormsAuthentication.SignOut()
End Sub
End Class
Login.aspx的HTML代码如下:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Login.aspx.vb" Inherits="MyFirst.login"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>登录窗口</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:textbox id="TextBox1" style="Z-INDEX: 101; LEFT: 152px; POSITION: absolute; TOP: 40px" runat="server"></asp:textbox><asp:button id="Button1" style="Z-INDEX: 102; LEFT: 184px; POSITION: absolute; TOP: 80px" runat="server"
Text="确认登录"></asp:button>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 56px; POSITION: absolute; TOP: 40px" runat="server"
Width="72px">输入ID</asp:Label></FONT></form>
</body>
</HTML>
Login.aspx.vb的逻辑代码如下:
Imports System.Web
Imports System.Web.Security
Public Class login
Inherits UI.Page
#Region " Web 窗体设计器生成的代码 "
'此处省略了窗体设计器身成的代码
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If checkuser() = True Then
cookie_add()
Response.Redirect(Request("ReturnUrl"))
Else
Response.Write("<script>alert('对不起,用户名错误!');</script>")
End If
Catch ex As Exception
Response.Write(ex.Message)
Finally
End Try
End Sub
' 用户身份检测
Function checkuser() As Boolean
'这里我们在程序中指定只有admin1和admin2两个用户是合法用户
'而实际应用中往往是和数据库中的user表结合的,通过查找用户名和密码来实现的
If TextBox1.Text = "admin1" Or TextBox1.Text = "admin2" Then
Return True
End If
End Function
Sub cookie_add()
Dim t As FormsAuthenticationTicket
t = New FormsAuthenticationTicket(TextBox1.Text, False, 30)
Dim s As String = FormsAuthentication.Encrypt(t)
Dim hc As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, s)
Response.Cookies.Add(hc)
End Sub
End Class
保存编译后,我们第一次通过浏览器或是没有登录的情况下访问index.aspx,页面会自动跳转到Login.aspx页面,如果登录成功就跳转到index.aspx页面。
值得注意的是,尽管我们在Login.aspx的逻辑代码中允许admin2登录,但是,由于在web.config中设置<deny users="admin2" />,所以尽管在Login.aspx中通过了验证,但是admin2的登录仍是被拒绝的,整个系统只有一个合法的用户admin1。