我有 2 页:
login.aspx <公共
user.aspx <应该在登录后被锁定
我一直在努力让它发挥作用,但我不知道我现在在哪里。 Google 主要展示 C# 而不是 vb.net。 我创建了一个登录页面,该页面可以工作并继续到下一页。
不过..
我也可以输入下一页的 URL 并且不需要登录。 我怎么做才能让第二页告诉我你需要登录才能查看这个页面。
到目前为止我所拥有的是:
登录.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="login.aspx.vb" Inherits="web.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
height: 172px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="auto-style1">
Loginpage<br />
<br />
user:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
password:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
背后的代码:
Imports System.Data.SqlClient
Public Class WebForm1
Inherits UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
con.ConnectionString = "myconstring"
Dim objcon As SqlConnection = Nothing
Dim objcmd As SqlCommand = Nothing
objcon = New SqlConnection("myconstring")
objcon.Open()
Dim stmt As String = "select * from login where Username = '" & TextBox1.Text & "' AND Password = '" & TextBox2.Text & "'"
objcmd = New SqlCommand(stmt, objcon)
Dim reader As SqlDataReader = objcmd.ExecuteReader
If reader.Read Then
Response.Redirect("user\user.aspx")
Else
Label1.Visible = True
Label1.Text = "Login onjuist"
End If
End Sub
End Class
答案 0 :(得分:1)
在用户页面后面的代码中,您可以检查用户是否已通过身份验证。如果他们未通过身份验证,您可以将他们重定向到一个页面,通知他们必须登录才能访问他们试图访问的页面。下面的代码只是重定向到登录页面。
If HttpContext.Current.User.Identity.IsAuthenticated = False Then
Response.Redirect("~/Login.aspx")
End If
另一个选项是您可以在 web.config 中定义一个位置部分。以下代码拒绝未经身份验证的用户访问 users.aspx。
<location path="users.aspx">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>