C#_登录_免注册_API
写在CommonClass.cs
/// 发送Web-Api-Post请求 /// </summary> /// <param name="url">请求地址</param> /// <param name=" body">参数格式</param> /// <myparam1>参数格式 http或https /// <myparam2>参数格式 yes:表示输入json或字符串型, no:表示要输入参数,body例:name=张三&password=123456 /// <returns></returns> public string ApiPost(string url, string body, string myparamhttp, string myparam2) { Encoding encoding = Encoding.UTF8; if (myparamhttp.Trim() == "https") { ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate; } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "text/html, application/xhtml+xml, */*"; if (myparam2.Trim() == "yes") { request.ContentType = "application/json"; } else { request.ContentType = "application/x-www-form-urlencoded"; } byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } }
写在new_login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="new_login.aspx.cs" Inherits="new_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> <div class="login-container"> <h2 font-family: '微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑';>用户登录</h2> <div> <label for="txtUserId">UserId:</label> <asp:TextBox ID="工号" runat="server" CssClass="form-control" placeholder="1. 请输入用户ID"></asp:TextBox> </div> <div> <label for="txtPassword">Password:</label> <asp:TextBox ID="密码" runat="server" TextMode="Password" CssClass="form-control" placeholder="2. 请输入密码"></asp:TextBox> </div> <br /> <div> <asp:TextBox ID="密码类型" runat="server" CssClass="form-control" text="windows登录密码" ></asp:TextBox> </div> <br /> <div> <asp:TextBox ID="密码验证状态" runat="server" CssClass="form-control" ></asp:TextBox> </div> <br /> <div> <asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" CssClass="btn btn-primary" /> </div> </div> <script src="Scripts/jquery/jquery-3.1.1.min.js"></script> <script src="Scripts/bootstrap/bootstrap.min.js"></script> </div> </form> </body> </html>
写在new_login.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class new_login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //导入自定义标准库 CommonClass class1 = new CommonClass(); protected void btnLogin_Click(object sender, EventArgs e) { if (this.密码类型.Text.Trim() == "windows登录密码") { string myurl = "http://????/api/Value6"; string mybody = "mycode=" + this.工号.Text.Trim() + "&mypassword=" + this.密码.Text.Trim(); string myresult = class1.ApiPost(myurl, mybody, "http", "no").Trim(); int a = myresult.IndexOf("OK"); if (a != -1) { //this.传递工号.Text = this.工号.Text.Trim(); this.密码验证状态.Text = "OK"; //this.传递密码.Text = this.密码.Text.Trim(); } else { //this.密码验证状态.Text = "待验证"; this.传递工号.Text = null; Response.Write(class1.MessageBox("windows登录密码验证错误!")); return; } } } }
浙公网安备 33010602011771号