支付宝APP支付后端WebAPI(.NET)异步回调验签

一、直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    /// <summary>
    /// App支付回调
    /// </summary>
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [RoutePrefix("api/AliPayCallBack")]
    public class AliPayCallBackController : ApiController
    {
        string publics = "支付宝公钥";
        /// <summary>
        /// 支付宝APP支付回调接口
        /// </summary>
        /// <param name="coll"></param>
        /// <returns></returns>
        [HttpGet]
        [HttpPost]
        public HttpResponseMessage GetAliPayNotify()
        {
            NameValueCollection collection = HttpContext.Current.Request.Form;
            String[] requestItem = HttpContext.Current.Request.Form.AllKeys;
            IDictionary<string, string> sArray = new Dictionary<string, string>();
            for (int i = 0; i < requestItem.Length; i++)
            {
                sArray.Add(requestItem[i], collection[requestItem[i]]);
            }
            bool flag = AlipaySignature.RSACheckV1(sArray, publics, "UTF-8", "RSA2", false);
            HttpResponseMessage res = new HttpResponseMessage();
            if (flag)
            {
                res.Content = new StringContent("success", Encoding.GetEncoding("UTF-8"), "text/plain");
         
            } else
            {
                res.Content = new StringContent("fail", Encoding.GetEncoding("UTF-8"), "text/plain");
            }
            return res;
        }
     
    }

二、注意

1、私钥签名、公钥验签

2、公钥加密、私钥解密

3、支付宝官方示例代码有坑,这是本人修改验证后的代码,仅供个人参考,若有错误欢迎留言,勿喷。