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 版本。

Install-Package IdentityServer4 -Version 4.1.2

添加 IdentityServer 服务

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

定义 Client

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

public static IEnumerable<ApiScope> GetApiScopes()
        {
            return new List<ApiScope>
            {
                new ApiScope("api1", "我的 API"),
            };
        }

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

services.AddIdentityServer()
                .AddDeveloperSigningCredential()
                .AddInMemoryClients(Startup.GetClients())
                .AddInMemoryApiScopes(Startup.GetApiScopes())
                ;

注入 IdentityServer 中间件

Configure 方法中注入 IdentityServer 中间件。

app.UseRouting();

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

使用 PostMan 测试

WebApi 集成 IdentityServer4 认证中心

添加依赖包

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

IdentityServer4.AccessTokenValidation -Version 3.0.1

添加认证服务

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

services.AddAuthentication("Bearer")
                .AddJwtBearer("Bearer", o => {
                    o.Authority = "http://localhost:5000";
                    o.RequireHttpsMetadata = false;
                    o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
                    {
                        ValidateAudience = false
                    };
                })
                ;

添加认证和授权中间件

在 Configure 方法中添加中间件。

 

app.UseRouting();

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

app.UseAuthentication();

app.UseAuthorization();

创建需要授权的 Controller

[ApiController]
    [Route("[controller]")]
    [Authorize]
    public class HomeController : ControllerBase
    {
        public HomeController()
        {
        }

        [HttpGet]
        public string Get()
        {
            return "极限编程网";
        }
    }

使用 PostMan 测试

结尾

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

 

本文转载自:https://limitcode.com/detail/606d4d61d9118c3cd4168786.html

版权声明:本文版权归作者所有,遵循 CC 4.0 BY-SA 许可协议, 转载请注明原文链接
https://www.cnblogs.com/limitcode/p/14639513.html