JeffreySu / WeiXinMPSDK

微信全平台 .NET SDK, Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 8.0。已支持微信公众号、小程序、小游戏、微信支付、企业微信/企业号、开放平台、JSSDK、微信周边等全平台。 WeChat SDK for C#.
https://weixin.senparc.com
Apache License 2.0
8.43k stars 4.35k forks source link

Asp.Net Core 中,支付回调时,ResponseHandler的构造函数,读取Form异常。 #283

Closed joesjiang closed 7 years ago

joesjiang commented 7 years ago

异常信息: System.InvalidOperationException: Incorrect Content-Type: text-xml at Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm() at Senparc.Weixin.MP.TenPayLibV3.ResponseHandler..ctor(HttpContext httpContext) in E:\Senparc项目\WeiXinMPSDK\src\Senparc.Weixin.MP\Senparc.Weixin.MP\TenPayLibV3\ResponseHandler.cs:line 99 at Weishop.Areas.Mobile.Controllers.WeixinController.PayNotify() in ...

原因:在ResponseHandler的构造函数中读取Form信息时,仅判断了 HttpMethod ,没有判断 Content-Type。而在Asp.Net Core的对应实现中是会判断读取Form时Content-type :

https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNetCore.Http/Features/FormFeature.cs

joesjiang commented 7 years ago

微信回调的 Content-Type 为 text-xml,在这种情况下,不能获取 Request.Form。

joesjiang commented 7 years ago

修复方案,可以简单的加上 ContentType 的判断即可。

ResponseHandler.cs 的98行:

if (this.HttpContext.Request.Method.ToUpper() == "POST")

修改为:

if (this.HttpContext.Request.Method.ToUpper() == "POST" && this.HttpContext.Request.HasFormContentType)