You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

432 rivejä
18 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.Factory;
  15. using Learun.Cache.Base;
  16. using Learun.Util.Operat;
  17. namespace Learun.Application.WebApi.Modules
  18. {
  19. public class SSOApi : BaseApi
  20. {
  21. private Perm_FunctionIBLL perm_FunctionIBLL = new Perm_FunctionBLL();
  22. private Perm_FunctionTypeIBLL perm_FunctionTypeIBLL = new Perm_FunctionTypeBLL();
  23. Perm_UserPermissionIBLL permUserPermissionIbll = new Perm_UserPermissionBLL();
  24. private Perm_FunctionVisitIBLL functionVisitIbll = new Perm_FunctionVisitBLL();
  25. private ICache cache = CacheFactory.CaChe();
  26. public SSOApi()
  27. : base("/quanjiang/sso")
  28. {
  29. Get["/list"] = GetList;
  30. Get["/list20"] = GetList20;
  31. Get["/goto"] = GoTo;
  32. Get["/goto20"] = GoToApplication;
  33. Get["first"] = First;
  34. Post["first"] = FirstPost;
  35. Get["authorize"] = Authorize;
  36. Get["/out"] = outApplication;
  37. Get["/userstat"] = userstat;
  38. }
  39. #region 统一身份认证3.0
  40. /// <summary>
  41. /// 退出
  42. /// </summary>
  43. /// <param name="_"></param>
  44. /// <returns></returns>
  45. public Response outApplication(dynamic _)
  46. {
  47. string appid = Request.Query["appid"];
  48. string appkey = Request.Query["appkey"];
  49. string m = Request.Query["m"];
  50. string t = Request.Query["t"];
  51. if (string.IsNullOrEmpty(appid))
  52. {
  53. return Fail("参数:appid不能为空");
  54. }
  55. if (string.IsNullOrEmpty(appkey))
  56. {
  57. return Fail("参数:appkey不能为空");
  58. }
  59. if (string.IsNullOrEmpty(m))
  60. {
  61. return Fail("参数:m不能为空");
  62. }
  63. if (string.IsNullOrEmpty(t))
  64. {
  65. return Fail("参数:t不能为空");
  66. }
  67. var application = perm_FunctionIBLL.GetPerm_FunctionEntity(appid);
  68. if (application != null)
  69. {
  70. if (Md5Helper.Encrypt(application.FSecret, 32) == appkey)
  71. {
  72. OperatorHelper.Instance.EmptyCurrent(DESEncrypt.Decrypt(t), DESEncrypt.Decrypt(m));
  73. return SuccessString("true");
  74. }
  75. else
  76. {
  77. return Fail("appkey错误");
  78. }
  79. }
  80. else
  81. return Fail("未授权的appid");
  82. }
  83. /// <summary>
  84. /// 获取用户登录状态
  85. /// </summary>
  86. /// <param name="_"></param>
  87. /// <returns></returns>
  88. public Response userstat(dynamic _)
  89. {
  90. string appid = Request.Query["appid"];
  91. string appkey = Request.Query["appkey"];
  92. string m = Request.Query["m"];
  93. string t = Request.Query["t"];
  94. if (string.IsNullOrEmpty(appid))
  95. {
  96. return Fail("参数:appid不能为空");
  97. }
  98. if (string.IsNullOrEmpty(appkey))
  99. {
  100. return Fail("参数:appkey不能为空");
  101. }
  102. if (string.IsNullOrEmpty(m))
  103. {
  104. return Fail("参数:m不能为空");
  105. }
  106. if (string.IsNullOrEmpty(t))
  107. {
  108. return Fail("参数:t不能为空");
  109. }
  110. var application = perm_FunctionIBLL.GetPerm_FunctionEntity(appid);
  111. if (application != null)
  112. {
  113. if (Md5Helper.Encrypt(application.FSecret, 32) == appkey)
  114. {
  115. OperatorResult res = OperatorHelper.Instance.IsOnLine(DESEncrypt.Decrypt(t), DESEncrypt.Decrypt(m));
  116. if (res.stateCode == 1)
  117. {
  118. return SuccessString("true");
  119. }
  120. else
  121. {
  122. return Fail("登录无效");
  123. }
  124. }
  125. else
  126. {
  127. return Fail("appkey错误");
  128. }
  129. }
  130. else
  131. return Fail("未授权的appid");
  132. }
  133. #endregion
  134. /// <summary>
  135. /// 统一身份认证2.0
  136. /// </summary>
  137. /// <param name="_"></param>
  138. /// <returns></returns>
  139. public Response Authorize(dynamic _)
  140. {
  141. string appid = Request.Query["appid"];
  142. string secret = Request.Query["secret"];
  143. string appkey = Request.Query["appkey"];
  144. if (string.IsNullOrEmpty(appid))
  145. {
  146. return Fail("参数:appid不能为空");
  147. }
  148. if (string.IsNullOrEmpty(secret))
  149. {
  150. return Fail("参数:secret不能为空");
  151. }
  152. if (string.IsNullOrEmpty(appkey))
  153. {
  154. return Fail("参数:appkey不能为空");
  155. }
  156. var application = perm_FunctionIBLL.GetPerm_FunctionEntity(appid);
  157. if (application != null)
  158. {
  159. if (Util.DESEncrypt.Decrypt(application.FSecret,
  160. ConfigurationManager.AppSettings["SSOPublicSecret"]).Equals(secret))
  161. {
  162. try
  163. {
  164. var code = DESEncrypt.Decrypt(appkey, "bjqjsso");
  165. if (!string.IsNullOrEmpty(cache.Read<string>(code)))
  166. {
  167. return Success(new { useraccount = cache.Read<string>(code) });
  168. }
  169. else
  170. {
  171. return Fail("appkey已过期");
  172. }
  173. }
  174. catch (Exception e)
  175. {
  176. return Fail("appkey错误");
  177. }
  178. }
  179. else
  180. {
  181. return Fail("secret错误");
  182. }
  183. }
  184. else
  185. return Fail("未授权的appid");
  186. }
  187. private Response FirstPost(dynamic _)
  188. {
  189. string publickey = ConfigurationManager.AppSettings["SSOPublicSecret"];
  190. var ssoparam = this.GetReqData<SSOParam>();
  191. if (!string.IsNullOrEmpty(ssoparam.UPId))
  192. {
  193. Perm_FunctionEntity up = new Perm_FunctionEntity();
  194. up.UPUserName = ssoparam.UPUserName;
  195. up.UPPass = ssoparam.UPPass;
  196. up.FId = ssoparam.FId;
  197. up.UserId = ssoparam.UserId;
  198. perm_FunctionIBLL.SaveEntityByUPId(ssoparam.UPId, up);
  199. return Success(new { FInterfaceUrl = "/SSO/GoTo?sysid=" + DESEncrypt.Encrypt(up.FId, publickey) + "&openid=" + DESEncrypt.Encrypt(up.UserId, publickey) });
  200. }
  201. else
  202. {
  203. return Fail("参数错误");
  204. }
  205. }
  206. private Response First(dynamic _)
  207. {
  208. string publickey = ConfigurationManager.AppSettings["SSOPublicSecret"];
  209. var ssoparam = this.GetReqData<SSOParam>();
  210. string sysid = ssoparam.sysid;
  211. string strsysid = DESEncrypt.Decrypt(sysid, publickey);
  212. string openid = ssoparam.openid;
  213. string userid = DESEncrypt.Decrypt(openid, publickey);
  214. var uplist = permUserPermissionIbll.GetPerm_UserPermissionEntityByFIdAndUid(strsysid, userid);
  215. if (uplist == null)
  216. {
  217. return Fail("用户未授权。");
  218. }
  219. var perfun = perm_FunctionIBLL.GetPerm_FunctionEntityByUPId(uplist.UPId);
  220. return Success(perfun);
  221. }
  222. public class SSOParam
  223. {
  224. public string sysid { get; set; }
  225. public string openid { get; set; }
  226. public string UPId { get; set; }
  227. public string FId { get; set; }
  228. public string UserId { get; set; }
  229. public string UPUserName { get; set; }
  230. public string UPPass { get; set; }
  231. public string appid { get; set; }
  232. }
  233. public Response GoToApplication(dynamic _)
  234. {
  235. var userinfo = userInfo;
  236. var ssoparam = this.GetReqData<SSOParam>();
  237. if (userinfo != null)
  238. {
  239. var type = 0;//1学生 0教师
  240. if (userinfo.Description == "学生")
  241. {
  242. type = 1;
  243. }
  244. var perm_application = perm_FunctionIBLL.GetPerm_FunctionEntity(ssoparam.appid);
  245. if (perm_application != null)
  246. {
  247. //写入当前请求所登录的用户
  248. var code = Util.CommonHelper.RndNum(9);
  249. cache.Write(code, userinfo.account, TimeSpan.FromMinutes(10));
  250. var url = perm_application.FUrl;
  251. if (url.Contains("?"))
  252. {
  253. url += "&appkey=" + Md5Helper.Encrypt(Util.DESEncrypt.Decrypt(perm_application.FSecret, ConfigurationManager.AppSettings["SSOPublicSecret"]), 32) + "&name=" + DESEncrypt.Encrypt(userinfo.realName, Util.DESEncrypt.Decrypt(perm_application.FSecret, ConfigurationManager.AppSettings["SSOPublicSecret"]), false) + "&no=" + DESEncrypt.Encrypt(userinfo.IdentityCardNo, Util.DESEncrypt.Decrypt(perm_application.FSecret, ConfigurationManager.AppSettings["SSOPublicSecret"]), false) + "&acc=" + DESEncrypt.Encrypt(userinfo.enCode, Util.DESEncrypt.Decrypt(perm_application.FSecret, ConfigurationManager.AppSettings["SSOPublicSecret"]), false) + "&type=" + type + "&m=" + DESEncrypt.Encrypt(userinfo.loginMark) + "&t=" + DESEncrypt.Encrypt(userinfo.token);
  254. }
  255. else
  256. {
  257. url += "?appkey=" + Md5Helper.Encrypt(Util.DESEncrypt.Decrypt(perm_application.FSecret, ConfigurationManager.AppSettings["SSOPublicSecret"]), 32) + "&name=" + DESEncrypt.Encrypt(userinfo.realName, Util.DESEncrypt.Decrypt(perm_application.FSecret, ConfigurationManager.AppSettings["SSOPublicSecret"]), false) + "&no=" + DESEncrypt.Encrypt(userinfo.IdentityCardNo, Util.DESEncrypt.Decrypt(perm_application.FSecret, ConfigurationManager.AppSettings["SSOPublicSecret"]), false) + "&acc=" + DESEncrypt.Encrypt(userinfo.enCode, Util.DESEncrypt.Decrypt(perm_application.FSecret, ConfigurationManager.AppSettings["SSOPublicSecret"]), false) + "&type=" + type + "&m=" + DESEncrypt.Encrypt(userinfo.loginMark) + "&t=" + DESEncrypt.Encrypt(userinfo.token);
  258. }
  259. return Success(new { FInterfaceUrl = url });
  260. }
  261. else
  262. return Fail("appid解析失败,请确认。");
  263. }
  264. else
  265. {
  266. return Fail("用户信息解析失败,请确认。");
  267. }
  268. }
  269. public Response GoTo(dynamic _)
  270. {
  271. try
  272. {
  273. var ssoparam = this.GetReqData<SSOParam>();
  274. string publickey = ConfigurationManager.AppSettings["SSOPublicSecret"];
  275. string sysid = ssoparam.sysid;
  276. string strsysid = DESEncrypt.Decrypt(sysid, publickey);
  277. string openid = ssoparam.openid;
  278. string userid = DESEncrypt.Decrypt(openid, publickey);
  279. var uplist = permUserPermissionIbll.GetPerm_UserPermissionEntityByFIdAndUid(strsysid, userid);
  280. Perm_FunctionVisitEntity functionVisitEntity = new Perm_FunctionVisitEntity();
  281. functionVisitEntity.Create();
  282. functionVisitEntity.Fid = strsysid;
  283. functionVisitEntity.PDate = DateTime.Now;
  284. functionVisitEntity.PUId = userid;
  285. var userinfo = userInfo;
  286. functionVisitEntity.PUName = userinfo.realName;
  287. if (uplist == null)
  288. {
  289. functionVisitEntity.PIsLoginSuccess = false;
  290. functionVisitEntity.PContent = "用户未授权";
  291. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  292. return Fail("用户未授权。");
  293. }
  294. var perfun = perm_FunctionIBLL.GetPerm_FunctionEntityByUPId(uplist.UPId);
  295. string secretkey = DESEncrypt.Decrypt(perfun.FSecret, publickey);
  296. if (perfun.FIsManagePage == true)
  297. {
  298. if (!string.IsNullOrEmpty(perfun.FInterfaceUrl))
  299. {
  300. if (!string.IsNullOrEmpty(perfun.UPUserName) && !string.IsNullOrEmpty(perfun.UPPass))
  301. {
  302. functionVisitEntity.PIsLoginSuccess = true;
  303. functionVisitEntity.PContent = "成功转到统一认证网站:" + perfun.FUrl;
  304. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  305. var user = LoginUserInfo.Get();
  306. return Success(new
  307. {
  308. FInterfaceUrl = perfun.FInterfaceUrl + "?u=" +
  309. DESEncrypt.Encrypt(DESEncrypt.Encrypt(perfun.UPUserName, secretkey),
  310. publickey) + "&p=" +
  311. DESEncrypt.Encrypt(DESEncrypt.Encrypt(perfun.UPPass, secretkey),
  312. publickey) + "&t=" +
  313. DESEncrypt.Encrypt(
  314. DESEncrypt.Encrypt(DateTime.Now.ToString("yyyyMMddHHmmss"), secretkey),
  315. publickey) + "&ip=" +
  316. DESEncrypt.Encrypt(DESEncrypt.Encrypt(GetIP(), secretkey), publickey)
  317. + "&m=" + DESEncrypt.Encrypt(user.loginMark) + "&t=" + DESEncrypt.Encrypt(user.token)
  318. });
  319. }
  320. else
  321. {
  322. functionVisitEntity.PIsLoginSuccess = false;
  323. functionVisitEntity.PContent = "用户未配置转到用户名密码配置页面";
  324. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  325. //用户未配置转到用户名密码配置页面
  326. return Success(new { FInterfaceUrl = "/SSO/FirstLogin?sysid=" + sysid + "&openid=" + openid });
  327. }
  328. }
  329. else
  330. {
  331. functionVisitEntity.PIsLoginSuccess = false;
  332. functionVisitEntity.PContent = "未配置登录接口地址";
  333. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  334. return Fail("未配置登录接口地址。");
  335. }
  336. }
  337. else
  338. {
  339. if (!string.IsNullOrEmpty(perfun.FUrl))
  340. {
  341. functionVisitEntity.PIsLoginSuccess = true;
  342. functionVisitEntity.PContent = "成功转到统一认证网站:" + perfun.FUrl;
  343. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  344. return Success(new { FInterfaceUrl = perfun.FUrl });
  345. }
  346. else
  347. {
  348. functionVisitEntity.PIsLoginSuccess = false;
  349. functionVisitEntity.PContent = "未配置地址";
  350. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  351. return Fail("未配置地址。");
  352. }
  353. }
  354. }
  355. catch (Exception e)
  356. {
  357. return Fail("参数错误。");
  358. }
  359. }
  360. public Response GetList20(dynamic _)
  361. {
  362. var userinfo = userInfo;
  363. var functionlist = perm_FunctionIBLL.GetListByUserId(userinfo.userId).Where(m => m.FIsH5 == true).Select(m =>
  364. new
  365. {
  366. m.FName,
  367. m.FId,
  368. });
  369. return Success(functionlist);
  370. }
  371. /// <summary>
  372. /// 获取页面显示列表数据
  373. /// <summary>
  374. /// <param name="_"></param>
  375. /// <returns></returns>
  376. public Response GetList(dynamic _)
  377. {
  378. string publickey = ConfigurationManager.AppSettings["SSOPublicSecret"];
  379. var logininfo = userInfo;
  380. var datatype = perm_FunctionTypeIBLL.GetListByUserId(userInfo.userId);
  381. foreach (var typeEntity in datatype)
  382. {
  383. var datafunction = perm_FunctionIBLL.GetListByFTId(typeEntity.FTId, userInfo.userId).Where(m => m.FIsH5 == true);
  384. foreach (var item in datafunction)
  385. {
  386. if (item.FIsManagePage == true)
  387. {
  388. item.FInterfaceUrl = "/SSO/GoTo?sysid=" + DESEncrypt.Encrypt(item.FId, publickey) + "&openid=" + DESEncrypt.Encrypt(logininfo.userId, publickey);
  389. }
  390. else
  391. {
  392. item.FInterfaceUrl = item.FUrl;
  393. }
  394. item.FUrl = null;
  395. }
  396. typeEntity.PermFunction = datafunction.ToList();
  397. }
  398. return Success(datatype);
  399. }
  400. /// <summary>
  401. /// 获取IP
  402. /// </summary>
  403. /// <returns></returns>
  404. private string GetIP()
  405. {
  406. string ip = string.Empty;
  407. if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
  408. ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
  409. if (string.IsNullOrEmpty(ip))
  410. ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
  411. return ip;
  412. }
  413. }
  414. }