使用 asp.net C# 以管理员和用户身份登录

本文关键字:用户 身份 登录 管理员 asp net 使用 | 更新日期: 2023-09-27 18:31:01

请帮帮我。我是新手。在这里也找到了这个解决方案。身份验证有效,但重定向部分无效。它总是重定向到 Default.Aspx,管理员应该重定向到添加.aspx请帮忙:'(提前感谢!

这是我的代码。

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">
    &nbsp;</p>
<p class="style1">
    Username:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBox1" runat="server" Width="167px"></asp:TextBox>
</p>
<p class="style1">
    Password:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBox2" runat="server" Width="167px"></asp:TextBox>
</p>
<p class="style1">

                                                 

还不是会员?点击 这里 .

表结构:数据库:用户数据-用户名-密码-许可

使用 asp.net C# 以管理员和用户身份登录

您还将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.";
    }
}