HttpContextExtensions.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.Extensions.Primitives;
  6. namespace TEAMModelOS.SDK.Extension
  7. {
  8. public static class HttpContextExtensions
  9. {
  10. /// <summary>
  11. /// 取得驗證金鑰,Authorization
  12. /// </summary>
  13. public static string GetToken(this HttpContext httpContext)
  14. {
  15. return httpContext.Request.Headers["Authorization"].ToString();
  16. }
  17. /// <summary>
  18. /// 取得JWT驗證金鑰,Authorization Bearer
  19. /// </summary>
  20. /// <param name="httpContext"></param>
  21. /// <returns></returns>
  22. public static string GetJwtToken(this HttpContext httpContext)
  23. {
  24. var token = string.Empty;
  25. string authorization = httpContext.Request.Headers["Authorization"].ToString();
  26. if (!string.IsNullOrWhiteSpace(authorization) && authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
  27. {
  28. token = authorization.Substring("Bearer ".Length).Trim();
  29. }
  30. return token;
  31. }
  32. /// <summary>
  33. /// 取得遠端呼叫的IP
  34. /// </summary>
  35. public static string GetRemoteIP(this HttpContext httpContext)
  36. {
  37. return httpContext?.Connection?.RemoteIpAddress?.ToString();
  38. }
  39. /// <summary>
  40. /// 取得X-Auth-Key值
  41. /// </summary>
  42. /// <param name="key">Key Name</param>
  43. /// <returns></returns>
  44. public static string GetXAuth(this HttpContext httpContext, string key = null)
  45. {
  46. try
  47. {
  48. if (httpContext.Request.Headers.TryGetValue($"X-Auth-{key}", out StringValues value))
  49. return value.ToString();
  50. else
  51. return null;
  52. }
  53. catch
  54. {
  55. return null;
  56. }
  57. }
  58. /// <summary>
  59. /// 取得AuthToken權杖資訊
  60. /// </summary>
  61. /// <param name="key">Key Name</param>
  62. /// <returns></returns>
  63. public static (string id, string school) GetApiTokenInfo(this HttpContext httpContext, string key = null)
  64. {
  65. object id = null, school = null;
  66. httpContext?.Items.TryGetValue("ID", out id);
  67. httpContext?.Items.TryGetValue("School", out school);
  68. return (id?.ToString(), school?.ToString());
  69. }
  70. /// <summary>
  71. /// 取得AuthToken權杖資訊
  72. /// </summary>
  73. /// <param name="key">Key Name</param>
  74. /// <returns></returns>
  75. public static (string id, string name, string picture, string school) GetAuthTokenInfo(this HttpContext httpContext, string key = null)
  76. {
  77. object id = null, name = null, picture = null, school = null;
  78. httpContext?.Items.TryGetValue("ID", out id);
  79. httpContext?.Items.TryGetValue("Name", out name);
  80. httpContext?.Items.TryGetValue("Picture", out picture);
  81. httpContext?.Items.TryGetValue("School", out school);
  82. return (id?.ToString(), name?.ToString(), picture?.ToString(), school?.ToString());
  83. }
  84. /// <summary>
  85. /// 取得User-Agent值
  86. /// </summary>
  87. public static string GetUserAgent(this HttpContext httpContext)
  88. {
  89. try
  90. {
  91. return httpContext.Request.Headers["User-Agent"].ToString();
  92. }
  93. catch
  94. {
  95. return null;
  96. }
  97. }
  98. /// <summary>
  99. /// 取得Scheme值
  100. /// </summary>
  101. public static string GetScheme(this HttpContext httpContext)
  102. {
  103. return httpContext?.Request?.Scheme;
  104. }
  105. /// <summary>
  106. /// 取得HostName值
  107. /// </summary>
  108. public static string GetHostName(this HttpContext httpContext)
  109. {
  110. return httpContext?.Request?.Host.ToString();
  111. }
  112. /// <summary>
  113. /// 设置本地cookie
  114. /// </summary>
  115. /// <param name="key">键</param>
  116. /// <param name="value">值</param>
  117. /// <param name="minutes">过期时长,单位:分钟</param>
  118. public static void SetCookies(HttpResponse Response, string key, string value, int minutes = 30)
  119. {
  120. Response.Cookies.Append(key, value, new CookieOptions
  121. {
  122. Expires = DateTimeOffset.Now.AddMinutes(minutes)
  123. });
  124. }
  125. /// <summary>
  126. /// 删除指定的cookie
  127. /// </summary>
  128. /// <param name="key">键</param>
  129. public static void DeleteCookies(HttpContext httpContext, string key)
  130. {
  131. httpContext.Response.Cookies.Delete(key);
  132. }
  133. }
  134. }