using System; using System.Collections.Generic; using System.Linq; using System.Text; using EMIS.Utility; using System.Net; using System.Web; using System.IO; using Bowin.Common.JSON; using System.Security.Cryptography.X509Certificates; using System.Net.Security; namespace EMIS.ExtensionLogic.ServiceLogic.SystemManage { public class UserServices : EMIS.CommonLogic.SystemServices.UserServices { private string ssoUrl = Const.SSO_HOST; private string appID = System.Configuration.ConfigurationManager.AppSettings["SSOAPPID"]; private string appSecret = System.Configuration.ConfigurationManager.AppSettings["SSOSecret"]; public UserServices() { ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; } public override string SSOLogin() { var accessToken = this.CheckToken(); return this.GetUser(accessToken); } private SSOCheckTokenData CheckToken() { var token = HttpContext.Current.Request["token"]; var url = ssoUrl + "/ssoapi/v2/checkToken"; var jsonData = ""; SSOCheckTokenData checkTokenData; var parameters = "token=" + token + "&appid=" + appID + "&appsecret=" + appSecret + "&userip=" + ApplicationClientHelper.GetIP(); var paraDatas = Encoding.UTF8.GetBytes(parameters); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = paraDatas.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(paraDatas, 0, paraDatas.Length); requestStream.Close(); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { jsonData = reader.ReadToEnd().ToString(); } checkTokenData = jsonData.JsonToObject(); } if (checkTokenData.error != 0) { throw new Exception("登录失败,认证平台无法识别登录票据:" + checkTokenData.message); } else { return checkTokenData; } } private string GetUser(SSOCheckTokenData accessToken) { var url = ssoUrl + "/oauth/getUserInfo"; var jsonData = ""; SSOAuthData authData; var parameters = "access_token=" + accessToken.access_token + "&openid=" + accessToken.openid; var paraDatas = Encoding.UTF8.GetBytes(parameters); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = paraDatas.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(paraDatas, 0, paraDatas.Length); requestStream.Close(); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { jsonData = reader.ReadToEnd().ToString(); } authData = jsonData.JsonToObject(); } if (authData.error != 0) { throw new Exception("登录失败,认证平台无法识别登录票据:" + authData.message); } else { return authData.username; } } } internal class SSOCheckTokenData { public int error { get; set; } public string message { get; set; } public string openid { get; set; } public string access_token { get; set; } } internal class SSOAuthData { public int error { get; set; } public string message { get; set; } public string username { get; set; } public string name { get; set; } public string group { get; set; } public string openid { get; set; } public string wx_openid { get; set; } } }