IdentityServer4实战:快速入门

项目结构

首先创建3个项目,这3个项目将做为我们学习 IdentityServer4 的基础项目,项目框架全部使用 .NET CORE 3.1。

 

端口约定如下:

MicroShell.IdentityServer4.Server : 5000

MicroShell.IdentityServer4.Api : 5001

MicroShell.IdentityServer4.Mvc : 5002

搭建 IdentityServer4 认证中心

添加依赖包

在项目 MicroShell.IdentityServer4.Server 中添加 Nuget 包:IdentityServer4,笔者使用的是 4.1.2 版本。

1

Install-Package IdentityServer4 -Version 4.1.2

添加 IdentityServer 服务

ConfigureServices 方法中添加 IdentityServer 的依赖服务。

定义 Client

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public static IEnumerable<Client> GetClients()

        {

            return new List<Client>

                {

                    new Client

                    {

                        ClientId = "client1",

 

                        // 没有交互性用户,使用 clientid/secret 实现认证。

                        AllowedGrantTypes = GrantTypes.ClientCredentials,

 

                        // 用于认证的密码

                        ClientSecrets =

                        {

                            new Secret("secret".Sha256())

                        },

                        // 客户端有权访问的范围(Scopes)

                        AllowedScopes = { "api1" }

                    }

                };

        }

定义 ApiScope

1

2

3

4

5

6

7

public static IEnumerable<ApiScope> GetApiScopes()

        {

            return new List<ApiScope>

            {

                new ApiScope("api1""我的 API"),

            };

        }

做为快速入门篇,遵循一切从简的原则, Client 、签名都使用内存持久化模型,数据库持久化我们放到后面去讲。 准备好 Client 和 ApiScope 后开始注入它们的依赖服务。

1

2

3

4

5

services.AddIdentityServer()

                .AddDeveloperSigningCredential()

                .AddInMemoryClients(Startup.GetClients())

                .AddInMemoryApiScopes(Startup.GetApiScopes())

                ;

注入 IdentityServer 中间件

Configure 方法中注入 IdentityServer 中间件。

1

2

3

app.UseRouting();

 

app.UseIdentityServer(); // 要放在  UseRouting 的后面

使用 PostMan 测试

WebApi 集成 IdentityServer4 认证中心

添加依赖包

在项目 MicroShell.IdentityServer4.Api 中添加 Nuget 包:IdentityServer4.AccessTokenValidation,笔者使用的是 3.0.1 版本。

1

IdentityServer4.AccessTokenValidation -Version 3.0.1

添加认证服务

在 ConfigureServices 方法中添加认证服务。

1

2

3

4

5

6

7

8

9

10

services.AddAuthentication("Bearer")

                .AddJwtBearer("Bearer", o => {

                    o.Authority = "http://localhost:5000";

                    o.RequireHttpsMetadata = false;

                    o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()

                    {

                        ValidateAudience = false

                    };

                })

                ;

添加认证和授权中间件

在 Configure 方法中添加中间件。

 

1

2

3

4

5

6

7

app.UseRouting();

 

// 认证和授权中间件要放到路由中间后面

 

app.UseAuthentication();

 

app.UseAuthorization();

创建需要授权的 Controller

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

[ApiController]

    [Route("[controller]")]

    [Authorize]

    public class HomeController : ControllerBase

    {

        public HomeController()

        {

        }

 

        [HttpGet]

        public string Get()

        {

            return "极限编程网";

        }

    }

使用 PostMan 测试

结尾

入门篇我们使用的 Client 是 ClientCredentials(客户端凭证模式),该模式因为没有用户参与,不适合 MVC 项目场景,所以笔者省略了 MVC 项目下的集成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值