選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

325 行
13 KiB

  1. using Learun.Application.AppMagager;
  2. using Learun.Util;
  3. using Nancy;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Learun.Application.Base.SystemModule;
  7. using Learun.Application.TwoDevelopment.LR_Desktop;
  8. using System.Text;
  9. using System.Data;
  10. using Learun.Application.OA;
  11. using System.Configuration;
  12. using Learun.Application.TwoDevelopment.Permission;
  13. using System;
  14. using Learun.Cache.Base;
  15. using Learun.Cache.Factory;
  16. namespace Learun.Application.WebApi.Modules
  17. {
  18. public class SSOApi : BaseApi
  19. {
  20. private Perm_FunctionIBLL perm_FunctionIBLL = new Perm_FunctionBLL();
  21. private Perm_FunctionTypeIBLL perm_FunctionTypeIBLL = new Perm_FunctionTypeBLL();
  22. Perm_UserPermissionIBLL permUserPermissionIbll = new Perm_UserPermissionBLL();
  23. private Perm_FunctionVisitIBLL functionVisitIbll = new Perm_FunctionVisitBLL();
  24. private ICache cache = CacheFactory.CaChe();
  25. public SSOApi()
  26. : base("/quanjiang/sso")
  27. {
  28. Get["/list"] = GetList;
  29. Get["/list20"] = GetList20;
  30. Get["/goto"] = GoTo;
  31. Get["/goto20"] = GoToApplication;
  32. Get["first"] = First;
  33. Post["first"] = FirstPost;
  34. Get["authorize"] = Authorize;
  35. }
  36. /// <summary>
  37. /// 统一身份认证2.0
  38. /// </summary>
  39. /// <param name="_"></param>
  40. /// <returns></returns>
  41. public Response Authorize(dynamic _)
  42. {
  43. string appid = Request.Query["appid"];
  44. string secret = Request.Query["secret"];
  45. string appkey = Request.Query["appkey"];
  46. if (string.IsNullOrEmpty(appid))
  47. {
  48. return Fail("参数:appid不能为空");
  49. }
  50. if (string.IsNullOrEmpty(secret))
  51. {
  52. return Fail("参数:secret不能为空");
  53. }
  54. if (string.IsNullOrEmpty(appkey))
  55. {
  56. return Fail("参数:appkey不能为空");
  57. }
  58. var application = perm_FunctionIBLL.GetPerm_FunctionEntity(appid);
  59. if (application != null)
  60. {
  61. if (Util.DESEncrypt.Decrypt(application.FSecret,
  62. ConfigurationManager.AppSettings["SSOPublicSecret"]).Equals(secret))
  63. {
  64. try
  65. {
  66. var code = DESEncrypt.Decrypt(appkey, "bjqjsso");
  67. if (!string.IsNullOrEmpty(cache.Read<string>(code)))
  68. {
  69. return Success(new { useraccount = cache.Read<string>(code) });
  70. }
  71. else
  72. {
  73. return Fail("appkey已过期");
  74. }
  75. }
  76. catch (Exception e)
  77. {
  78. return Fail("appkey错误");
  79. }
  80. }
  81. else
  82. {
  83. return Fail("secret错误");
  84. }
  85. }
  86. else
  87. return Fail("未授权的appid");
  88. }
  89. private Response FirstPost(dynamic _)
  90. {
  91. string publickey = ConfigurationManager.AppSettings["SSOPublicSecret"];
  92. var ssoparam = this.GetReqData<SSOParam>();
  93. if (!string.IsNullOrEmpty(ssoparam.UPId))
  94. {
  95. Perm_FunctionEntity up = new Perm_FunctionEntity();
  96. up.UPUserName = ssoparam.UPUserName;
  97. up.UPPass = ssoparam.UPPass;
  98. up.FId = ssoparam.FId;
  99. up.UserId = ssoparam.UserId;
  100. perm_FunctionIBLL.SaveEntityByUPId(ssoparam.UPId, up);
  101. return Success(new { FInterfaceUrl = "/SSO/GoTo?sysid=" + DESEncrypt.Encrypt(up.FId, publickey) + "&openid=" + DESEncrypt.Encrypt(up.UserId, publickey) });
  102. }
  103. else
  104. {
  105. return Fail("参数错误");
  106. }
  107. }
  108. private Response First(dynamic _)
  109. {
  110. string publickey = ConfigurationManager.AppSettings["SSOPublicSecret"];
  111. var ssoparam = this.GetReqData<SSOParam>();
  112. string sysid = ssoparam.sysid;
  113. string strsysid = DESEncrypt.Decrypt(sysid, publickey);
  114. string openid = ssoparam.openid;
  115. string userid = DESEncrypt.Decrypt(openid, publickey);
  116. var uplist = permUserPermissionIbll.GetPerm_UserPermissionEntityByFIdAndUid(strsysid, userid);
  117. if (uplist == null)
  118. {
  119. return Fail("用户未授权。");
  120. }
  121. var perfun = perm_FunctionIBLL.GetPerm_FunctionEntityByUPId(uplist.UPId);
  122. return Success(perfun);
  123. }
  124. public class SSOParam
  125. {
  126. public string sysid { get; set; }
  127. public string openid { get; set; }
  128. public string UPId { get; set; }
  129. public string FId { get; set; }
  130. public string UserId { get; set; }
  131. public string UPUserName { get; set; }
  132. public string UPPass { get; set; }
  133. public string appid { get; set; }
  134. }
  135. public Response GoToApplication(dynamic _)
  136. {
  137. var userinfo = userInfo;
  138. var ssoparam = this.GetReqData<SSOParam>();
  139. if (userinfo != null)
  140. {
  141. var perm_application = perm_FunctionIBLL.GetPerm_FunctionEntity(ssoparam.appid);
  142. if (perm_application != null)
  143. {
  144. //写入当前请求所登录的用户
  145. var code = Util.CommonHelper.RndNum(9);
  146. cache.Write(code, userinfo.account, TimeSpan.FromMinutes(10));
  147. var url = perm_application.FInterfaceUrl;
  148. if (url.Contains("?"))
  149. {
  150. url += "&appkey=" + DESEncrypt.Encrypt(code, "bjqjsso");
  151. }
  152. else
  153. {
  154. url += "?appkey=" + DESEncrypt.Encrypt(code, "bjqjsso");
  155. }
  156. return Success(new{ FInterfaceUrl=url });
  157. }
  158. else
  159. return Fail("appid解析失败,请确认。");
  160. }
  161. else
  162. {
  163. return Fail("用户信息解析失败,请确认。");
  164. }
  165. }
  166. public Response GoTo(dynamic _)
  167. {
  168. try
  169. {
  170. var ssoparam = this.GetReqData<SSOParam>();
  171. string publickey = ConfigurationManager.AppSettings["SSOPublicSecret"];
  172. string sysid = ssoparam.sysid;
  173. string strsysid = DESEncrypt.Decrypt(sysid, publickey);
  174. string openid = ssoparam.openid;
  175. string userid = DESEncrypt.Decrypt(openid, publickey);
  176. var uplist = permUserPermissionIbll.GetPerm_UserPermissionEntityByFIdAndUid(strsysid, userid);
  177. Perm_FunctionVisitEntity functionVisitEntity = new Perm_FunctionVisitEntity();
  178. functionVisitEntity.Create();
  179. functionVisitEntity.Fid = strsysid;
  180. functionVisitEntity.PDate = DateTime.Now;
  181. functionVisitEntity.PUId = userid;
  182. var userinfo = userInfo;
  183. functionVisitEntity.PUName = userinfo.realName;
  184. if (uplist == null)
  185. {
  186. functionVisitEntity.PIsLoginSuccess = false;
  187. functionVisitEntity.PContent = "用户未授权";
  188. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  189. return Fail("用户未授权。");
  190. }
  191. var perfun = perm_FunctionIBLL.GetPerm_FunctionEntityByUPId(uplist.UPId);
  192. string secretkey = DESEncrypt.Decrypt(perfun.FSecret, publickey);
  193. if (perfun.FIsManagePage == true)
  194. {
  195. if (!string.IsNullOrEmpty(perfun.FInterfaceUrl))
  196. {
  197. if (!string.IsNullOrEmpty(perfun.UPUserName) && !string.IsNullOrEmpty(perfun.UPPass))
  198. {
  199. functionVisitEntity.PIsLoginSuccess = true;
  200. functionVisitEntity.PContent = "成功转到统一认证网站:" + perfun.FUrl;
  201. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  202. return Success(new
  203. {
  204. FInterfaceUrl = perfun.FInterfaceUrl + "?u=" +
  205. DESEncrypt.Encrypt(DESEncrypt.Encrypt(perfun.UPUserName, secretkey),
  206. publickey) + "&p=" +
  207. DESEncrypt.Encrypt(DESEncrypt.Encrypt(perfun.UPPass, secretkey),
  208. publickey) + "&t=" +
  209. DESEncrypt.Encrypt(
  210. DESEncrypt.Encrypt(DateTime.Now.ToString("yyyyMMddHHmmss"), secretkey),
  211. publickey) + "&ip=" +
  212. DESEncrypt.Encrypt(DESEncrypt.Encrypt(GetIP(), secretkey), publickey)
  213. });
  214. }
  215. else
  216. {
  217. functionVisitEntity.PIsLoginSuccess = false;
  218. functionVisitEntity.PContent = "用户未配置转到用户名密码配置页面";
  219. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  220. //用户未配置转到用户名密码配置页面
  221. return Success(new { FInterfaceUrl = "/SSO/FirstLogin?sysid=" + sysid + "&openid=" + openid });
  222. }
  223. }
  224. else
  225. {
  226. functionVisitEntity.PIsLoginSuccess = false;
  227. functionVisitEntity.PContent = "未配置登录接口地址";
  228. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  229. return Fail("未配置登录接口地址。");
  230. }
  231. }
  232. else
  233. {
  234. if (!string.IsNullOrEmpty(perfun.FUrl))
  235. {
  236. functionVisitEntity.PIsLoginSuccess = true;
  237. functionVisitEntity.PContent = "成功转到统一认证网站:" + perfun.FUrl;
  238. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  239. return Success(new { FInterfaceUrl = perfun.FUrl });
  240. }
  241. else
  242. {
  243. functionVisitEntity.PIsLoginSuccess = false;
  244. functionVisitEntity.PContent = "未配置地址";
  245. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  246. return Fail("未配置地址。");
  247. }
  248. }
  249. }
  250. catch (Exception e)
  251. {
  252. return Fail("参数错误。");
  253. }
  254. }
  255. public Response GetList20(dynamic _)
  256. {
  257. var userinfo = userInfo;
  258. var functionlist = perm_FunctionIBLL.GetListByUserId(userinfo.userId).Where(m=>m.FIsH5==true).Select(m=>
  259. new
  260. {
  261. m.FName,
  262. m.FId,
  263. });
  264. return Success(functionlist);
  265. }
  266. /// <summary>
  267. /// 获取页面显示列表数据
  268. /// <summary>
  269. /// <param name="_"></param>
  270. /// <returns></returns>
  271. public Response GetList(dynamic _)
  272. {
  273. string publickey = ConfigurationManager.AppSettings["SSOPublicSecret"];
  274. var logininfo = userInfo;
  275. var datatype = perm_FunctionTypeIBLL.GetListByUserId(userInfo.userId);
  276. foreach (var typeEntity in datatype)
  277. {
  278. var datafunction = perm_FunctionIBLL.GetListByFTId(typeEntity.FTId, userInfo.userId).Where(m=>m.FIsH5==true);
  279. foreach (var item in datafunction)
  280. {
  281. if (item.FIsManagePage == true)
  282. {
  283. item.FInterfaceUrl = "/SSO/GoTo?sysid=" + DESEncrypt.Encrypt(item.FId, publickey) + "&openid=" + DESEncrypt.Encrypt(logininfo.userId, publickey);
  284. }
  285. else
  286. {
  287. item.FInterfaceUrl = item.FUrl;
  288. }
  289. item.FUrl = null;
  290. }
  291. typeEntity.PermFunction = datafunction.ToList();
  292. }
  293. return Success(datatype);
  294. }
  295. /// <summary>
  296. /// 获取IP
  297. /// </summary>
  298. /// <returns></returns>
  299. private string GetIP()
  300. {
  301. string ip = string.Empty;
  302. if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
  303. ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
  304. if (string.IsNullOrEmpty(ip))
  305. ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
  306. return ip;
  307. }
  308. }
  309. }