HttpContextExtensions.cs 4.3 KB

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