BaseController.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Microsoft.AspNetCore.Mvc;
  2. namespace IES.ExamServer.Controllers
  3. {
  4. public class BaseController : ControllerBase
  5. {
  6. public BaseController()
  7. {
  8. }
  9. public string GetIP()
  10. {
  11. var IpPort = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
  12. if (string.IsNullOrEmpty(IpPort))
  13. {
  14. IpPort = $"{HttpContext.Connection.RemoteIpAddress}";
  15. }
  16. if (IpPort.Contains("::"))
  17. {
  18. IpPort = "127.0.0.1";
  19. }
  20. return IpPort;
  21. }
  22. public string GetCookie(string key)
  23. {
  24. IRequestCookieCollection cookies = HttpContext.Request.Cookies;
  25. string value = "";
  26. if (cookies != null)
  27. {
  28. foreach (var ck in cookies)
  29. {
  30. if (ck.Key.Equals(key))
  31. {
  32. value = ck.Value;
  33. break;
  34. }
  35. }
  36. }
  37. return value;
  38. }
  39. /// <summary>
  40. /// 取得AuthToken權杖資訊
  41. /// </summary>
  42. /// <param name="key">Key Name</param>
  43. /// <returns></returns>
  44. public (string id, string? name, string picture, string school,string scope ,string timeZone,List<string> rolse, string keyData) GetAuthTokenInfo(string? key = null)
  45. {
  46. object? keyData = null;
  47. HttpContext.Items.TryGetValue("ID", out object? id);
  48. HttpContext.Items.TryGetValue("Name", out object? name);
  49. HttpContext.Items.TryGetValue("Picture", out object? picture);
  50. HttpContext.Items.TryGetValue("School", out object? school);
  51. HttpContext.Items.TryGetValue("Scope", out object? scope);
  52. HttpContext.Items.TryGetValue("TimeZone", out object? timeZone);
  53. List<string> rolse= new List<string>();
  54. if (HttpContext.Items.TryGetValue("Roles", out object? _roles))
  55. {
  56. if (_roles is List<string> s)
  57. {
  58. rolse=s;
  59. }
  60. }
  61. if (!string.IsNullOrWhiteSpace(key))
  62. {
  63. HttpContext.Items.TryGetValue(key, out keyData);
  64. }
  65. return ($"{id}", $"{name}", $"{picture}", $"{school}",$"{scope}",$"{timeZone}", rolse, $"{keyData}");
  66. }
  67. /// <summary>
  68. /// 取得驗證金鑰,Authorization
  69. /// </summary>
  70. public string GetToken()
  71. {
  72. return HttpContext.Request.Headers["Authorization"].ToString();
  73. }
  74. /// <summary>
  75. /// 取得JWT驗證金鑰,Authorization Bearer
  76. /// </summary>
  77. /// <param name="httpContext"></param>
  78. /// <returns></returns>
  79. public string GetJwtToken()
  80. {
  81. var token = string.Empty;
  82. string authorization = HttpContext.Request.Headers["Authorization"].ToString();
  83. if (!string.IsNullOrWhiteSpace(authorization) && authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
  84. {
  85. token = authorization.Substring("Bearer ".Length).Trim();
  86. }
  87. return token;
  88. }
  89. public int code = 0;
  90. public string msg = "OK";
  91. }
  92. }