三、IDS4建立authorization server
建立authorization server
一、环境搭建
1、创建项目
2、引用NuGet的identityserver4
3、配置asp.net core 管道
打开Startup.cs, 编辑Configure方法:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseIdentityServer();
}
就是使用上面这个中间件.
4、运行方式改为使用控制台运行而不是IISExpress, 以便查看各种debug信息.
把IISExpress相关的内容删掉, 然后端口改为5000.
Program.cs里的BuildWebHost也应该加上Url:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("http://0.0.0.0:5000")
.UseStartup<Startup>();
其实不加也好用.
运行就会弹出控制台:
二、配置Identity Server
还是Startup.cs,编辑ConfigureServices方法:
这里不仅要把IdentityServer注册到容器中, 还要至少对其配置三点内容:
1. 哪些API可以使用这个authorization server.
2. 那些客户端Client(应用)可以使用这个authorization server.
3. 指定可以使用authorization server授权的用户.
首先需要把上面这些做成一个配置文件:
建立Configuration/InMemoryConfiguration.cs:
namespace ids4.Configuration
{
public class InMemoryConfiguration
{
public static IEnumerable<ApiResource> ApiResources()
{
return new[]
{
new ApiResource("socialnetwork", "社交网络")
};
}
public static IEnumerable<Client> Clients()
{
return new[]
{
new Client
{
ClientId = "socialnetwork",
ClientSecrets = new [] { new Secret("secret".Sha256()) },
AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
AllowedScopes = new [] { "socialnetwork" }
}
};
}
public static IEnumerable<TestUser> Users()
{
return new[]
{
new TestUser
{
SubjectId = "",
Username = "mail@qq.com",
Password = "password"
}
};
}
}
}
ApiResources: 这里指定了name和display name, 以后api使用authorization server的时候, 这个name一定要一致, 否则就不好用的.
Clients: Client的属性太多了, 这里就指定几个. 其中ClientSecrets是Client用来获取token用的. AllowedGrantType: 这里使用的是通过用户名密码和ClientCredentials来换取token的方式. ClientCredentials允许Client只使用ClientSecrets来获取token. 这比较适合那种没有用户参与的api动作. AllowedScopes: 这里只用socialnetwork
Users: 这里的内存用户的类型是TestUser, 只适合学习和测试使用, 实际生产环境中还是需要使用数据库来存储用户信息的, 例如接下来会使用asp.net core identity. TestUser的SubjectId是唯一标识.
然后回到StartUp的ConfigureServices:
前一篇文章讲过, 我们需要对token进行签名, 这意味着identity server需要一对public和private key. 幸运的是, 我们可以告诉identity server在程序的运行时候对这项工作进行设定: AddDeveloperSigningCredential(), 它默认会存到硬盘上的, 所以每次重启服务不会破坏开发时的数据同步. 这个方法只适合用于identity server4在单个机器运行, 如果是production farm你得使用AddSigningCredential()这个方法.
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddTestUsers(InMemoryConfiguration.Users().ToList())
.AddInMemoryClients(InMemoryConfiguration.Clients())
.AddInMemoryApiResources(InMemoryConfiguration.ApiResources());
}
注意-StartUp.cs总配置总计就两处:
然后运行一下:
没报错, 红线部分是内存配置版的一些解释.
此时要访问 http://localhost:5000/.well-known/openid-configuration
三、获取Token,打开postman
访问http://localhost:5000/connect/token
1、发送一个正确的数据:
client_id socialnetwork
client_secret secret
grant_type client_credentials
username main@qq.com
password password
GIf
2、由于identity server我们设置的是 ResourceOwnerPasswordAndClientCredentials 这个GrantType, 所以使用用户名密码以及使用ClientCredentials都可以. 那我们把用户名和密码去掉, 只发送Client Credentials:
仍然获取到了token. 控制台上的信息与上一个稍有不同, 没有user相关的信息了:
3、然后我们发送一个错误的client_id, 然后得到的结果是: invalid_client. 控制台的信息如下:
三、IDS4建立authorization server的更多相关文章
- 四、IDS4建立Authorization server和Client
一.准备 创建一个名为QuickstartIdentityServer的ASP.NET Core Web 空项目(asp.net core 2.2),端口5000创建一个名为Api的ASP.NET C ...
- 使用Identity Server 4建立Authorization Server (1)
预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 本文内容基本完全来自于Identity Server 4官方文档: https://identitys ...
- 从头编写asp.net core 2.0 web api 基础框架 (5) + 使用Identity Server 4建立Authorization Server (7) 可运行前后台源码
前台使用angular 5, 后台是asp.net core 2.0 web api + identity server 4. 从头编写asp.net core 2.0 web api 基础框架: 第 ...
- 使用Identity Server 4建立Authorization Server (4)
预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 第一部分: http://www.cnblogs.com/cgzl/p/7780559.html 第二 ...
- ASP.NET Core3.1使用Identity Server4建立Authorization Server
前言 网上关于Identity Server4的资料有挺多的,之前是一直看杨旭老师的,最近项目中有使用到,在使用.NET Core3.1的时候有一些不同.所以在此记录一下. 预备知识: https:/ ...
- 使用Identity Server 4建立Authorization Server
使用Identity Server 4建立Authorization Server (6) - js(angular5) 客户端 摘要: 预备知识: http://www.cnblogs.com/cg ...
- 使用Identity Server 4建立Authorization Server (2)
第一部分: http://www.cnblogs.com/cgzl/p/7780559.html 第一部分主要是建立了一个简单的Identity Server. 接下来继续: 建立Web Api项目 ...
- 使用Identity Server 4建立Authorization Server (5)
预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 第一部分: http://www.cnblogs.com/cgzl/p/7780559.html 第二 ...
- 使用Identity Server 4建立Authorization Server (6) - js(angular5) 客户端
预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 第一部分: http://www.cnblogs.com/cgzl/p/7780559.html 第二 ...
随机推荐
- CentOS 6.5之SSH 免密码登录
0.说明 这里为了方便说明问题,假设有A和B两台安装了centos6.5的主机.目标是实现A.B两台主机分别能够通过ssh免密码登录到对方主机.不同主机的配置过程一样,这里介绍A主机的配置过程. 事先 ...
- C++ 从txt文本中读取map
由于存入文本文件的内容都为文本格式,所以在读取内容时需要将文本格式的内容遍历到map内存中,因此在读取时需要将文本进行切分(切分成key和value) 环境gcc #include<iostre ...
- 前端每日实战:65# 视频演示如何用纯 CSS 创作一个摇摇晃晃的 loader
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览.https://codepen.io/comehope/pen/oyJvpe 可交互视频 此视频是可以 ...
- docker:docker的基本了解
1.什么是docker 简单的理解:docker相当于vmvare,容器相当于多个虚拟机,vmvare上可以运行ubantu16.04的虚拟机,也可以运行centos虚拟机,还可以运行redhat虚拟 ...
- onkeyup的使用(将输入值为非数字的字符替换为空)
onkeyup:当输入值的键盘抬起时触发这个事件. 例如: onkeyup="this.value=this.value.replace(/\D/g,'') 这是个正则式验证,用来验证输入值 ...
- iOS项目开发中的知识点与问题收集整理①
前言部分 注:本文并非绝对原创 大部分内容摘自 http://blog.csdn.net/hengshujiyi/article/details/20943045 文中有些方法可能已过时并不适用于现在 ...
- SQL笔试
一.题目 二.练习数据 SELECT VERSION(); create table CDM_AGENT(ano VARCHAR(6),aname VARCHAR(8)); create table ...
- bootstrap 点击模态框上的提交按钮后,模态框不能关闭的解决办法
项目问题如下图, 点击确定后,模态框没反应,按理,点击删除按钮时,弹出确认删除的模态框,点击确定后,使用ajax请求服务器,把数据库中对应的数据进行删除,根据服务器 servlet返回的状态值(del ...
- webservice文件上传下载(byte[] 实现方式)
测试环境:axis2-1.6.1.6.0.20.jdk1.5 说明:本方式仅适用于文件小于10M的场景(否则会出现内存溢出),大文件的上传下载应另选其他方式. 1.创建要发布成webservice的j ...
- opencc模块用langconv替换
将一下两个py下载并放入代码目录:https://raw.githubusercontent.com/skydark/nstools/master/zhtools/langconv.py https: ...