通过mysql workbench使用rds数据库的aspx.net登录页

cgyqldqp  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(375)

我已经开发了一个登录页面,并通过mysql workbench连接到我的awsrds数据库。我已经创建了表,可以插入新的用户注册。但是对于login.aspx,我正在为login.aspx.cs编写与本地数据库连接相同的代码,但是我的代码似乎不起作用,当我单击login按钮时,它不会带我到主页。有人能帮我找出我做错了什么吗?提前谢谢!这是我的login.aspx;

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="loginfinal.aspx.cs" Inherits="Aname.login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<script src="Scripts/jquery-1.9.1.min.js"></script>
<script src="Scripts/moment.min.js"></script>
<script src="Scripts/bootstrap-datetimepicker.min.js"></script>

</head>
<body>
    <form id="form1" runat="server">

            <asp:Image runat="server" imageURL="~/images/PlantManager.jfif" width="200px"/>
            <asp:Image ImageAlign="Middle" runat="server" imageURL="~/images/quote.jfif" height="128px" />
            <asp:Image runat="server" imageURL="~/images/TeamLogo.jfif" width="126px"/>

            <div style="text-align:right">
               <asp:Hyperlink ID="Hyperlink1" NavigateUrl="AboutUsPM.aspx" runat="server">About Plant Manager </asp:Hyperlink>

            </div>

            <div class="form-group">
            <table class="auto-style1">
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">

            <asp:label runat="server"> <b> UserID </b> </asp:label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">
            <asp:TextBox id="Txtid" CssClass="auto-style2" placeholder="Enter UserID" runat="server" Height="42px" Width="55%"/></td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Txtid" Display="Dynamic" ErrorMessage="You cannot leave this blank." ForeColor="Red"></asp:RequiredFieldValidator>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">

            <asp:label runat="server"> <b> Password </b> </asp:label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">
            <asp:TextBox id="TxtPsw" CssClass="auto-style5" Textmode="Password" placeholder="Enter Password" runat="server" Height="42px" Width="55%"/></td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TxtPsw" Display="Dynamic" ErrorMessage="You cannot leave this blank." ForeColor="Red"></asp:RequiredFieldValidator>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">

            <asp:Checkbox ID="Chkrmb" CssClass="checkbox-inline" Text="Remember me" runat="server" /></td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">

                        &nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">

            <asp:Hyperlink ID="MyHyperLinkControl" NavigateUrl="~/forgetpsw.aspx" runat="server">Forget password? </asp:Hyperlink>

                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">

            <asp:Button ID="Btnlogin" CssClass="auto-style6" Text="Login" OnClick="Btnlogin_Click" style="color:white" onmouseover="this.style.textDecoration='underline'" onmouseout="this.style.textDecoration='none'" runat="server" />

                    </td>
                    <td>&nbsp;</td>
                </tr>
                </table>

            &nbsp;</div>

</form>

login.aspx.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;

namespace Aname
{
    public partial class login : System.Web.UI.Page
    {
//rds connection string
        static string connection = @"Server=rds-mysql.xxxxxxxx.us-west-2.rds.amazonaws.com; Port=xxxx; Database=mydb; User Id=xxxx; password=xxxxx";
    MySqlConnection sqlcon = new MySqlConnection(connection);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData("");

        }
    }

    private void LoadData(string text)
    {
        DataTable dt = new DataTable();
        using (MySqlConnection sqlquery = new MySqlConnection(connection))
        {
            string query = "SELECT user_id, name, contact_no, email, role FROM mydb.User ; ";
            MySqlDataAdapter adpter = new MySqlDataAdapter(query, sqlquery);
            adpter.Fill(dt);
        }
        if (dt.Rows.Count > 0)
        {
            Session["user_id"] = Txtid.Text;
            Response.Redirect("~/home.aspx");
        }
    }
    protected void Btnlogin_Click(object sender, EventArgs e)
    {
        sqlcon.Open();
        string query = "select count(*) from mydb.User where user_id='" + Txtid.Text + "'and pass='" + TxtPsw.Text + "'";

        MySqlCommand cmd = new MySqlCommand(query, sqlcon);
        int output = Convert.ToInt32(cmd.ExecuteScalar().ToString());
        sqlcon.Close();

        if (output == 1)
        {
            sqlcon.Open();
            string checkPasswordQuery = "select password from mydb.User where user_id='" + Txtid.Text + "'";
            MySqlCommand checkpsw = new MySqlCommand(query, sqlcon);
            string password = checkpsw.ExecuteScalar().ToString();

            if (password == TxtPsw.Text)
            {
                Session["user_id"] = Txtid.Text;
                Response.Redirect("~/home.aspx");
            }
            else
            {
                Response.Write("Password is incorrect!");
            }
        }
        else
        {
            Response.Write("UserID is incorrect!");
        }

     }

}
}
vx6bjr1n

vx6bjr1n1#

如果有人想知道,这是我更新的login.aspx.cs代码;

public partial class login : System.Web.UI.Page
{
    static string connection = @"Server=rds-mysql.xxxxxxxx.us-west-2.rds.amazonaws.com; Port=xxxx; Database=mydb; User Id=xxxx; password=xxxxx";
    MySqlConnection sqlcon = new MySqlConnection(connection);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            LoadData("");
        }
    }

    private void LoadData(string text)
    {
        sqlcon.Open();
        string query = "select * from mydb.User where user_id='" + Txtid.Text + "'";

        MySqlCommand cmd = new MySqlCommand(query, sqlcon);
        try
        {
            string output = cmd.ExecuteScalar().ToString();
            string checkPasswordQuery = "select password from mydb.User where password='" + TxtPsw.Text + "'";

            MySqlCommand checkpsw = new MySqlCommand(checkPasswordQuery, sqlcon);
            string password = null;
            try
            {
                password = checkpsw.ExecuteScalar().ToString();
                Session["user_id"] = Txtid.Text;
                Response.Redirect("~/home.aspx");
            }
            catch (Exception e)
            {
                Ltlup.Text = "UserID or password is incorrect!";
            }
        }
        catch(Exception e)
        {
            Ltlup.Text = "UserID or password is incorrect!";
        }

相关问题