C#_登录_普通

写在Login.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login - 复制.aspx.cs" Inherits="Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>登录页面</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />

    <style>
        body {
            background-color: #48b7c9; /* #c3e7f5 马卡龙蓝色 */
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
        }

        .login-container {
            background-color: #fff; /* 白色背景 */
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        .login-container label {
            font-weight: bold;
            color: #007bff; /* Bootstrap 蓝色 */
        }

        .login-container button {
            background-color: #007bff; /* Bootstrap 蓝色 */
            color: #fff; /* 白色字体 */
            width: 100%;
            margin-top: 10px;
        }

        .login-container button:hover {
            background-color: #0056b3; /* 深蓝色 */
        }

        .login-container a {
            color: #007bff; /* Bootstrap 蓝色 */
            text-decoration: none;
        }

        .login-container a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body>
    <form id="form1" runat="server">        
        <div class="login-container">
            <h2 font-family: '微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑';>用户登录</h2>
            <div>
                <label for="txtUserId">UserId:</label>
                <asp:TextBox ID="txtUserId" runat="server" CssClass="form-control" placeholder="1. 请输入用户ID"></asp:TextBox>
            </div>
            <div>
                <label for="txtPassword">Password:</label>
                <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" CssClass="form-control" placeholder="2. 请输入密码"></asp:TextBox>
            </div>
            <div>
                <asp:Button ID="btnLogin" runat="server" Text="登录" OnClientClick="validateLogin();" OnClick="btnLogin_Click" CssClass="btn btn-primary" />
            </div>
        </div>

        <%--<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>--%>
        <%--<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>--%>
        <script src="Scripts/jquery/jquery-3.1.1.min.js"></script>        
        <script src="Scripts/bootstrap/bootstrap.min.js"></script>

        <script>
            function validateLogin() {
                var userid = $('#<%= txtUserId.ClientID %>').val();
                var password = $('#<%= txtPassword.ClientID %>').val();

                if (!userid || !password) {
                    alert('密码和用户ID不能为空');
                    return false;
                }
            }
        </script>
    

    </form>
</body>
</html>
View Code

 

写在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 System.Data.SqlClient;


public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string userid = txtUserId.Text;
        string password = txtPassword.Text;

        //if (string.IsNullOrEmpty(userid) || string.IsNullOrEmpty(password))
        //{
        //    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('密码和用户ID不能为空');", true);
        //    return;
        //}

        // 在这里,你需要连接数据库,检查用户名和密码是否匹配
        // 以下是一个简单的示例,假设用户名和密码储存在 USERId 表中
        string connectionString = "server=***.***.***.***;database=?;uid=?;pwd=?";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            string query = "SELECT COUNT(*) FROM Exp_USERId WHERE UserId = @UserId AND Password = @Password";
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                command.Parameters.AddWithValue("@UserId", userid);
                command.Parameters.AddWithValue("@Password", password);

                int count = (int)command.ExecuteScalar();

                if (count > 0)
                {
                    // 登录成功,重定向到 index.aspx
                    Response.Redirect("index.aspx");
                }
                else
                {
                    // 登录失败,清空输入框,并弹出错误提示
                    txtUserId.Text = string.Empty;
                    txtPassword.Text = string.Empty;
                    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('密码或用户ID错误');", true);
                }
            }
        }
    }


}
View Code

 

写在SQL

 

 

 

posted @ 2024-01-26 13:46  AutomationAnywhere  阅读(33)  评论(0)    收藏  举报