第一选择选项总是返回登录错误,即使密码在数据库中是正确的
本文关键字:数据库 密码 选项 选择 返回 错误 登录 | 更新日期: 2023-09-27 18:05:42
我有一个相当奇怪的问题与ASP。净c#。
似乎我可以登录与任何其他选项在我的选择下拉除了第一个。我已经检查了数据库中的密码,它是正确的。
有人知道为什么会这样吗?
下面是我的代码:. aspx
<form id="loginForm" runat="server">
<asp:Login runat="server" OnAuthenticate="Login1_Authenticate" Width="293px" Height="172px"
ID="Login_Box">
<LayoutTemplate>
<!-- department -->
<div id="dept" style="text-align: left">
Department<br />
<asp:DropDownList ID="UserName" runat="server" DataSourceID="SqlDataSource1" DataTextField="dept"
DataValueField="id" DataMember="DefaultView">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:team13ConnectionString %>"
SelectCommand="SELECT [id], [dept] FROM [ts_dept] ORDER BY [dept]"></asp:SqlDataSource>
</div>
<!-- password -->
<div id="pass" style="text-align: left">
Password:<br />
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1"
ForeColor="Red" Display="Dynamic">Password is required.</asp:RequiredFieldValidator>
</div>
<!-- failure text -->
<div style="text-align: left; color: Red">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False" Mode="Encode"></asp:Literal>
</div>
<br />
<!-- sign in button -->
<div class="btn" style="float: right" runat="server">
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1"
CssClass="cupid-green"/>
</div>
</LayoutTemplate>
</asp:Login>
</form>
Login.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Configuration;
public partial class login : System.Web.UI.Page
{
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string username = Login_Box.UserName;
string pwd = Login_Box.Password;
string strConn;
strConn = WebConfigurationManager.ConnectionStrings["team13ConnectionString"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();
string sqlUserName;
sqlUserName = "SELECT id,pass FROM ts_dept ";
sqlUserName += " WHERE (id ='" + username + "')";
sqlUserName += " AND (pass ='" + pwd + "')";
SqlCommand com = new SqlCommand(sqlUserName, Conn);
string CurrentName;
CurrentName = (string)com.ExecuteScalar();
if (CurrentName != null)
{
Session["UserAuthentication"] = username;
Session.Timeout = 1;
Response.Redirect("Default.aspx");
}
else
{
Session["UserAuthentication"] = "";
}
}
}
一切看起来都很好,我认为我们需要查看数据库以确保用户实际上具有正确的部门id。