ASP.NET Database Login
This is for all you ASP programmers out there looking for a way to validate users against a SQL database and are used to the old ADO RecordSource.EOF way like myself. You must change to represent your database.
AI
Riepilogo AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Codice sorgente
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="700" id="AutoNumber1"> <tr> <td width="100%"><font size="2"><%@ Page Language="vb" AutoEventWireup="false" Codebehind="index.aspx.vb" Inherits="VirtualManagement.Login"%><br> <%@ Register TagPrefix="uc1" TagName="Menu" Src="Menu.ascx" %><br> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><br> <HTML><br> <HEAD><br> <title>Login</title><br> <meta content="False" name="vs_showGrid"><br> <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR"><br> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE"><br> <meta content="JavaScript" name="vs_defaultClientScript"><br> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"><br> <script language="VB" runat="server"><br> <br> Sub Login_Click(Sender As Object, E As EventArgs)<br> <font color="#008000"> ' Robert Long<br> ' GlobalDeveloping.com<br> ' This is for all you ASP programmers out there looking for <br> ' a way to validate users against a SQL database and are used to <br> ' the old ADO RecordSource.EOF way like myself.</font><br> <br> Dim StrUser As String, StrPass As String<br> Dim BValid As Boolean<br> Dim ConVM As SqlConnection<br> Dim CmdCoInfo As SqlCommand<br> Dim DrCoInfo As SqlDataReader<br> Dim StrSQL As String<br> <br> <font color="#008000"> ' We will request all variables from our form with this.<br> ' Look Mom "No QueryStrings".</font><br> StrUser = TxtUser.Text<br> StrPass = TxtPass.Text<br> <br> <font color="#008000"> ' This is our boolean variable for validation purposes set to true if valid user</font><br> BValid = False<br> <br> <font color="#008000"> ' Initialize Database Connection<br> ' Use whatever server you have</font><br> ConVM = New SqlConnection("Server=MyServer\NetSDK;UID=sa;PWD=;Database=Pubs")<br> <br> <font color="#008000"> ' Create Select Command<br> ' Also, notice that it is only selecting a record if the username/password match<br> ' Otherwise it selects nothing<br> ' Make sure you change to match your variables</font><br> StrSQL = "SELECT * FROM Pubs WHERE UserName='" & StrUser & "'"<br> CmdCoInfo = New SqlCommand(StrSQL, ConVM)<br> <br> <font color="#008000"> ' Retrieve the record that matches the username/password</font><br> ConVM.Open()<br> <br> <br> DrCoInfo = CmdCoInfo.ExecuteReader()<br> <br> <font color="#008000">' This acts like the (Not RecordSource.Eof) in ASP 3.0</font><br> While DrCoInfo.Read()<br> If DrCoInfo("UserName") <> "" And DrCoInfo("Password") <> "" Then<br> '(Optional) Write the username to a cookie to personilize the site<br> Response.Cookies("ValidUser").Value = DrCoInfo("UserName")<br> Response.Cookies("ValidUser").Expires = DateTime.Now.AddMonths(1)<br> BValid = True<br> Else<br> End If<br> End While<br> <font color="#008000"><br> ' Don't forget this</font><br> ConVM.Close()<br> <font color="#008000"><br> ' This handles all response per validation<br> ' If validated it goes to a welcome.aspx page</font><br> If BValid = True Then<br> Response.Redirect("Welcome.aspx")<br> ElseIf BValid = False Then<br> LblError.Text = "Login Error: Please try again."<br> End If<br> End Sub<br> <br> </script> <br> </HEAD><br> <body bottomMargin="0" bgColor="gray" leftMargin="0" topMargin="0" rightMargin="0" MS_POSITIONING="GridLayout"><br> <form runat="server"><br> <asp:panel id="PnlLogin" style="Z-INDEX: 101; LEFT: 29px; POSITION: absolute; TOP: 72px" runat="server" HorizontalAlign="Center" Height="184px" BorderColor="Transparent" BorderWidth="2px" BorderStyle="Outset" BackColor="LightSteelBlue" Width="326px"><br> <P>&nbsp;</P><br> <P><br> <asp:Label id="LblUser" runat="server" Width="73px" Font-Names="Arial" Font-Size="X-Small">Username</asp:Label><br> <asp:TextBox id="TxtUser" runat="server" Width="120px" BackColor="Azure" Height="20px"></asp:TextBox></P><br> <P><br> <asp:Label id="LblPass" runat="server" Width="73px" Font-Names="Arial" Font-Size="X-Small">Password</asp:Label><br> <asp:TextBox id="TxtPass" runat="server" Width="120px" BackColor="Azure" Height="20px" TextMode="Password"></asp:TextBox></P><br> <P><br> <asp:Button id="CmdLogin" runat="server" Width="99px" BackColor="Gold" Height="25px" Font-Names="Arial" Font-Size="X-Small" OnServerClick="Login_Click" Text="Log-in Now" ForeColor="Black"></asp:Button></P><br> </asp:panel><asp:panel id="PnlTitle" style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute; TOP: 75px" runat="server" HorizontalAlign="Left" Height="19px" BorderColor="Navy" BorderWidth="1px" BorderStyle="Solid" BackColor="DarkBlue" Width="320px" Font-Names="Arial" Font-Size="X-Small" ForeColor="White" Font-Bold="True">&nbsp;Log-on Window</asp:panel>&nbsp;<br> <asp:Label id="LblError" style="Z-INDEX: 103; LEFT: 46px; POSITION: absolute; TOP: 46px" runat="server" Width="288px" Font-Names="Arial Black" ForeColor="Yellow"></asp:Label><br> </form><br> </body><br> </HTML></font></td> </tr> </table> <p> </p> <p><font size="2"><br> </font></p>
Commenti originali (3)
Recuperato da Wayback Machine