有人请通过修改此代码来帮助我。在我的登录页面中我有三列Login,UserName,Password.In登录我保留了下拉列表,其中包含两个列表项Admin和User.So当我选择Admin,UserName,Password应该转到所需的目标页面,当我选择用户,用户名,密码时,它应该转到另一个所需的目标页面。这是我的代码。请帮帮我......
Login.aspx:
<asp:Label ID="lblLogin" runat="server" Text="Login" CssClass="Label"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem>Admin</asp:ListItem>
<asp:ListItem>User</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblUserName" runat="server" Text="User Name"</asp:Label>
<asp:TextBox ID="TxtUserName" runat="server" TextMode="SingleLine"></asp:TextBox>
<asp:Label ID="lblPassword" runat="server" Text="Password" CssClass="Label"></asp:Label>
<asp:TextBox ID="TxtPassword" runat="server" TextMode="Password"></asp:TextBox>
<td align="center">
<asp:Button ID="BtnLogin" runat="server" Text="Login"
onclick="BtnLogin_Click"/>
Login.aspx.cs:
protected void BtnLogin_Click(object sender, EventArgs e)
{
Session["UserName"] = TxtUserName.Text;
Login lg = new Login();
if ((lg.GetLogin(TxtUserName.Text, TxtPassword.Text) == 1))
{
Response.Redirect("c1.aspx");
}
else
{
Lbl1.Text = "Sorry,Invalid UserName or Password";
}
Login.cs:
public class Login
{ public string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
public int GetLogin(string UserName, string Password)
{
SqlConnection con = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("select * from Login where UserName='"+UserName+"' and Password='"+Password+"'",con);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
if ((ds.Tables[0].Rows[0].ItemArray[1].ToString() == UserName) && (ds.Tables[0].Rows[0].ItemArray[2].ToString() == Password))
{
return 1;
}
else
{
return 0;
}
}
else
{
return -1;
}
}
谢谢, Masum
答案 0 :(得分:1)
首先,您打开应用程序和SQL注入攻击。 我建议至少使用存储过程来查询数据库的登录凭据,以最大限度地降低这种风险 用户输入的第一条规则 - 不信任用户输入 可能值得看看这个问题以获得进一步的帮助:
What is the best way to avoid sql injection attacks
至于您请求帮助的问题,如果GetLogin()方法成功,您肯定需要根据下拉列表的选定值做出重定向决定。
protected void BtnLogin_Click(object sender, EventArgs e)
{
Session["UserName"] = TxtUserName.Text;
Login lg = new Login();
if ((lg.GetLogin(TxtUserName.Text, TxtPassword.Text) == 1))
{
if(DropDownList1.SelectedValue == "Admin")
{
Response.Redirect("c1.aspx");
}
else if(DropDownList1.SelectedValue == "User")
{
Response.Redirect("c2.aspx");
}
}
else
{
Lbl1.Text = "Sorry,Invalid UserName or Password";
}
答案 1 :(得分:0)
您可能希望了解实施Forms Authentication with Role-based security。然后,您可以根据用户是否具有特定角色进行重定向。像 -
这样的东西//if (HttpContext.Current.User.Identity.IsAuthenticated)
//{
if (User.IsInRole("Administrator"))
{
Response.Redirect("myAdminPage.aspx");
}
else if (User.IsInRole("User"))
{
Response.Redirect("myUserPage.aspx");
}
//}
else
{
Lbl1.Text = "Sorry,Invalid UserName or Password";
}