首页 技术 正文
技术 2022年11月15日
0 收藏 886 点赞 3,549 浏览 1900 个字

上一节课拿到的AccessToken和IdToken
25.ProfileService实现(调试)

实现ProfileService类

在服务端 添加ProfileService类
25.ProfileService实现(调试)
需要继承IProfileServiuce
25.ProfileService实现(调试)

用到的画图工具

25.ProfileService实现(调试)

IprofileService接口里面有两个方法, 我们去实现这两个方法
这里是否激活,我们先设置为false
25.ProfileService实现(调试)

IsActive当我们拿到Subject的时候再去设置为true,也就是我们这里拿到的sub的值为4的这个。
25.ProfileService实现(调试)

对应我们数据里的这个用户id。AspNetUsers这个表的主键。用户的id是4
25.ProfileService实现(调试)

获取到subjectId
25.ProfileService实现(调试)
然后根据SubjectId拿到用户的信息。注入UserManager。命名空间在AspNetCore.Identity命名空间下
25.ProfileService实现(调试)

通过SubjectId获取到用户的信息,SubjecId其实就是User表的用户Id

25.ProfileService实现(调试)
精简为一行代码。当用户对象不为空就返回true,那么IsActive就是True
25.ProfileService实现(调试)
加上await 和 async
25.ProfileService实现(调试)
我们之前没有启用用户的锁定,如果用户输入错 次数太多。LockoutEnabled就会锁定
25.ProfileService实现(调试)

需要对IssuedClaim进行赋值。这些Claims就是从用户里来的
25.ProfileService实现(调试)

Claim在System.Security.Claims这个命名空间下
25.ProfileService实现(调试)
我们需要通过GetClaimFormUser这个方法来返回IssuedClaims
25.ProfileService实现(调试)

JwtClaimTypes返回的都是常量,说白了就是固定的字符串
25.ProfileService实现(调试)
userName也就是数据库内的Administrator
25.ProfileService实现(调试)
通过user获取到Roles的列表然后 添加到Claims里面
25.ProfileService实现(调试)

还需要加上一个头像的属性。在ApplicationUser里面加上了Avatar头像的这么一个属性值
25.ProfileService实现(调试)
加上Avatar的Claim
25.ProfileService实现(调试)
把这个方法名字加上Async重命名一下
25.ProfileService实现(调试)
这样最终返回了IssuedClaims
25.ProfileService实现(调试)

注入ProfileService类

在StartUp里面

25.ProfileService实现(调试)

数据迁移

因为我们修改了ApplicationUser,所以需要对数据库做更新。因为这里我们增加了Avatar头像的属性,需要对数据库做迁移
25.ProfileService实现(调试)
25.ProfileService实现(调试)
25.ProfileService实现(调试)
更新数据库
25.ProfileService实现(调试)
这样我们头像的字段就加上来了
25.ProfileService实现(调试)

把AspNetUsers表内的数据删除掉

25.ProfileService实现(调试)

Seed初始化数据

删掉数据之后,执行我们的seed方法
25.ProfileService实现(调试)

25.ProfileService实现(调试)
头像地址是从网上随便找的图片的地址
25.ProfileService实现(调试)
然后把默认的用户添加到administrator的角色里面
25.ProfileService实现(调试)
运行 role添加失败了
25.ProfileService实现(调试)

25.ProfileService实现(调试)

25.ProfileService实现(调试)
也有个succeed,如果失败了就抛出一个异常
25.ProfileService实现(调试)

再次运行,又报了个错误
25.ProfileService实现(调试)

随即的一个值,用来设置密码的,这里我们先随便设置成admin
25.ProfileService实现(调试)
运行程序  依然报错
25.ProfileService实现(调试)

主键可能需要改一下,因为它没有办法自动生成
25.ProfileService实现(调试)

ApplicationUserRole也是同样的
25.ProfileService实现(调试)
再去做数据迁移
25.ProfileService实现(调试)
生成的内容比较多,但是有用的就是你这一句
25.ProfileService实现(调试)
角色也加了一些东西
25.ProfileService实现(调试)

然后更新数据库
25.ProfileService实现(调试)
依然失败
25.ProfileService实现(调试)

因为用户还没有创建成功就去给用户设置角色了造成了这个错误
25.ProfileService实现(调试)
还是有错-_-!
25.ProfileService实现(调试)

调试,返回了密码规则错误的问题
25.ProfileService实现(调试)

小写、特殊符号、大写、长度这些密码的规则我们设置为false
25.ProfileService实现(调试)
用户创建成功 返回了succeed
25.ProfileService实现(调试)
user表有数据了
25.ProfileService实现(调试)

头像字段也有值了
25.ProfileService实现(调试)
RoleId给了个1
25.ProfileService实现(调试)

角色表
25.ProfileService实现(调试)

测试

把cookie都清除掉
25.ProfileService实现(调试)

启动客户端。这样就可以使用123456 简单的密码来登陆

25.ProfileService实现(调试)

点同意。登陆成功之后跳转回去
25.ProfileService实现(调试)

25.ProfileService实现(调试)

再次清除缓存,登陆
 25.ProfileService实现(调试)

客户端
25.ProfileService实现(调试)

这里调试用VScode调试
25.ProfileService实现(调试)

25.ProfileService实现(调试)
可以看到只有三个Claims
25.ProfileService实现(调试)

服务端需要设置这里。这里做映射,从prolfileServie拿到Claim做一个绑定,绑定过之后才可以从从ProfileService返回里面去拿到
25.ProfileService实现(调试)

25.ProfileService实现(调试)
这个端口可以通过Access_token拿到
25.ProfileService实现(调试)

再次运行测试
25.ProfileService实现(调试)
role和avatar都拿到了
25.ProfileService实现(调试)

客户端的设置。openIdConnect这里自己要做一个映射
25.ProfileService实现(调试)

如果不这么做也可以通过Access_Token
25.ProfileService实现(调试)

这里是发起了另外一个请求到我们5000的端口
25.ProfileService实现(调试)

其实就相当于,发送了一次请求,把获取的token又访问了一次这个地址拿到UserInfo的这几个信息
25.ProfileService实现(调试)
我们还可以把idToken复制到jwt.io解密出来
25.ProfileService实现(调试)
他没有包含头像的信息,这是因为它是另外发起一个请求到endPoint去哪头像这些信息
所以客户端的这里,GetCliamsFormUserInfoEndpoint设置为了True
25.ProfileService实现(调试)
这里注意,设置的也就是页面上拿到的IdToken
25.ProfileService实现(调试)

服务端设置,这里为true,那么这些信息都被加密在idtoken里面
25.ProfileService实现(调试)
客户端这里就不用设置了。
25.ProfileService实现(调试)
客户端这里也可以去掉了
25.ProfileService实现(调试)

一种是通过发起另外的请求去获取用户的信息,另外一种是在访问idToken直接就带过来了
客户端和服务端再重启
客户端的cookie再次清除
25.ProfileService实现(调试)
25.ProfileService实现(调试)
这样用户的信息直接在idToken里面都返回了
25.ProfileService实现(调试)
解密下idToken

25.ProfileService实现(调试)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850