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类: 添加测试用户,并 ...
随机推荐
- 虚拟机iso整理
供个人备用,随缘补充 ubuntu-16.04.6-desktop-amd64.iso 资源: https://pan.baidu.com/s/1ZR_5jgzNsGeOrkE6hAqxEA 提取码: ...
- Lua语言自学之01.基础概念的理解
编程不只是这么简单,它的思维是理性的编程思维,操纵机器干事本来就不是一件简单的事,要干什么,该怎么做,怎么做得才好. 脚本的概念在程序中十分重要,在游戏开发领域,它更是决定性的.脚本语言让程序员可以区 ...
- [洛谷P2107] 小Z的AK计划
题目类型:贪心,堆 传送门:>Here< 题意:给出\(N\)个房间,每个房间距离起点的距离为\(x[i]\),每个房间可以选择进去和不进去,如果进去了那么要\(t[i]\)秒后才能出来. ...
- 初识 go 语言
目录 go简介 安装 hello world 函数 变量 常量 可见性规则 结束 前言: 最近组内要试水区块链,初步方案定为使用fabirc来弄,而fabric的智能合约就是用go写的,借此机会正好学 ...
- oracle 查询数据库的各种命令
以下查询都是使用plsql查询oracle 11g 1.查询数据库版本信息 select * from v$version; 2.查询数据库优化模式 select name, value from v ...
- MySQL 导入导出数据库、表
使用 GUI 软件很好操作,下面介绍命令行操作. 导出 cmd 命令 # 1.1 导出整个数据库 mysqldump -hlocalhost -uroot -p student_db > C:\ ...
- usb输入子系统写程序(三)
目录 usb输入子系统写程序 小结 内核修改 怎么写代码 类型匹配 probe disconnect 程序设计 1th匹配probe 2th 获取usb数据 3th 输入子系统上报按键 title: ...
- 分布式监控系统开发【day38】:报警阈值程序逻辑解析(三)
一.需求讨论 1.请问如何解决延迟问题 1000台机器,每1分钟循环一次但是刚好第一次循环第一秒刚处理完了,结果还没等到第二分钟又出问题,你那必须等到第二次循环,假如我这个服务很重要必须实时知道,每次 ...
- Servlet中转发和重定向的路径问题【转】
转发和重定向的路径问题 Servlet中有两种方式获得转发对象(RequestDispatcher):一种是通过HttpServletRequest的getRequestDispatcher()方法获 ...
- HDU 5965(三行扫雷 dp)
题意是在一个 3 行 n 列的图上进行扫雷,中间一行没有雷,且中间一行的每一格都会显示周围的雷数,问根据已知的雷数在上下两行设置地雷的方法数. 分析知每一列所填雷数的和与周围的雷数有关,但每列具体的填 ...