asp.net core 网页接入微信扫码登录

创建微信开放平台账号,然后创建网页应用
在这里插入图片描述
获取appid和appsecret
在这里插入图片描述
在这里插入图片描述

前端使用的vue,安装插件vue-wxlogin
在这里插入图片描述
调用代码

<wxlogin :appid="appId" :scope="scope" :redirect_uri="redirect_uri"></wxlogin>
<script>
import wxlogin from "vue-wxlogin";

const appId = ref("wx开头的appid");
const scope = ref("snsapi_login");
const redirect_uri = ref("跳转的URL,需要URL编码");//比如原来是http://www.baidu.com需要填写http%3A%2F%2Fwww.baidu.com,使用encodeURIComponent即可实现
</script>

这样就能出来二维码了,微信扫码之后有一个codehttps://www.xxxx.com/?code=011UQe100NJMTR1tsG100BNvo93UQe1n&state=
前端需要返回code给后台,后台解析
安装Nuget包https://github.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat
在这里插入图片描述
在这里插入图片描述
实现解密code,需要读取appid和appsecret,这两个参数都放到appsettings.json中

 "OpenWeixin": {
   "OpenWechatAppId": "",
   "OpenWechatAppSecret": ""
 }

OpenWeixinOptions

services.AddConfigurableOptions<OpenWeixinOptions>();

public class OpenWeixinOptions : IConfigurableOptions
{
    /// <summary>
    /// 微信开放平台appid
    /// </summary>
    public string OpenWechatAppId { get; set; }

    /// <summary>
    /// 微信开放平台appsecret
    /// </summary>
    public string OpenWechatAppSecret { get; set; }
}

编写帮助类OpenWechatHelper

using Furion;
using Furion.DependencyInjection;
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;

/// <summary>
/// 微信开放平台帮助类
/// </summary>
public class OpenWechatHelper : ISingleton
{
    private WechatApiClient client;

    /// <summary>
    /// 构造函数注入
    /// </summary>
    public OpenWechatHelper()
    {
        var wechatConfig = App.GetOptions<OpenWeixinOptions>();
        var options = new WechatApiClientOptions()
        {
            AppId = wechatConfig.OpenWechatAppId,
            AppSecret = wechatConfig.OpenWechatAppSecret,
        };
        client = new WechatApiClient(options);
    }

    /// <summary>
    /// 根据code获取某个应用下的微信用户id
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public async Task<string> GetOpenId(string code)
    {
        var request = new SnsOAuth2AccessTokenRequest();
        request.Code = code;
        var tokenResponse = await client.ExecuteSnsOAuth2AccessTokenAsync(request);
        if (!tokenResponse.IsSuccessful())
        {
            return string.Empty;
        }

        return tokenResponse.OpenId;
    }
}

剩下的按照业务逻辑处理即可

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
ASP.NET Core Web 可以通过微信支付提供的 API 进行扫码支付的接入。下面是实现步骤: 1. 申请微信支付账号和开通扫码支付功能。在申请过程中,需要提供商户信息和开户银行信息等。 2. 在 ASP.NET Core Web 中引入微信支付 SDK,可以通过 NuGet 包管理器安装。 ``` Install-Package Senparc.Weixin.MP.Pay ``` 3. 在 ASP.NET Core Web 项目中添加配置文件 appsettings.json,用于存储微信支付相关的配置参数,如下所示: ``` { "WeixinPay": { "MchId": "微信支付分配的商户号", "AppId": "应用ID", "Key": "商户支付密钥" } } ``` 4. 在 ASP.NET Core Web 项目中创建一个控制器,用于处理扫码支付的请求。控制器代码如下所示: ``` [Route("api/[controller]")] [ApiController] public class WeixinPayController : ControllerBase { private readonly IOptions<WeixinPayOptions> _options; public WeixinPayController(IOptions<WeixinPayOptions> options) { _options = options; } [HttpPost("unifiedorder")] public async Task<IActionResult> UnifiedOrder([FromBody]UnifiedorderRequest request) { // 设置请求参数 var data = new TenPayV3UnifiedorderRequestData( body: request.Body, outTradeNo: request.OutTradeNo, totalFee: request.TotalFee, spbillCreateIp: request.SpbillCreateIp, notifyUrl: request.NotifyUrl, tradeType: "NATIVE", productId: request.ProductId ); // 调用统一下单 API 进行支付 var result = await TenPayV3.UnifiedorderAsync(_options.Value.AppId, _options.Value.MchId, _options.Value.Key, data); // 处理返回结果 if (result.ReturnCode == "SUCCESS" && result.ResultCode == "SUCCESS") { // 生成二维码图片 var url = result.CodeUrl; var qrCode = new QRCodeGenerator().CreateQrCode(url, QRCodeGenerator.ECCLevel.Q); // 返回二维码图片 return File(qrCode.GetGraphic(20), "image/png"); } // 返回错误信息 return BadRequest(result.ReturnMsg); } } ``` 5. 创建一个模型类,用于保存扫码支付的请求参数。 ``` public class UnifiedorderRequest { public string Body { get; set; } public string OutTradeNo { get; set; } public int TotalFee { get; set; } public string SpbillCreateIp { get; set; } public string NotifyUrl { get; set; } public string ProductId { get; set; } } ``` 6. 在 Startup.cs 文件中添加微信支付相关的配置,代码如下所示: ``` services.Configure<WeixinPayOptions>(Configuration.GetSection(nameof(WeixinPayOptions))); ``` 7. 启动 ASP.NET Core Web 项目,使用 Postman 或其他工具向接口发送扫码支付的请求,请求参数包括:商品描述、商户订单号、总金额、终端 IP、通知地址、商品 ID 等。 ``` POST /api/weixinpay/unifiedorder HTTP/1.1 Host: localhost:5000 Content-Type: application/json { "body": "测试商品", "outTradeNo": "123456", "totalFee": 1, "spbillCreateIp": "127.0.0.1", "notifyUrl": "http://localhost:5000/api/weixinpay/notify", "productId": "123456" } ``` 8. 如果请求成功,将返回一个二维码图片,可以使用扫码工具扫描该二维码进行支付。如果请求失败,将返回错误信息。 以上就是 ASP.NET Core Web 支付功能接入微信扫码支付的实现步骤。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

假装我不帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值