登录页面,布局可以直接拿官方给的登录页面稍作修改就行了。
目前后端我用的是api接口。
点击登录按钮:
private void btnLogon_Click(object sender, EventArgs e) { string user = txtUserName.Text.Trim(); ; string pwd = txtPassWord.Text.Trim(); if (user.Length <= 0) { MessageBox.Show("请输入账号!"); } else if (pwd.Length <= 0) { MessageBox.Show("请输入密码!"); } else if (pwd.Length < 4 || pwd.Length > 12) { MessageBox.Show("密码必须为6-12位!"); } else { LoadClientData(MobileServer.ServerID + "user", user); if (checkRemb.Checked == true) { //记住密码 LoadClientData(MobileServer.ServerID + "pwd", pwd); } else { //删除客户端数据 RemoveClientData(MobileServer.ServerID + "pwd", (object s, ClientDataResultHandlerArgs args) => txtPassWord.Text = ""); } pwd = Md5.Md5Upper32(pwd); Client.Session["url"] = "http接口"; string url = Client.Session["url"].ToString() +"/wapi/OP/UserLogin1?username=" + user + "&password=" + pwd; string Data = RequestHttpHelper.LoadData(url);//远程调用app接口 JsonCommon<UserInfo> js = JsonHelper.DeserializeJson<JsonCommon<UserInfo>>(Data); if (js.success == 1) { Client.Session["UserInfo"] = js.data[0]; SmobilerMain main = new SmobilerMain(); Show(main); } else { MessageBox.Show("用户名或者密码错误!"); } } }
记住密码:
private void smobilerForm1_Load(object sender, EventArgs e) { //读取用户名 ReadClientData(MobileServer.ServerID + "user", (object sender1, ClientDataResultHandlerArgs e1) => { if (String.IsNullOrEmpty(e1.error)) { txtUserName.Text = e1.Value; } }); //读取密码 ReadClientData(MobileServer.ServerID + "pwd", (object sender1, ClientDataResultHandlerArgs e1) => { if (String.IsNullOrEmpty(e1.error)) { txtPassWord.Text = e1.Value; if (txtPassWord.Text.Length > 0) { checkRemb.Checked = true; } } }); }
MD5加密:
public class Md5 { /// <summary> /// MD5加密,保留32位大写 /// </summary> /// <param name="str">加密字符</param> /// <returns></returns> [Obsolete] public static string Md5Upper32(string str) { return md5(str, 32).ToUpper(); } /// <summary> /// MD5加密 /// </summary> /// <param name="str">加密字符</param> /// <param name="code">加密位数16/32</param> /// <returns></returns> [Obsolete] public static string md5(string str, int code) { string strEncrypt = string.Empty; if (code == 16) { strEncrypt = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").Substring(8, 16); } else if (code == 32) { strEncrypt = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5"); } return strEncrypt; } }
远程调用API接口的方法:
public class RequestHttpHelper { public static string LoadData(string url) { //创建请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //GET请求 request.Method = "GET"; request.ReadWriteTimeout = 5000; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); //返回内容 string retString = myStreamReader.ReadToEnd(); return retString; } /// <summary> /// 删除文件 /// </summary> /// <returns></returns> public static void DelFile(string strFileName) { if (File.Exists(strFileName)) { File.Delete(strFileName); } } public static void SendImg(string path, string fileName, string apiUrl) { //读取文件 using (var stream = File.OpenRead(path)) { Upload(stream, fileName, apiUrl);//上传文件到API接口 } } /// <summary> /// 图片转二进制 /// </summary> /// <param name="imageIn"></param> /// <returns></returns> public byte[] imageToByteArray(System.Drawing.Image imageIn) { MemoryStream ms = new MemoryStream(); imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); return ms.ToArray(); } /// <summary> /// 二进制转图片 /// </summary> /// <param name="byteArrayIn"></param> /// <returns></returns> public Image byteArrayToImage(byte[] byteArrayIn) { MemoryStream ms = new MemoryStream(byteArrayIn); Image returnImage = Image.FromStream(ms); return returnImage; } /// <summary> /// 上传文件到API接口 /// </summary> /// <param name="stream">文件流</param> /// <param name="fileName">文件名称</param> /// <param name="apiUrl">API接口</param> private static void Upload(FileStream stream, string fileName, string apiUrl) { string url = apiUrl; using (var client = new HttpClient()) { string para = "";//参数 byte[] pReadByte = new byte[0]; BinaryReader r = new BinaryReader(stream); r.BaseStream.Seek(0, SeekOrigin.Begin); //将文件指针设置到文件开 pReadByte = r.ReadBytes((int)r.BaseStream.Length);//文件流 para = string.Format("stream={0}&fileName={1}", Convert.ToBase64String(pReadByte).Replace("+", " "), fileName); byte[] bytePara = Encoding.UTF8.GetBytes(para); using (StreamContent sc = new StreamContent(new MemoryStream(bytePara))) { sc.Headers.ContentLength = bytePara.Length; sc.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");//application/x-www-form-urlencoded var respMsg = client.PostAsync(url, sc); HttpResponseMessage responseMessage = respMsg.Result; } } } /// <summary> /// 上传文件到API接口 /// </summary> /// <param name="stream">文件流</param> /// <param name="fileName">文件名称</param> /// <param name="apiUrl">API接口</param> public static void Upload1(byte[] bytePara1,string fileName, string apiUrl) { string url = apiUrl; using (var client = new HttpClient()) { string para = "";//参数 para = string.Format("stream={0}&fileName={1}", Convert.ToBase64String(bytePara1).Replace("+", " "), fileName); byte[] bytePara = Encoding.UTF8.GetBytes(para); using (StreamContent sc = new StreamContent(new MemoryStream(bytePara))) { sc.Headers.ContentLength = bytePara.Length; sc.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");//application/x-www-form-urlencoded var respMsg = client.PostAsync(url, sc); HttpResponseMessage responseMessage = respMsg.Result; } } } }
JsonCommon类:
public class JsonCommon<T> { public int success { get; set; } public List<T> data { get; set; } }
UserInfo类:
public class UserInfo { public int uid { get; set; } public string realname { get; set; } public int departid { get; set; } public string departname { get; set; } public string phone { get; set; } public string email { get; set; } }
运行效果: