C# 使用asp.net c与管理员和用户登录#
请帮帮我。我是个新手。在这里也找到了这个解决方案。身份验证有效,但重定向部分无效。它总是重定向到Default.Aspx。但是管理员应该被重定向到Add.Aspx。请帮助:'(提前感谢 这是我的密码C# 使用asp.net c与管理员和用户登录#,c#,asp.net,login,admin,C#,Asp.net,Login,Admin,请帮帮我。我是个新手。在这里也找到了这个解决方案。身份验证有效,但重定向部分无效。它总是重定向到Default.Aspx。但是管理员应该被重定向到Add.Aspx。请帮助:'(提前感谢 这是我的密码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Syste
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace CRUD
{
public partial class Login1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string conn = "";
conn = ConfigurationManager.ConnectionStrings["employee1ConnectionString"].ToString();
SqlConnection objsqlconn = new SqlConnection(conn);
objsqlconn.Open();
SqlCommand cmd = new SqlCommand("select * from userdata where username=@username and password=@password", objsqlconn);
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox1.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["permission"].ToString() == "admin")
Response.Redirect("Add.aspx");
else
Response.Redirect("Default.aspx");
}
else
{
Label1.Text = "Invalid username or password. Please try again.";
}
}
}
}
而且
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login1.aspx.cs" Inherits="CRUD.Login1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<p class="style1">
LOG IN</p>
<p class="style1">
</p>
<p class="style1">
Username:
<asp:TextBox ID="TextBox1" runat="server" Width="167px"></asp:TextBox>
</p>
<p class="style1">
Password:
<asp:TextBox ID="TextBox2" runat="server" Width="167px"></asp:TextBox>
</p>
<p class="style1">
.style1
{
文本对齐:居中;
}
登录
用户名:
密码:
尚未成为会员?单击
在这里
表结构:
数据库:用户数据
-用户名
-密码
-权限您同时将
textbox1
值赋予密码。进行如下更改
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", txtPassword.Text);
请使用正确的语法输入代码,并检查:-
protected void Button1_Click(object sender, EventArgs e)
{
string conn = "";
conn = ConfigurationManager.ConnectionStrings["employee1ConnectionString"].ToString();
SqlConnection objsqlconn = new SqlConnection(conn);
objsqlconn.Open();
SqlCommand cmd = new SqlCommand("select * from userdata where username=@username and password=@password", objsqlconn);
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox1.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["permission"].ToString() == "admin")
{
Response.Redirect("Add.aspx");
}
else
{
Response.Redirect("Default.aspx");
}
}
else
{
Label1.Text = "Invalid username or password. Please try again.";
}
}
调试代码时,
dt.Rows[0][“permission”].ToString()
的值是多少?并且不要将密码存储为纯文本。调试代码,可能有一些错误我同意@SonerGönül,不要将密码存储为纯文本。我的权限值是“admin”或“user”您不将密码存储为纯文本是什么意思?您好,是的。抱歉弄错了。已经修复了它,但它仍然是一样的:(您调试了代码吗?您在dt.Rows[0][“permission”].ToString()处得到了什么值?是的。当我将它传递给标签时,它会得到admin或user.:(但不要重定向。请分享您的完整代码和表结构,我会检查并让您知道。我会尝试!:)非常感谢您,纳迪姆先生。我会更新您的。我也编辑了我的帖子,您可以看到我的完整代码