BaseController.cs 4.0 KB

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