C#植入友商网在线服务并实现同步用户注册接口实现
C#植入友商网在线服务并实现同步用户注册接口实现
C#植入友商网在线服务并实现同步用户注册接口实现 请先下载《友商网同步用户和单点登录接口v3.0说明书》,阅读后再理解下面的代码. 1.建立TUser类 /// <summary> /// 友商网提供的xml文件建立User类 /// </summary> public class TUser { private string _userAccount = ""; private string _Password = ""; private string _realName = ""; private string _gender = ""; private string _email = ""; private string _mobile = ""; private string _phone = ""; private string _address = ""; private string _bindTo = ""; private string _bindAsAdmin = ""; public string userAccount { get { return _userAccount; } set { _userAccount = value; } } public string password { get { return _Password; } set { _Password = value; } } public string realName { get { return _realName; } set { _realName = value; } } public string gender { get { return _gender; } set { _gender = value; } } public string email { get { return _email; } set { _email = value; } } public string mobile { get { return _mobile; } set { _mobile = value; } } public string phone { get { return _phone; } set { _phone = value; } } public string address { get { return _address; } set { _address = value; } } public string bindTo { get { return _bindTo; } set { _bindTo = value; } } public string bindAsAdmin { get { return _bindAsAdmin; } set { _bindAsAdmin = value; } } } //如转载请注明出处www.csframework.com } 2.注册用户的类(YoushangUserRegister) /// <summary> /// 调用友商网注册用户接口 /// </summary> public class YoushangUserRegister { private string _partnerID; private string _aesKey; public YoushangUserRegister(string partnerID, string aesKey) { _partnerID = partnerID; _aesKey = aesKey; } /// <summary> /// 注册用户 /// </summary> public bool Register(TUser user) { string url = "http://agent.youshang.com/federation/commonservice/servicePortal.do"; //友商网接口 string urlParam = "partnerId={0}&cipherCode={1}&iv={2}"; //接口的URL参数 string iv = AES_Crypto.GetIv(16); //生成iv代码 string xmlUser = this.getUserXML(user); //TUser对象转换为xml文件 //AES加密用户XML数据 string cipherCode = AES_Crypto.AESEncrypt(xmlUser, _aesKey, iv); cipherCode = HttpUtility.UrlEncode(cipherCode);//HTML ENCODE iv = HttpUtility.UrlEncode(iv);//HTML ENCODE urlParam = string.Format(urlParam, _partnerID, cipherCode, iv); return this.HttpPost(url, urlParam); //提交数据.... } /// <summary> /// 调用友商网提供的注册用户接口,发送XML数据及接收。 /// </summary> /// <param name="url">接口URL地址</param> /// <param name="param">URL参数,xml已加密</param> /// <returns></returns> private bool HttpPost(string url, string param) { WebRequest req = WebRequest.Create(url); req.ContentType = "application/x-www-form-urlencoded"; req.Method = "POST"; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(param); req.ContentLength = bytes.Length; //发送的请求(Request)数据大小 System.IO.Stream os = req.GetRequestStream(); os.Write(bytes, 0, bytes.Length); //发送... os.Close(); System.Net.WebResponse resp = req.GetResponse(); //等待友商网的服务器回应. if (resp == null) return false; System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()); string retXML = sr.ReadToEnd().Trim(); return IsSuccess(retXML); } /// <summary> /// 检查友商网返回的XML数据. /// </summary> /// <param name="retXML"></param> /// <returns></returns> private bool IsSuccess(string retXML) { XmlDocument xml = new XmlDocument(); xml.LoadXml(retXML); XmlNode node = xml.SelectSingleNode("response/status"); //返回结果: "0":表示成功; "1":表示失败 return node.InnerText.Trim() == "0"; } /// <summary> /// 友商网注册用户的XML文件规范 /// </summary> /// <param name="user">将user转换为xml文件</param> /// <returns></returns> private string getUserXML(TUser user) { string xml = "<?xml version=\"1.0\" encoding = \"utf-8\"?>" + "<request>" + "<type>userSync</type>" + "<data>" + "<attr name=\"userAccount\">{0}</attr> " + "<attr name=\"realName\">{1}</attr>" + "<attr name=\"gender\">{2}</attr>" + "<attr name=\"email\">{3}</attr>" + "<attr name=\"mobile\">{4}</attr>" + "<attr name=\"phone\">{5}</attr>" + "<attr name=\"address\">{6}</attr>" + "<attr name=\"bindTo\">{7}</attr>" + "<attr name=\"bindAsAdmin\">{8}</attr>" + "</data>" + "</request>"; xml = string.Format(xml, user.userAccount, user.realName, user.gender, user.email, user.mobile, user.phone, user.address, user.bindTo, user.bindAsAdmin); return xml; } 3. 测试方法 友商网提供测试平台接口: http://agent.youshang.com/federation/commonservice/servicePortal.do?partnerId=value&cipherCode=value&iv=value 请先修改本地的DNS,改为:219.133.170.202,DNS自动转发到测试接口,否则调用实际环境测试会出错。建立一个WebSite工程,在default.aspx页面加一些输入框,如下图: 提交按钮代码: protected void Button1_Click(object sender, EventArgs e) { TUser user = new TUser(); user.address = txtAddress.Text; user.email = txtEmail.Text; user.gender = txtGender.Text; user.mobile = txtMobile.Text; user.password = txtPassword.Text; user.phone = txtPhone.Text; user.realName = txtRealName.Text; user.userAccount = txtUserAccount.Text; //注册用户对象 YoushangUserRegister register = new YoushangUserRegister( SecurityClass.Instance.GetPartnerID(), //友商网伙伴编号 SecurityClass.Instance.GetAESKey()); //AES加密密码,与友商网开发组约定AES密码 bool ret = register.Register(user); if (ret) { this.Response.Write("插入友商后台数据库成功!"); } else { this.Response.Write("插入友商后台数据失败!"); } } 注意: 必须有友商网伙伴编号及约定AES密码.否则友商网后台无法解释加密的XML文件!!! 《友商网同步用户和单点登录接口v3.0说明书》下载
参考文档:
[原创]C#植入友商网在线服务并实现同步用户注册接口(一) [原创]C#植入友商网在线服务并实现同步用户注册接口(三) C#仿QQ聊天程序v1.0(IConnectionList接口实现) C#实现.Net Remoting服务端与客户端通信 详述IComparer,IComparable接口,实现自定义方法比较对象大小并排序(C#) C# 同步工作站与SQL服务器的时间,设置本机时间 平台建立WCF服务操作指引、三层源码+桥接接口实现 C# 从注册表获取Windows服务的文件路径 C#源码-Windows服务管理器,实现安装,卸载,启动和停止服务等功能 C# Winform 软件用户授权注册系统正式发布 基于Web前端用户调用CSFramework.WebApi服务端登录登出接口实现 C#实现接口的两种方式:显示实现和隐式实现接口 C#接口中多态的实现 Web端使用VUE调用WebApi接口实现用户登录及采用Token方式数据交互 C# Winform 软件用户授权注册系统V2.1 测试程序(Tester.exe)
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |