UrlUtility.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:UrlUtility.cs
  17. 文件功能描述:URL工具类
  18. 创建标识:Senparc - 20170912
  19. 修改标识:Senparc - 20170917
  20. 修改描述:修改GenerateOAuthCallbackUrl(),更方便移植到.NET Core
  21. 修改标识:Senparc - 20180502
  22. 修改描述:v4.20.3 为 .NET Core 优化 UrlUtility.GenerateOAuthCallbackUrl() 方法中的端口获取过程
  23. 修改标识:Senparc - 20180502
  24. 修改描述:v5.1.3 优化 UrlUtility.GenerateOAuthCallbackUrl() 方法
  25. 修改标识:Senparc - 20180909
  26. 修改描述:v6.0.4 UrlUtility.GenerateOAuthCallbackUrl() 方法,更好支持反向代理
  27. 修改标识:Senparc - 20180917
  28. 修改描述:v6.1.1 还原上一个版本 v6.0.4 的修改
  29. 修改标识:Senparc - 20190123
  30. 修改描述:v6.3.6 支持在子程序环境下获取 OAuth 回调地址
  31. ----------------------------------------------------------------*/
  32. using System;
  33. using System.Collections.Generic;
  34. using System.Linq;
  35. using System.Text;
  36. using Senparc.CO2NET.Extensions;
  37. #if NET35 || NET40 || NET45
  38. using System.Web;
  39. #else
  40. using System.Net.Http;
  41. using System.Net.Http.Headers;
  42. using Microsoft.AspNetCore.Http;
  43. #endif
  44. using Senparc.Weixin.Exceptions;
  45. namespace Senparc.Weixin.HttpUtility
  46. {
  47. /// <summary>
  48. /// URL工具类
  49. /// </summary>
  50. public class UrlUtility
  51. {
  52. /// <summary>
  53. /// 生成OAuth用的CallbackUrl参数(原始状态,未整体进行UrlEncode)
  54. /// </summary>
  55. /// <param name="httpContext"></param>
  56. /// <param name="oauthCallbackUrl"></param>
  57. /// <returns></returns>
  58. #if NET40 || NET45
  59. public static string GenerateOAuthCallbackUrl(HttpContextBase httpContext, string oauthCallbackUrl)
  60. #else
  61. public static string GenerateOAuthCallbackUrl(HttpContext httpContext, string oauthCallbackUrl)
  62. #endif
  63. {
  64. #if NET35 || NET40 || NET45
  65. if (httpContext.Request.Url == null)
  66. {
  67. throw new WeixinNullReferenceException("httpContext.Request.Url 不能为null!", httpContext.Request);
  68. }
  69. var returnUrl = httpContext.Request.Url.ToString();
  70. var urlData = httpContext.Request.Url;
  71. var scheme = urlData.Scheme;//协议
  72. var host = urlData.Host;//主机名(不带端口)
  73. var port = urlData.Port;//端口
  74. string portSetting = null;//Url中的端口部分
  75. string schemeUpper = scheme.ToUpper();//协议(大写)
  76. string baseUrl = httpContext.Request.ApplicationPath;//子站点应用路径
  77. #else
  78. if (httpContext.Request == null)
  79. {
  80. throw new WeixinNullReferenceException("httpContext.Request 不能为null!", httpContext);
  81. }
  82. var request = httpContext.Request;
  83. //var location = new Uri($"{request.Scheme}://{request.Host}{request.Path}{request.QueryString}");
  84. //var returnUrl = location.AbsoluteUri; //httpContext.Request.Url.ToString();
  85. var returnUrl = request.AbsoluteUri();
  86. var urlData = httpContext.Request;
  87. var scheme = urlData.Scheme;//协议
  88. var host = urlData.Host.Host;//主机名(不带端口)
  89. var port = urlData.Host.Port ?? -1;//端口(因为从.NET Framework移植,因此不直接使用urlData.Host)
  90. string portSetting = null;//Url中的端口部分
  91. string schemeUpper = scheme.ToUpper();//协议(大写)
  92. string baseUrl = httpContext.Request.PathBase;//子站点应用路径
  93. #endif
  94. if (port == -1 || //这个条件只有在 .net core 中, Host.Port == null 的情况下才会发生
  95. (schemeUpper == "HTTP" && port == 80) ||
  96. (schemeUpper == "HTTPS" && port == 443))
  97. {
  98. portSetting = "";//使用默认值
  99. }
  100. else
  101. {
  102. portSetting = ":" + port;//添加端口
  103. }
  104. //授权回调字符串
  105. var callbackUrl = string.Format("{0}://{1}{2}{6}{3}{4}returnUrl={5}",
  106. scheme,
  107. host,
  108. portSetting,
  109. oauthCallbackUrl,
  110. oauthCallbackUrl.Contains("?") ? "&" : "?",
  111. returnUrl.UrlEncode(),
  112. //添加应用目录:https://github.com/JeffreySu/WeiXinMPSDK/issues/1552
  113. baseUrl
  114. );
  115. return callbackUrl;
  116. }
  117. }
  118. }