IdentityServer4 密码模式认证
授权服务器设置
- 添加用户
添加测试用户,也可以从数据库查
- public static List<TestUser> GetTestUser()
- {
- return new List<TestUser>() {
- new TestUser(){
- SubjectId = "",
- Username ="zps",
- Password = "zps",
- Claims = new List<Claim>(){
- new Claim("role","zps"),
- new Claim("aaa","asdasdsd"),
- }
- },
- new TestUser(){
- SubjectId = "",
- Username ="admin",
- Password = "admin",
- Claims = new List<Claim>(){
- new Claim("role","admin")
- }
- }
- };
- }
- 添加Api资源
添加api资源 ,api的key要和注册的client的api要匹配
- public static IEnumerable<ApiResource> GetResource()
- {
- return new List<ApiResource>(){
- new ApiResource("api","my api")
- };
- }
- 添加客户端
- 客户端模式
- 密码模式
- 授权码模式
- 混合模式
授权码模式和mvc模式的时候 这两个模式先不管
//请求确认
- RequireConsent = false, 这个属性要注意 如果是true 会先跳转到确认页面 然后再跳转到RedirectUris
- public static IEnumerable<Client> GetClients()
- {
- return new List<Client>(){
- new Client(){
- ClientId="client",
- //客户端模式
- AllowedGrantTypes=GrantTypes.ClientCredentials,
- ClientSecrets={new Secret("secret".Sha256())},
- AllowedScopes={"api"}
- },
- new Client(){
- ClientId="pwdClient",
- //OAuth密码模式
- AllowedGrantTypes=GrantTypes.ResourceOwnerPassword,
- ClientSecrets={new Secret("secret".Sha256())},
- AllowedScopes={"api"}
- },
- new Client
- {
- ClientId = "mvc",
- ClientName = "MVC Client",
- AllowedGrantTypes = GrantTypes.Hybrid,
- ClientSecrets =
- {
- new Secret("secret".Sha256())
- },
- // where to redirect to after login
- RedirectUris = { "http://localhost:5001/signin-oidc" },
- RequireConsent = false,
- AllowOfflineAccess = true,
- // where to redirect to after logout
- PostLogoutRedirectUris = { "http://localhost:5001/signout-callback-oidc" },
- AllowedScopes = new List<string>
- {
- IdentityServerConstants.StandardScopes.OpenId,
- IdentityServerConstants.StandardScopes.Profile,
- }
- },
- new Client
- {
- ClientId = "js",
- ClientName = "JavaScript Client",
- AllowedGrantTypes = GrantTypes.Code,
- RequirePkce = true,
- RequireClientSecret = false,
- RedirectUris = { "http://localhost:5003/callback.html" },
- PostLogoutRedirectUris = { "http://localhost:5003/index.html" },
- AllowedCorsOrigins = { "http://localhost:5003" },
- RequireConsent = false,
- AllowedScopes =
- {
- IdentityServerConstants.StandardScopes.OpenId,
- IdentityServerConstants.StandardScopes.Profile,
- "api"
- }
- }
- };
- }
- 添加IdentityServer 保护的资源
可以自定义Claim
- public static IEnumerable<IdentityResource> GetIdentityResources()
- {
- return new IdentityResource[]
- {
- new IdentityResources.OpenId(),
- new IdentityResources.Profile(),
- };
- }
- 把identityserver注入到容器
.AddDeveloperSigningCredential() 生成token 需要的密钥和公钥 正式环境需要换成正经的
o.UserInteraction.LoginUrl = "/Auth/Login";
o.UserInteraction.LogoutUrl = "/Auth/Logout";
- o.UserInteraction.ErrorUrl = "/Auth/Error";
这三个是混合模式需要的 登录的地址 登出的地址 授权失败的地址
- services.AddIdentityServer(o =>
- {
- o.UserInteraction.LoginUrl = "/Auth/Login";
- o.UserInteraction.LogoutUrl = "/Auth/Logout";
- o.UserInteraction.ErrorUrl = "/Auth/Error";
- })
- .AddInMemoryIdentityResources(Config.GetIdentityResources())
- .AddDeveloperSigningCredential()
- .AddInMemoryClients(Config.GetClients())
- .AddInMemoryApiResources(Config.GetResource())
- .AddTestUsers(Config.GetTestUser());
Configure把中间件加到netcore中
- app.UseIdentityServer();
postman测试
- grant-type:密码模式对应 password
- username 用户名
- password 密码
- client_id 客户端id 对应 授权服务ClientId
- client_secret 客户端secret
IdentityServer4 密码模式认证的更多相关文章
- IdentityServer4 密码模式实现
1. 修改 Config.cs using System.Collections; using System.Collections.Generic; using IdentityServer4.M ...
- IdentityServer4密码模式接入现有用户数据表
具体接入identityserver请看文档,这里只简单列举部分步骤 1.创建一个web项目,引入Identityserver4的nuget包 2.新建一个类,实现IResourceOwnerPass ...
- Core篇——初探IdentityServer4(客户端模式,密码模式)
Core篇——初探IdentityServer4(客户端模式,密码模式) 目录 1.Oatuth2协议的客户端模式介绍2.IdentityServer4客户端模式实现3.Oatuth2协议的密码模式介 ...
- asp.net权限认证:OWIN实现OAuth 2.0 之密码模式(Resource Owner Password Credential)
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- asp.net core 使用identityServer4的密码模式来进行身份认证(一)
IdentityServer4是ASP.NET Core的一个包含OpenID和OAuth 2.0协议的框架.具体Oauth 2.0和openId请百度. 前言本博文适用于前后端分离或者为移动产品来后 ...
- 【ASP.NET Core分布式项目实战】(一)IdentityServer4登录中心、oauth密码模式identity server4实现
本博客根据http://video.jessetalk.cn/my/course/5视频整理 资料 OAuth2 流程:http://www.ruanyifeng.com/blog/2014/05/o ...
- IdentityServer4 实现OAuth2.0四种模式之密码模式
接上一篇:IdentityServer4 实现OAuth2.0四种模式之客户端模式,这一篇讲IdentityServer4 使用密码模式保护API访问. 一,IdentityServer配置 1,添加 ...
- OAuth2密码模式已死,最先进的Spring Cloud认证授权方案在这里
旧的Spring Security OAuth2停止维护已经有一段时间了,99%的Spring Cloud微服务项目还在使用这些旧的体系,严重青黄不接.很多同学都在寻找新的解决方案,甚至还有念念不忘密 ...
- IdentityServer4:IdentityServer4+API+Client+User实践OAuth2.0密码模式(2)
一.密码模式实操 仍然使用第一节的代码:做如下改动: 1.授权服务端 前面我们使用项目:Practice.IdentityServer作为授权服务器 修改项目的Config.cs类: 添加测试用户,并 ...
随机推荐
- 使用css实现无滚动条滚动+使用插件自定义滚动条样式
使用css实现无滚动条滚动,摘抄自:曹小萌博客 使用css实现无滚动条滚动,大体思路是在div外面再套一个div.这个div设置overflow:hidden.而内容div设置 overflow-x: ...
- [ZJOI2019]麻将(动态规划,自动机)
[ZJOI2019]麻将(动态规划,自动机) 题面 洛谷 题解 先做一点小铺垫,对于一堆牌而言,我们只需要知道这\(n\)张牌分别出现的次数就行了,即我们只需要知道一个长度为\(n\)的串就可以了. ...
- <数据结构基础学习>(四)链表 Part 1
一.链表基础 动态数组.栈.队列底层都是依托静态数组实现的,靠resize来解决固定容量问题. 链表是真正的动态数据结构,是一种最简单的一种动态数据结构. 更深入的理解引用(或者指针). 更深入的理解 ...
- Linux-Centos破解安装confluene6.3.1
Centos 安装企业wiki confluence是一个专业的企业知识管理与协同软件,可以用于构建企业wiki.通过它可以实现团队成员之间的协作和知识共享.现在大多数公司都会部署一套confluen ...
- NEED TO DO
任务清单 计算几何 KDtree 容斥 后缀自动机套数据结构 FFT 四边形不等式/决策单调性优化 欧拉路 KM算法 BM算法 数论 min25筛 后缀数组 吉司机线段树 生成函数 ...
- ElasticSearch6.5.0 【Java客户端之REST Client】
说明 High Level Client 是基于 Low Level Client 的.官方文档如下: * https://www.elastic.co/guide/en/elasticsearch/ ...
- 自动化运维工具Ansible介绍
一个由 Python 编写的强大的配置管理解决方案.尽管市面上已经有很多可供选择的配置管理解决方案,但他们各有优劣,而 ansible 的特点就在于它的简洁. 让 ansible 在主流的配置管理系统 ...
- 使用vlc打开usb摄像头
打开vlc播放器 可以打开网络串流的方式打开摄像头,但只支持第一个摄像头 这一串地址拼凑方法看下面,下面可以选择摄像头 为什么只支持第一个摄像头可以参考下一篇使用Vlc.DotNet打开摄像头并截图 ...
- shell 通过EOF在脚本中输入需要的用户名或密码
参考地址:https://www.cnblogs.com/liyuanhong/p/10390786.html expect使用参考:https://www.cnblogs.com/liyuanhon ...
- [再寄小读者之数学篇](2014-06-20 Beta 函数)
令 $\dps{B(m,n)=\sum_{k=0}^n C_n^k \cfrac{(-1)^k}{m+k+1}}$, $m,n\in\bbN^+$. (1) 证明 $B(m,n)=B(n,m)$; ( ...