微信小程序 登录 服务器 c,asp.net core 3.x 微信小程序登录库(也可用于abp)
库版本.net core 3.1
我的abp版本:abp5.3 .net core 3.1
请先看微信小程序官方文档。下面说说abp中如何使用。原生asp.net core可以参考实现
服务端配置
1、安装nuget包
install-package bxjg.wechart -version 1.0.0
2、修改配置文件 abp.web.host/appsettings.json
3、修改启动配置类abp.web.host//startupauthconfigurer.cs
因为startup中是通过这个类中的静态方法注册身份验证相关服务的
1 public static void configure(iservicecollection services, iconfiguration configuration)
2 {
3 var authbuilder = services.addauthentication(options =>
4 {
5 options.defaultauthenticatescheme = "jwtbearer";
6 options.defaultchallengescheme = "jwtbearer";
7 });
8
9 if (bool.parse(configuration["authentication:jwtbearer:isenabled"]))
10 {
11 authbuilder.addjwtbearer("jwtbearer", options =>
12 {
13 options.audience = configuration["authentication:jwtbearer:audience"];
14
15 options.tokenvalidationparameters = new tokenvalidationparameters
16 {
17 // the signing key must match!
18 validateissuersigningkey = true,
19 issuersigningkey = new symmetricsecuritykey(encoding.ascii.getbytes(configuration["authentication:jwtbearer:securitykey"])),
20
21 // validate the jwt issuer (iss) claim
22 validateissuer = true,
23 validissuer = configuration["authentication:jwtbearer:issuer"],
24
25 // validate the jwt audience (aud) claim
26 validateaudience = true,
27 validaudience = configuration["authentication:jwtbearer:audience"],
28
29 // validate the token expiry
30 validatelifetime = true,
31
32 // if you want to allow a certain amount of clock drift, set that here
33 clockskew = timespan.zero
34 };
35
36 options.events = new jwtbearerevents
37 {
38 onmessagereceived = querystringtokenresolver
39 };
40 });
41 }
42
43 if (bool.parse(configuration["authentication:wechartminiprogram:isenabled"]))
44 {
45 authbuilder.addwechartminiprogram(opt =>
46 {
47 opt.appid = configuration["authentication:wechartminiprogram:appid"];
48 opt.secret = configuration["authentication:wechartminiprogram:secret"];
49
50 opt.claimactions.mapjsonkey("nickname", "nickname");
51 opt.claimactions.mapjsonkey("avatarurl", "avatarurl");
52 opt.claimactions.mapjsonkey("gender", "gender");
53 opt.claimactions.mapjsonkey("country", "country");
54 opt.claimactions.mapjsonkey("province", "province");
55 opt.claimactions.mapjsonkey("city", "city");
56 opt.claimactions.mapjsonkey("language", "language");
57 });
58 }
59 }
更多配置请参考视频
4、实现abp集成
找到abp.web.core/controllers/tokenauthcontroller
先注入usermanager
然后添加下面的方法
1 [httppost]
2 public async task wechartminiprogramloginasync()
3 {
4
5 //从第三方登录拿到当前用户(包含openid、sessionkey)
6 var t = await base.httpcontext.authenticateasync(miniprogramconsts.authenticationscheme);//间接使用第三方身份验证方案获取信息
7 //拿到openid
8 var openid = t.principal.claims.single(c => c.type == claimtypes.nameidentifier).value;
9 var tenancyname = gettenancynameornull();
10 //尝试做第三发登录(内部通过openid找到本地账号做登录),
11 var loginresult = await _loginmanager.loginasync(new userlogininfo(miniprogramconsts.authenticationscheme, openid, miniprogramconsts.authenticationschemedisplayname), tenancyname);
12 //根据登录结果,若成功则直接返回jwttoken 或者自动注册后返回
13 switch (loginresult.result)
14 {
15 case abploginresulttype.success:
16 {
17 //更新微信用户信息
18 foreach (var item in t.principal.claims)
19 {
20 await usermanager.replaceclaimasync(loginresult.user, new claim(item.type, ""), item);
21 }
22
23 //返回jwttoken
24 var accesstoken = createaccesstoken(createjwtclaims(loginresult.identity));
25 return new externalauthenticateresultmodel
26 {
27 accesstoken = accesstoken,
28 encryptedaccesstoken = getencryptedaccesstoken(accesstoken),
29 expireinseconds = (int)_configuration.expiration.totalseconds
30 };
31 }
32 case abploginresulttype.unknownexternallogin:
33 {
34 //若未找到关联的本地账号则自动注册,再返回jwttoken
35 var newuser = await registerexternaluserasync(new externalauthuserinfo
36 {
37 provider = miniprogramconsts.authenticationscheme,
38 providerkey = openid,
39 name = t.principal.claims.singleordefault(c => c.type == "nickname")?.value,
40 emailaddress = guid.newguid().tostring("n") + "@mp.com",
41 surname = "a"
42 });
43 if (!newuser.isactive)
44 {
45 return new externalauthenticateresultmodel
46 {
47 waitingforactivation = true
48 };
49 }
50
51 // try to login again with newly registered user!
52 loginresult = await _loginmanager.loginasync(new userlogininfo(miniprogramconsts.authenticationscheme, openid, miniprogramconsts.authenticationschemedisplayname), tenancyname);
53 if (loginresult.result != abploginresulttype.success)
54 {
55 throw _abploginresulttypehelper.createexceptionforfailedloginattempt(
56 loginresult.result,
57 openid,
58 tenancyname
59 );
60 }
61 //保存微信用户信息(排出openid,因为它存储在userlogins里)
62 await usermanager.addclaimsasync(loginresult.user, t.principal.claims.where(c=>c.type!= claimtypes.nameidentifier));
63
64 return new externalauthenticateresultmodel
65 {
66 accesstoken = createaccesstoken(createjwtclaims(loginresult.identity)),
67 expireinseconds = (int)_configuration.expiration.totalseconds
68 };
69 }
70 default:
71 {
72 throw _abploginresulttypehelper.createexceptionforfailedloginattempt(
73 loginresult.result,
74 openid,
75 tenancyname
76 );
77 }
78 }
79 }
小程序端处理
小程序调用wx.login拿到code,然后调用wx.getuserinfo拿到用户昵称、头像、性别.....等数据
将上面的数据组成json post提交到 我方服务器/wechart-miniprogram-signin
此时会返回一个加密的cookie字符串,小程序端需要想法从响应的cookie中拿到此字符串
用上面的字符串作为cookie post请求 我方服务器/api/tokenauth/wechartminiprogramlogin
此时服务端会返回jwttoken
后续请求跟之前的处理就一样了。
微信小程序 登录 服务器 c,asp.net core 3.x 微信小程序登录库(也可用于abp)相关推荐
- 重学ASP.NET Core 中的标记帮助程序
标记帮助程序是什么 标记帮助程序使服务器端代码可以在 Razor 文件中参与创建和呈现 HTML 元素. 例如,内置的 ImageTagHelper 可以将版本号追加到图片名称. 每当图片发生变化时 ...
- ASP.NET Core 登录登出 - ASP.NET Core 基础教程 - 简单教程,简单编程
ASP.NET Core 登录登出 - ASP.NET Core 基础教程 - 简单教程,简单编程 原文:ASP.NET Core 登录登出 - ASP.NET Core 基础教程 - 简单教程,简单 ...
- Asp.Net Core SignalR 与微信小程序交互笔记
什么是Asp.Net Core SignalR Asp.Net Core SignalR 是微软开发的一套基于Asp.Net Core的与Web进行实时交互的类库,它使我们的应用能够实时的把数据推送给 ...
- 菜市场小程序推荐服务器,极力推荐的3款生鲜微信小程序,总有一个能用上!...
下班回家晚?不想去菜市场买菜?还是去菜市场想买的菜都卖光了?这些问题都不大,今天全网小编给大家极力推荐的3款生鲜微信小程序,总有一个能用上! 随着人们生活水平的普遍提高,海鲜乘客平常百姓家餐桌上的常见 ...
- 抖音微信登录服务器繁忙,抖音无法正常使用微信登录_抖音微信登陆失败解决方法_游戏吧...
抖音出现了无法正常使用微信登陆的问题,很多小伙伴都非常的苦恼,这到底是怎么回事呢?下面游戏吧小编就为各位玩家带来了抖音微信登陆失败解决方法. 抖音微信登陆失败解决方法 1.登录的时候没有同意授权; 2 ...
- ASP.NET Core 2.0和Angular 4:从头开始构建用于车辆管理的Web应用程序
目录 介绍 背景 使用代码 I)服务器端 a)先决条件 b)设置项目 c)设置数据库 d)使用AutoMapper e)使用Swagger f)运行API II)客户端 a)先决条件 b)设置项目 c ...
- linux运行core控制台程序,VisualStudioCode创建的asp.net core控制台程序部署到linux
1.asp.net core控制台程序 static void Main(string[] args) {int times=10;while(times>=0) { Console.Write ...
- linux .net 控制台应用程序,VisualStudioCode创建的asp.net core控制台程序部署到linux
1.asp.net core控制台程序 static void Main(string[] args) {int times=10;while(times>=0) { Console.Write ...
- 坦克世界登录服务器未响应,为你操作win7系统坦克世界登录连接不上服务器的方案_...
win7系统有很多人都喜欢使用,我们操作的过程中常常会碰到win7系统坦克世界登录连接不上服务器的问题.如果遇到win7系统坦克世界登录连接不上服务器该怎么办呢?很多电脑水平薄弱的网友不知道win7系 ...
最新文章
- Fine-tune之后的NLP新范式:Prompt越来越火,CMU华人博士后出了篇综述文章
- java矩阵传递给r_从JAVA调用R得到卡方统计和p值
- Spring起步(一)Building a RESTful Web Service
- 一种神经元探索系统方法及装置
- 神秘大三角(判断点与三角形的关系)
- oracle 取整点的数据,Oracle SQL语句操作数字:取整、四舍五入及格式化
- 分布式应用中的一致性协议
- UVa 1347 旅行
- System.out和System.err的区别
- excel线性拟合的斜率_如何利用EXCEL求直线斜率?
- Java 应用Nginx+ffmpeg实现海康视频web直播
- Python扫码登录保存和验证cookies值——微视篇(三)
- 用来判断当前python语句在分支结构中是_【单选题】哪个选项是用来判断当前 Python 语句在分支结构中?...
- 《ZigBee开发笔记》第五部分 外设篇 - 协议栈实验 第4章 CC2530热释电红外传感器
- html百度天气查询api,百度提供天气预报查询接口API
- 怎样一键制作设计毛笔字体?
- Daimayuan Online Judge 蒟蒻
- 轻文章-维修2台IBM服务器
- Linux:详解talk服务的启用和talk命令使用
- 饲料配方软件最新修改方案
热门文章
- Java模拟实现银行系统
- 2019年值得关注的区块链4大趋势
- 计算机操作系统的目标和作用
- 耳蜗ndows版本不支持该处理器,人工耳蜗植入体会老化吗?跟不上处理器升级又该怎么办?...
- java学习——面向对象
- lingo减少迭代次数、减少求解时间的方法
- 如果你喜欢上了一个程序员小伙-献给所有的程序员女友
- UG安装出现Server Start Failed. The Server May Already Be Running!!解决方法
- 家电双11来了,苏宁418搞了个大事,搅动电商江湖
- NORDIC nrf52833使用笔记