UserServices.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMIS.Utility;
  6. using System.Net;
  7. using System.Web;
  8. using System.IO;
  9. using Bowin.Common.JSON;
  10. using System.Security.Cryptography.X509Certificates;
  11. using System.Net.Security;
  12. namespace EMIS.ExtensionLogic.ServiceLogic.SystemManage
  13. {
  14. public class UserServices : EMIS.CommonLogic.SystemServices.UserServices
  15. {
  16. private string ssoUrl = Const.SSO_HOST;
  17. private string appID = System.Configuration.ConfigurationManager.AppSettings["SSOAPPID"];
  18. private string appSecret = System.Configuration.ConfigurationManager.AppSettings["SSOSecret"];
  19. public UserServices()
  20. {
  21. ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  22. {
  23. return true;
  24. };
  25. }
  26. public override string SSOLogin()
  27. {
  28. var accessToken = this.CheckToken();
  29. return this.GetUser(accessToken);
  30. }
  31. private SSOCheckTokenData CheckToken()
  32. {
  33. var token = HttpContext.Current.Request["token"];
  34. var url = ssoUrl + "/ssoapi/v2/checkToken";
  35. var jsonData = "";
  36. SSOCheckTokenData checkTokenData;
  37. var parameters = "token=" + token + "&appid=" + appID + "&appsecret=" + appSecret + "&userip=" + ApplicationClientHelper.GetIP();
  38. var paraDatas = Encoding.UTF8.GetBytes(parameters);
  39. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  40. request.Method = "POST";
  41. request.ContentType = "application/x-www-form-urlencoded";
  42. request.ContentLength = paraDatas.Length;
  43. using (Stream requestStream = request.GetRequestStream())
  44. {
  45. requestStream.Write(paraDatas, 0, paraDatas.Length);
  46. requestStream.Close();
  47. }
  48. using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  49. {
  50. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  51. {
  52. jsonData = reader.ReadToEnd().ToString();
  53. }
  54. checkTokenData = jsonData.JsonToObject<SSOCheckTokenData>();
  55. }
  56. if (checkTokenData.error != 0)
  57. {
  58. throw new Exception("登录失败,认证平台无法识别登录票据:" + checkTokenData.message);
  59. }
  60. else
  61. {
  62. return checkTokenData;
  63. }
  64. }
  65. private string GetUser(SSOCheckTokenData accessToken)
  66. {
  67. var url = ssoUrl + "/oauth/getUserInfo";
  68. var jsonData = "";
  69. SSOAuthData authData;
  70. var parameters = "access_token=" + accessToken.access_token + "&openid=" + accessToken.openid;
  71. var paraDatas = Encoding.UTF8.GetBytes(parameters);
  72. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  73. request.Method = "POST";
  74. request.ContentType = "application/x-www-form-urlencoded";
  75. request.ContentLength = paraDatas.Length;
  76. using (Stream requestStream = request.GetRequestStream())
  77. {
  78. requestStream.Write(paraDatas, 0, paraDatas.Length);
  79. requestStream.Close();
  80. }
  81. using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  82. {
  83. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  84. {
  85. jsonData = reader.ReadToEnd().ToString();
  86. }
  87. authData = jsonData.JsonToObject<SSOAuthData>();
  88. }
  89. if (authData.error != 0)
  90. {
  91. throw new Exception("登录失败,认证平台无法识别登录票据:" + authData.message);
  92. }
  93. else
  94. {
  95. return authData.username;
  96. }
  97. }
  98. }
  99. internal class SSOCheckTokenData
  100. {
  101. public int error { get; set; }
  102. public string message { get; set; }
  103. public string openid { get; set; }
  104. public string access_token { get; set; }
  105. }
  106. internal class SSOAuthData
  107. {
  108. public int error { get; set; }
  109. public string message { get; set; }
  110. public string username { get; set; }
  111. public string name { get; set; }
  112. public string group { get; set; }
  113. public string openid { get; set; }
  114. public string wx_openid { get; set; }
  115. }
  116. }