ASP.Net Core 获取微信小程序 OpenId 验证

Senparc 官网地址:https://sdk.weixin.senparc.com/Docs/WxOpen

首先引用 NuGet 包:Senparc.Weixin.WxOpen、Senparc.Weixin.AspNet

然后在 Startup.cs 类中,注意这里用的是老式写法,新的写法在Program.cs 里面,可以到官网地址去查阅具体内容,找到 ConfigureServices 方法里面添加代码:

复制代码
  services.AddMemoryCache();
  services.AddSenparcWeixinServices(Configuration);

再找到 Configure 方法里面添加代码,注意:AppId 在微信小程序平台获取来:

复制代码
  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
      app.UseSenparcWeixin(env, null, null, register => { },
          (register, weixinSetting) =>
      {
          register.RegisterWxOpenAccount(weixinSetting, "【这里填AppId】");
      });
}

然后在 appsettings.json 文件里面根中添加配置字段:

复制代码
  "SenparcWeixinSetting": {
    "IsDebug": true,
    "WxOpenAppId": "【微信小程序平台获取】",
    "WxOpenAppSecret": "【微信小程序平台获取】"
  },

最后随便创建个 Api 控制器,创建一个[http]方法调用内容:

复制代码
  var item = Config.SenparcWeixinSetting.Items["【这里填对应的AppId】"];
  var result = await SnsApi.JsCode2JsonAsync(item.WxOpenAppId, item.WxOpenAppSecret, "【这里填小程序传过来的Code】");
相关推荐
代码栈上的思考1 小时前
消息队列:内存与磁盘数据中心设计与实现
后端·spring
程序员小假2 小时前
我们来说一下 b+ 树与 b 树的区别
java·后端
Meepo_haha2 小时前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端
sheji34162 小时前
【开题答辩全过程】以 基于springboot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
Victor3563 小时前
MongoDB(57)如何优化MongoDB的查询性能?
后端
Victor3563 小时前
MongoDB(58)如何使用索引优化查询?
后端
行百里er3 小时前
优雅应对异常,从“try-catch堆砌”到“设计驱动”
java·后端·代码规范
码财小子3 小时前
聊聊 C++ 模块“注册式”的优雅姿势
后端·代码规范
掘金码甲哥4 小时前
higress 这个中登才是AI时代的心头好
后端
IT_陈寒4 小时前
一文搞懂JavaScript的核心概念
前端·人工智能·后端