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类: 添加测试用户,并 ...
随机推荐
- Unity3d中如何查找一个脚本被挂在那些预设上面?
用一个脚本函数可以获取到选择的脚本文件被哪些预设和场景引用 [MenuItem("Assets/Tool/GetReference")] static void GetRefere ...
- 【XSY3048 】Polynominal 数学
题目描述 给你三个正整数 \(a,b,c\),求有多少个系数均为非负整数的多项式 \(f(x)\) 满足 \(f(a)=b\) 且 \(f(b)=c\) \(a,b,c\leq {10}^{18}\) ...
- Markdown初入门(使用Typora编辑)
标题 使用#来实现标题的大小控制 # h1 标题1 ## h2 标题2 ### h3 标题3 #### h4 标题4 ##### h5 标题5 ###### h6 标题6 标题一 标题二 标题三 标题 ...
- linux虚拟机 在install yum时提示无法获得锁 var/lib/dekg/lock时该如何解决?
起因 在新虚拟机中使用yum命令时提示要安装,安装过程中提示出错. 问题 在sudo apt install yum时提示下列错误该如何解决? E: 无法获得锁 /var/lib/dpkg/lock ...
- [FJOI2018]领导集团问题
[FJOI2018]领导集团问题 dp[i][j],i为根子树,最上面的值是j,选择的最大值 观察dp方程 1.整体Dp已经可以做了. 2.考虑优美一些的做法: dp[i]如果对j取后缀最大值,显然是 ...
- BSGS及扩展BSGS算法及例题
\(BSGS(baby-step-giant-step)\)算法是用来解高次同余方程的最小非负整数解的算法,即形如这个的方程: \(a^x\equiv b(mod\ p)\) 其中\(p\)为质数(其 ...
- Cucumber使用中问题
1.cucumber自动化执行提示chrome使用不支持的命令标记 --ignore-certificate-errors 大概问题是chrome版本和chrmedriver版本不对应 2." ...
- B-树(B树)详解
具体讲解之前,有一点,再次强调下:B-树,即为B树.因为B树的原英文名称为B-tree,而国内很多人喜欢把B-tree译作B-树,其实,这是个非常不好的直译,很容易让人产生误解.如人们可能会以为B-树 ...
- Luogu P3227 [HNOI2013]切糕 最小割
首先推荐一个写的很好的题解,个人水平有限只能写流水账,还请见谅. 经典的最小割模型,很多人都说这个题是水题,但我还是被卡了=_= 技巧:加边表示限制 在没有距离\(<=d\)的限制时候,我们对每 ...
- Hadoop基础-Hadoop的集群管理之服役和退役
Hadoop基础-Hadoop的集群管理之服役和退役 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在实际生产环境中,如果是上千万规模的集群,难免一个一个月会有那么几台服务器出点故 ...