BrowserUtility.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. 文件名:BrowserUtility.cs
  17. 文件功能描述:浏览器公共类
  18. 创建标识:Senparc - 20150419
  19. 修改标识:Senparc - 20161219
  20. 修改描述:v4.9.6 修改错别字:Browser->Browser
  21. 修改标识:Senparc - 20161219
  22. 修改描述:v4.11.2 修改SideInWeixinBrowser判断逻辑
  23. 修改标识:Senparc - 20180513
  24. 修改描述:v4.11.2 1、增加对小程序请求的判断方法 SideInWeixinMiniProgram()
  25. 2、添加 GetUserAgent() 方法
  26. ---- CO2NET ----
  27. 修改标识:Senparc - 20180601
  28. 修改描述:v5.0.0 引入 Senparc.CO2NET
  29. 修改标识:Senparc - 20180618
  30. 修改描述:v5.0.1 引入 Senparc.CO2NET v0.1.1,BrowserUtility.GetUserAgent() 返回原始字符串,不再返回大写
  31. ----------------------------------------------------------------*/
  32. using System;
  33. using System.Web;
  34. #if NETSTANDARD2_0 || NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2
  35. using Microsoft.AspNetCore.Http;
  36. #endif
  37. namespace Senparc.Weixin.BrowserUtility
  38. {
  39. /// <summary>
  40. /// 浏览器公共类
  41. /// </summary>
  42. public static class BrowserUtility
  43. {
  44. /// <summary>
  45. /// 判断是否在微信内置浏览器中
  46. /// </summary>
  47. /// <param name="httpContext">HttpContextBase对象</param>
  48. /// <returns>true:在微信内置浏览器内。false:不在微信内置浏览器内。</returns>
  49. #if NET40 || NET45
  50. public static bool SideInWeixinBrowser(this HttpContextBase httpContext)
  51. #else
  52. public static bool SideInWeixinBrowser(this HttpContext httpContext)
  53. #endif
  54. {
  55. string userAgent = CO2NET.Utilities.BrowserUtility.GetUserAgent(httpContext.Request).ToUpper();
  56. //判断是否在微信浏览器内部
  57. var isInWeixinBrowser = userAgent != null &&
  58. (userAgent.Contains("MICROMESSENGER")/*MicroMessenger*/ ||
  59. userAgent.Contains("WINDOWS PHONE")/*Windows Phone*/);
  60. return isInWeixinBrowser;
  61. }
  62. /// <summary>
  63. /// 判断是否在微信小程序内发起请求(注意:此方法在Android下有效,在iOS下暂时无效!)
  64. /// </summary>
  65. /// <param name="httpContext">HttpContextBase对象</param>
  66. /// <returns>true:在微信内置浏览器内。false:不在微信内置浏览器内。</returns>
  67. #if NET40 || NET45
  68. public static bool SideInWeixinMiniProgram(this HttpContextBase httpContext)
  69. #else
  70. public static bool SideInWeixinMiniProgram(this HttpContext httpContext)
  71. #endif
  72. {
  73. string userAgent = CO2NET.Utilities.BrowserUtility.GetUserAgent(httpContext.Request).ToUpper();
  74. //判断是否在微信小程序的 web-view 组件内部
  75. var isInWeixinMiniProgram = userAgent != null && userAgent.Contains("MINIPROGRAM")/*miniProgram*/;
  76. return isInWeixinMiniProgram;
  77. }
  78. }
  79. }