如何在不使用 asp 控件的情况下验证登录表单
How to Authenticate Login form without using asp controls
我制作了一个非常漂亮的登录页面,我在其中使用 HTML 输入标签作为我的用户名、密码和登录按钮。我有两种不同的登录表单,一种用于每种类型的用户(学生用户和企业用户)。我从来没有对数据库进行过身份验证,所以我认为我的方法很容易执行。在网上搜索使用我已有的方法来实现的简单方法后,我发现在不使用 asp:login
控件的情况下几乎不可能弄清楚如何实现我的目标。不情愿地,我放弃了我拥有的东西并投入 asp:login
控制。我的问题是,实现起来很痛苦(创建存储过程,并弄乱 web.config),它看起来很可怕,因为它不符合我之前写的 CSS,并且我使用的 FormsAuthentication 只允许我在登录后重定向一个用户(登录成功后重定向到主门户,如果登录失败则返回登录页面。)在我的 Web.config:[=24 中使用身份验证表单模式=]
<forms defaultUrl="~/InteriorStudentPortal.aspx" loginUrl="~/ExteriorLogin.aspx" slidingExpiration="true" timeout="2880"></forms>
因为我有两个用户,所以我需要根据登录的用户进行重定向。这是我一直在使用的 asp:login
控件的示例:
<asp:Login ID="Login1" runat="server" CssClass="Login" OnAuthenticate= "ValidateStudent" Width="313px"></asp:Login>
连同后台代码:
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.Security
Partial Class ExteriorLogin
Inherits System.Web.UI.Page
Protected Sub ValidateStudent(sender As Object, e As AuthenticateEventArgs) Handles Login1.Authenticate
Dim studentId As Integer = 0
Dim constr As String = ConfigurationManager.ConnectionStrings("DatabaseConnectionString").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("Validate_Student")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Username", Login1.UserName)
cmd.Parameters.AddWithValue("@Password", Login1.Password)
cmd.Connection = con
con.Open()
studentId = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
End Using
Select Case studentId
Case -1
Login1.FailureText = "Username and/or password is incorrect."
Exit Select
Case -2
Login1.FailureText = "Account has not been activated."
Exit Select
Case Else
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet)
Exit Select
End Select
End Using
End Sub
End Class
我的目标是继续使用 asp:login
控件并弄清楚如何在登录后允许多个用户重定向....或者 最好 ,不进行身份验证使用 asp:login
并使用我原来的 HTML 和 CSS。这是我在使用 asp 控件之前的内容:
<%-- STUDENT LOGIN --%>
<div class="student_login">
<div id="wrapper">
<form name="login-form" class="login-form" action="" method="post">
<div class="header">
<h1>Student Login</h1>
<span>Sign in below to login to start showing your skills, earning money, and gaining valuable experience...and did I mention earning MONEY.</span>
</div>
<div class="content">
<input name="username" type="text" class="input username" placeholder="Username" />
<input name="password" type="password" class="input password" placeholder="Password" />
</div>
<div class="footer">
<input type="submit" name="submit" value="Login" class="button" />
<a href="ExteriorStudentTestimonials.aspx#register" class="register">Register</a>
</div>
</form>
</div>
</div><!--close student_login-->
注意:这部分代码是在内容占位符中编写的。母版页确实有一个包含页面主体的 <form runat="server"></form>
标记。我没有此特定方法的任何返回代码,因为我真的不知道如何在不使用 asp:login
控件和 FormsAuthentication
的情况下进行身份验证。有人可以帮助我了解如何在没有 asp:login
的情况下完成身份验证,以便我可以保留设计精美的登录表单吗?请慷慨或友善地对待 comments/explanation,这是我的第一个登录表单,所以我没有这样做的经验。
你可以使用 Login
控件的 LayoutTemplate
来做你想做的事情。
诀窍是你需要使用 Literal
和 FailureText 的 ID
,用户字段是 TextBox
UserName 的 ID
,以及 Password 的 ID
的另一个 TextBox
。
您还需要触发 Login
命令,它可以是 Button
.
这允许您自定义您自己的布局,我自己做过几次。下面是一个 copy/paste 未经测试的快速版本,以您的 HTML 为例。
请参阅 https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.layouttemplate.aspx
上的 MSDN 参考
<asp:Login ID="Login1" runat="server"
OnAuthenticate="ValidateStudent"
Width="100%">
<LayoutTemplate>
<div class="student_login">
<div id="wrapper">
<asp:Literal ID="FailureText" runat="server"></asp:Literal>
<asp:ValidationSummary ID="vsLogin" runat="server" ValidationGroup="Login" />
<div class="header">
<h1>Student Login</h1>
<span>Sign in below to login to start showing your skills, earning money, and gaining valuable experience...and did I mention earning MONEY.</span>
</div>
<div class="content">
<asp:TextBox id="UserName" runat="server" CssClass="input username" placeholder="Enter your username"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvUserName" runat="server"
ControlToValidate="UserName"
Display="None"
ErrorMessage="Username is required"
ValidationGroup="Login">
</asp:RequiredFieldValidator>
<asp:TextBox id="Password" runat="server" CssClass="input password" TextMode="Password" placeholder="Password"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvPassword" runat="server"
ControlToValidate="Password"
Display="None"
ErrorMessage="Password is required"
ValidationGroup="Login">
</asp:RequiredFieldValidator>
</div>
<div class="footer">
<asp:Button ID="Login" runat="server" CommandName="Login" CssClass="button" Text="Login" ValidationGroup="Login" />
<a href="ExteriorStudentTestimonials.aspx#register" class="register">Register</a>
</div>
</div>
</div>
</LayoutTemplate>
</asp:Login>
我制作了一个非常漂亮的登录页面,我在其中使用 HTML 输入标签作为我的用户名、密码和登录按钮。我有两种不同的登录表单,一种用于每种类型的用户(学生用户和企业用户)。我从来没有对数据库进行过身份验证,所以我认为我的方法很容易执行。在网上搜索使用我已有的方法来实现的简单方法后,我发现在不使用 asp:login
控件的情况下几乎不可能弄清楚如何实现我的目标。不情愿地,我放弃了我拥有的东西并投入 asp:login
控制。我的问题是,实现起来很痛苦(创建存储过程,并弄乱 web.config),它看起来很可怕,因为它不符合我之前写的 CSS,并且我使用的 FormsAuthentication 只允许我在登录后重定向一个用户(登录成功后重定向到主门户,如果登录失败则返回登录页面。)在我的 Web.config:[=24 中使用身份验证表单模式=]
<forms defaultUrl="~/InteriorStudentPortal.aspx" loginUrl="~/ExteriorLogin.aspx" slidingExpiration="true" timeout="2880"></forms>
因为我有两个用户,所以我需要根据登录的用户进行重定向。这是我一直在使用的 asp:login
控件的示例:
<asp:Login ID="Login1" runat="server" CssClass="Login" OnAuthenticate= "ValidateStudent" Width="313px"></asp:Login>
连同后台代码:
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.Security
Partial Class ExteriorLogin
Inherits System.Web.UI.Page
Protected Sub ValidateStudent(sender As Object, e As AuthenticateEventArgs) Handles Login1.Authenticate
Dim studentId As Integer = 0
Dim constr As String = ConfigurationManager.ConnectionStrings("DatabaseConnectionString").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("Validate_Student")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Username", Login1.UserName)
cmd.Parameters.AddWithValue("@Password", Login1.Password)
cmd.Connection = con
con.Open()
studentId = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
End Using
Select Case studentId
Case -1
Login1.FailureText = "Username and/or password is incorrect."
Exit Select
Case -2
Login1.FailureText = "Account has not been activated."
Exit Select
Case Else
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet)
Exit Select
End Select
End Using
End Sub
End Class
我的目标是继续使用 asp:login
控件并弄清楚如何在登录后允许多个用户重定向....或者 最好 ,不进行身份验证使用 asp:login
并使用我原来的 HTML 和 CSS。这是我在使用 asp 控件之前的内容:
<%-- STUDENT LOGIN --%>
<div class="student_login">
<div id="wrapper">
<form name="login-form" class="login-form" action="" method="post">
<div class="header">
<h1>Student Login</h1>
<span>Sign in below to login to start showing your skills, earning money, and gaining valuable experience...and did I mention earning MONEY.</span>
</div>
<div class="content">
<input name="username" type="text" class="input username" placeholder="Username" />
<input name="password" type="password" class="input password" placeholder="Password" />
</div>
<div class="footer">
<input type="submit" name="submit" value="Login" class="button" />
<a href="ExteriorStudentTestimonials.aspx#register" class="register">Register</a>
</div>
</form>
</div>
</div><!--close student_login-->
注意:这部分代码是在内容占位符中编写的。母版页确实有一个包含页面主体的 <form runat="server"></form>
标记。我没有此特定方法的任何返回代码,因为我真的不知道如何在不使用 asp:login
控件和 FormsAuthentication
的情况下进行身份验证。有人可以帮助我了解如何在没有 asp:login
的情况下完成身份验证,以便我可以保留设计精美的登录表单吗?请慷慨或友善地对待 comments/explanation,这是我的第一个登录表单,所以我没有这样做的经验。
你可以使用 Login
控件的 LayoutTemplate
来做你想做的事情。
诀窍是你需要使用 Literal
和 FailureText 的 ID
,用户字段是 TextBox
UserName 的 ID
,以及 Password 的 ID
的另一个 TextBox
。
您还需要触发 Login
命令,它可以是 Button
.
这允许您自定义您自己的布局,我自己做过几次。下面是一个 copy/paste 未经测试的快速版本,以您的 HTML 为例。
请参阅 https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.layouttemplate.aspx
上的 MSDN 参考<asp:Login ID="Login1" runat="server"
OnAuthenticate="ValidateStudent"
Width="100%">
<LayoutTemplate>
<div class="student_login">
<div id="wrapper">
<asp:Literal ID="FailureText" runat="server"></asp:Literal>
<asp:ValidationSummary ID="vsLogin" runat="server" ValidationGroup="Login" />
<div class="header">
<h1>Student Login</h1>
<span>Sign in below to login to start showing your skills, earning money, and gaining valuable experience...and did I mention earning MONEY.</span>
</div>
<div class="content">
<asp:TextBox id="UserName" runat="server" CssClass="input username" placeholder="Enter your username"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvUserName" runat="server"
ControlToValidate="UserName"
Display="None"
ErrorMessage="Username is required"
ValidationGroup="Login">
</asp:RequiredFieldValidator>
<asp:TextBox id="Password" runat="server" CssClass="input password" TextMode="Password" placeholder="Password"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvPassword" runat="server"
ControlToValidate="Password"
Display="None"
ErrorMessage="Password is required"
ValidationGroup="Login">
</asp:RequiredFieldValidator>
</div>
<div class="footer">
<asp:Button ID="Login" runat="server" CommandName="Login" CssClass="button" Text="Login" ValidationGroup="Login" />
<a href="ExteriorStudentTestimonials.aspx#register" class="register">Register</a>
</div>
</div>
</div>
</LayoutTemplate>
</asp:Login>