nder 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