客户端实现:login
namespace LoginApp { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.txtPwd = new System.Windows.Forms.TextBox(); this.txtUserName = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.btnClose = new System.Windows.Forms.Button(); this.btnLogin = new System.Windows.Forms.Button(); this.SuspendLayout(); // // txtPwd // this.txtPwd.Location = new System.Drawing.Point(191, 100); this.txtPwd.Margin = new System.Windows.Forms.Padding(4); this.txtPwd.Name = "txtPwd"; this.txtPwd.PasswordChar = '*'; this.txtPwd.Size = new System.Drawing.Size(124, 25); this.txtPwd.TabIndex = 11; this.txtPwd.Text = "admin"; // // txtUserName // this.txtUserName.Location = new System.Drawing.Point(191, 55); this.txtUserName.Margin = new System.Windows.Forms.Padding(4); this.txtUserName.Name = "txtUserName"; this.txtUserName.Size = new System.Drawing.Size(124, 25); this.txtUserName.TabIndex = 10; this.txtUserName.Text = "admin"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(78, 100); this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(37, 15); this.label2.TabIndex = 9; this.label2.Text = "密码"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(78, 55); this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(52, 15); this.label1.TabIndex = 8; this.label1.Text = "用户名"; // // btnClose // this.btnClose.Location = new System.Drawing.Point(207, 168); this.btnClose.Margin = new System.Windows.Forms.Padding(4); this.btnClose.Name = "btnClose"; this.btnClose.Size = new System.Drawing.Size(125, 29); this.btnClose.TabIndex = 7; this.btnClose.Text = "关闭"; this.btnClose.UseVisualStyleBackColor = true; this.btnClose.Click += new System.EventHandler(this.btnClose_Click); // // btnLogin // this.btnLogin.Location = new System.Drawing.Point(55, 168); this.btnLogin.Margin = new System.Windows.Forms.Padding(4); this.btnLogin.Name = "btnLogin"; this.btnLogin.Size = new System.Drawing.Size(125, 29); this.btnLogin.TabIndex = 6; this.btnLogin.Text = "登录"; this.btnLogin.UseVisualStyleBackColor = true; this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(387, 253); this.Controls.Add(this.txtPwd); this.Controls.Add(this.txtUserName); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.btnClose); this.Controls.Add(this.btnLogin); this.Name = "Form1"; this.Text = "登录"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox txtPwd; private System.Windows.Forms.TextBox txtUserName; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button btnClose; private System.Windows.Forms.Button btnLogin; } }
后台登陆代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace LoginApp { public partial class Form1 : Form { string strURL = "http://localhost:50229/Ashx/Login.ashx"; public Form1() { InitializeComponent(); } /// <summary> /// 登录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLogin_Click(object sender, EventArgs e) { string name = this.txtUserName.Text.Trim(); string pwd = this.txtPwd.Text.Trim(); // string postData = name;//要提交的数据 //GetPage(strURL, postData); //定义返回json数据 解析json数据并显示信息到窗体控件值上 // string jsoncontent = GetPage(strURL, postData); //List<Custerm> CustermList = JsonConvert.DeserializeObject<List<Custerm>>(jsoncontent); //foreach (Custerm custerm in CustermList) //{ // this.txtName.Text = custerm.CustermName; //} StringBuilder sb = new StringBuilder(); sb.AppendFormat("name={0}&pwd={1}", name, pwd); string result = GetPage(strURL, sb.ToString()); switch (result) { case "1": MessageBox.Show("登录成功!"); break; case "2": MessageBox.Show("登录失败!"); break; default: MessageBox.Show("登录参数错误!"); break; } } /// <summary> /// Post数据到web服务端 /// </summary> /// <param name="strURL">网址</param> /// <param name="postData">参数</param> /// <returns></returns> public string GetPage(string strURL, string postData) { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8"); byte[] data = encoding.GetBytes(postData); // 准备请求... try { // 设置参数 request = WebRequest.Create(strURL) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; //Post请求方式 request.Method = "POST"; //是否保持常连接 request.KeepAlive = false; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)"; //内容类型 request.ContentType = "application/x-www-form-urlencoded"; //表示请求消息正文的长度 request.ContentLength = data.Length; outstream = request.GetRequestStream(); //send the data//将传输的数据,请求正文写入请求流 outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream();//获得请求流 sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); Console.WriteLine(content); string err = string.Empty; return content; } catch (Exception ex) { string err = ex.Message; return string.Empty; } } /// <summary> /// 关闭 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnClose_Click(object sender, EventArgs e) { this.Close(); } } }
服务端login
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>系统登录</title> <script type="text/javascript" src="Scripts/jquery-1.11.0.js"></script> <script type="text/javascript"> $(function () { $("#btnLogin").click(function () { var name = $("#txtUserName").val(); var pwd = $("#txtPwd").val(); $.ajax({ url: "Ashx/Login.ashx", data: "name=" + name + "&pwd=" + pwd, type: "Post", dataType: "text", success: function (msg) { if (msg == "1") { $("#divMsg").html("登录成功!"); } else { $("#divMsg").html("登录失败!"); } } }); }); }); </script> </head> <body> <div style="text-align:center;"> <table> <tr> <td>用户名:</td> <td><input type="text" id="txtUserName" name="name" value="admin" /></td> </tr> <tr> <td>密码:</td> <td><input type="password" id="txtPwd" name="name" value="admin" /></td> </tr> <tr> <td colspan="2"><input type="button" id="btnLogin" name="name" value="登录" /></td> </tr> </table> <div id="divMsg"></div> </div> </body> </html>
一般处理程序;Login.ashx
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace LoginWebApp.Ashx { /// <summary> /// Login 的摘要说明 /// </summary> public class Login : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //接收客户端传送过来的用户名和密码 string name = context.Request["name"]; string pwd = context.Request["pwd"]; Userinfo info = new Userinfo(); if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(pwd)) { context.Response.Write("2"); //登录失败 } else { if (name == "admin" && pwd == "admin") { //登录成功记入cookie context.Response.Cookies["n"].Value = name; context.Response.Cookies["n"].Expires = DateTime.Now.AddDays(7); context.Response.Cookies["p"].Value = pwd; context.Response.Cookies["p"].Expires = DateTime.Now.AddDays(7); context.Response.Write("1"); //登陆成功 } else { context.Response.Write("2"); } } } public bool IsReusable { get { return false; } } } }
引用jquery jquery-1.11.0.js以及样式 后端登录可以扩展实现数据库的访问方式获取用户名和密码判断登录
这里主要展现实现模拟登陆的方式 看了其他文章 跟以前做的有点相似所以在此做个改动 其实原理一样。