建立authorization server

一、环境搭建

1、创建项目

2、引用NuGet的identityserver4

3、配置asp.net core 管道

打开Startup.cs, 编辑Configure方法:

  1. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  2. {
  3. app.UseDeveloperExceptionPage();
  4. app.UseIdentityServer();
  5. }

就是使用上面这个中间件.

4、运行方式改为使用控制台运行而不是IISExpress, 以便查看各种debug信息.

把IISExpress相关的内容删掉, 然后端口改为5000.

Program.cs里的BuildWebHost也应该加上Url:

  1. public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
  2. WebHost.CreateDefaultBuilder(args)
  3. .UseUrls("http://0.0.0.0:5000")
  4. .UseStartup<Startup>();

其实不加也好用.

运行就会弹出控制台:

二、配置Identity Server

还是Startup.cs,编辑ConfigureServices方法:

这里不仅要把IdentityServer注册到容器中, 还要至少对其配置三点内容:

1. 哪些API可以使用这个authorization server.

2. 那些客户端Client(应用)可以使用这个authorization server.

3. 指定可以使用authorization server授权的用户.

首先需要把上面这些做成一个配置文件:

建立Configuration/InMemoryConfiguration.cs:

  1. namespace ids4.Configuration
  2. {
  3. public class InMemoryConfiguration
  4. {
  5. public static IEnumerable<ApiResource> ApiResources()
  6. {
  7. return new[]
  8. {
  9. new ApiResource("socialnetwork", "社交网络")
  10. };
  11. }
  12. public static IEnumerable<Client> Clients()
  13. {
  14. return new[]
  15. {
  16. new Client
  17. {
  18. ClientId = "socialnetwork",
  19. ClientSecrets = new [] { new Secret("secret".Sha256()) },
  20. AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
  21. AllowedScopes = new [] { "socialnetwork" }
  22. }
  23. };
  24. }
  25. public static IEnumerable<TestUser> Users()
  26. {
  27. return new[]
  28. {
  29. new TestUser
  30. {
  31. SubjectId = "",
  32. Username = "mail@qq.com",
  33. Password = "password"
  34. }
  35. };
  36. }
  37. }
  38. }

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()这个方法.

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddIdentityServer()
  4. .AddDeveloperSigningCredential()
  5. .AddTestUsers(InMemoryConfiguration.Users().ToList())
  6. .AddInMemoryClients(InMemoryConfiguration.Clients())
  7. .AddInMemoryApiResources(InMemoryConfiguration.ApiResources());
  8. }

注意-StartUp.cs总配置总计就两处:

然后运行一下:

没报错, 红线部分是内存配置版的一些解释.

此时要访问 http://localhost:5000/.well-known/openid-configuration

三、获取Token,打开postman

 访问http://localhost:5000/connect/token

1、发送一个正确的数据:

  1. client_id socialnetwork
  2. client_secret secret
  3. grant_type client_credentials
  4. username main@qq.com
  5. password password

GIf

2、由于identity server我们设置的是 ResourceOwnerPasswordAndClientCredentials 这个GrantType, 所以使用用户名密码以及使用ClientCredentials都可以. 那我们把用户名和密码去掉, 只发送Client Credentials:

仍然获取到了token. 控制台上的信息与上一个稍有不同, 没有user相关的信息了:

3、然后我们发送一个错误的client_id, 然后得到的结果是: invalid_client. 控制台的信息如下:

三、IDS4建立authorization server的更多相关文章

  1. 四、IDS4建立Authorization server和Client

    一.准备 创建一个名为QuickstartIdentityServer的ASP.NET Core Web 空项目(asp.net core 2.2),端口5000创建一个名为Api的ASP.NET C ...

  2. 使用Identity Server 4建立Authorization Server (1)

    预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 本文内容基本完全来自于Identity Server 4官方文档: https://identitys ...

  3. 从头编写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 基础框架: 第 ...

  4. 使用Identity Server 4建立Authorization Server (4)

    预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 第一部分: http://www.cnblogs.com/cgzl/p/7780559.html 第二 ...

  5. ASP.NET Core3.1使用Identity Server4建立Authorization Server

    前言 网上关于Identity Server4的资料有挺多的,之前是一直看杨旭老师的,最近项目中有使用到,在使用.NET Core3.1的时候有一些不同.所以在此记录一下. 预备知识: https:/ ...

  6. 使用Identity Server 4建立Authorization Server

    使用Identity Server 4建立Authorization Server (6) - js(angular5) 客户端 摘要: 预备知识: http://www.cnblogs.com/cg ...

  7. 使用Identity Server 4建立Authorization Server (2)

    第一部分: http://www.cnblogs.com/cgzl/p/7780559.html 第一部分主要是建立了一个简单的Identity Server. 接下来继续: 建立Web Api项目 ...

  8. 使用Identity Server 4建立Authorization Server (5)

    预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 第一部分: http://www.cnblogs.com/cgzl/p/7780559.html 第二 ...

  9. 使用Identity Server 4建立Authorization Server (6) - js(angular5) 客户端

    预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 第一部分: http://www.cnblogs.com/cgzl/p/7780559.html 第二 ...

随机推荐

  1. windows linux子系统(Windows Subsystem for Linux)的存放目录

    win10子系统把windows的底层接口做了个转换到Linux从而能运行linux,但是他在安装的时候并没有提供安装位置的选项.(还有hyper v) 现在,所有从商店安装的发行版都存在于以下目录中 ...

  2. 【LeetCode】智商题 brainteaser(共3题)

    [292]Nim Game [319]Bulb Switcher [777]Swap Adjacent in LR String (2019年2月13日,谷歌tag) 给了两个字符串start 和en ...

  3. Linux --忘记root密码/su: Authentication failure

    如果忘记了root用户的密码,或者su root的时候,提示:su: Authentication failure 那么,可以通过以下的方式来重新设置密码,而后,再尝试,那么就可以顺利su root了 ...

  4. ElasticSearch1.7 java api

    package cn.xdf.wlyy.solr.utils; import java.util.ArrayList;import java.util.HashMap;import java.util ...

  5. CF883J 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest - J. Renovation 贪心+树状数组

    首先对于一个月的预算,如果非常小的话,我们可以留到后面的 \(a_i\) 最大的月来用,因为 \(a_i\) 越大能够拆建筑的越多. 于是我们把 \(a_i\) 合并给 \(i\) 后面的 \(a\) ...

  6. Halo(十一)

    Spring Boot 继承 AbstractErrorController 实现全局异常处理 @RequestMapping("${server.error.path:${error.pa ...

  7. [原创] Delphi InputBox、InputQuery函数

    Delphi InputBox.InputQuery函数 两个函数都是弹框提示输入信息 function InputQuery(const ACaption, APrompt: string; var ...

  8. 大量TIME_WAIT连接的解决办法

    1.使用keep alive连接(待补充) 2.修改tcp参数 根据TCP协议的连接断开规定,发起socket主动关闭的一方,socket将进入TIME_WAIT状态,TIME_WAIT状态将持续2个 ...

  9. nofollow标签的作用 nofollow标签添加方法

    这篇文章主要介绍了nofollow标签的作用 nofollow标签添加方法,需要的朋友可以参考下   nofollow标签的作用 nofollow标签添加方法  模拟搜狗蜘蛛   nofollow标签 ...

  10. c# 如何获取系统管理员权限(UAC) 及判断当前是否是管理员权限

    环境说明: VS2012,windows 7  亲自验证过win7 和xp ,XP直接不弹框,因为XP没有UAC控制机制 步骤1: 右键项目-->属性-->安全性-->选中[启用Cl ...