HttpContextExtensions.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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) GetAuthTokenInfo(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. /// 取得User-Agent值
  72. /// </summary>
  73. public static string GetUserAgent(this HttpContext httpContext)
  74. {
  75. try
  76. {
  77. return httpContext.Request.Headers["User-Agent"].ToString();
  78. }
  79. catch
  80. {
  81. return null;
  82. }
  83. }
  84. /// <summary>
  85. /// 取得Scheme值
  86. /// </summary>
  87. public static string GetScheme(this HttpContext httpContext)
  88. {
  89. return httpContext?.Request?.Scheme;
  90. }
  91. /// <summary>
  92. /// 取得HostName值
  93. /// </summary>
  94. public static string GetHostName(this HttpContext httpContext)
  95. {
  96. return httpContext?.Request?.Host.ToString();
  97. }
  98. /// <summary>
  99. /// 设置本地cookie
  100. /// </summary>
  101. /// <param name="key">键</param>
  102. /// <param name="value">值</param>
  103. /// <param name="minutes">过期时长,单位:分钟</param>
  104. public static void SetCookies(HttpResponse Response, string key, string value, int minutes = 30)
  105. {
  106. Response.Cookies.Append(key, value, new CookieOptions
  107. {
  108. Expires = DateTime.Now.AddMinutes(minutes)
  109. });
  110. }
  111. /// <summary>
  112. /// 删除指定的cookie
  113. /// </summary>
  114. /// <param name="key">键</param>
  115. public static void DeleteCookies(HttpContext httpContext, string key)
  116. {
  117. httpContext.Response.Cookies.Delete(key);
  118. }
  119. }
  120. }