如何扩展IdentityServer4工作流程以运行自定义代码

编程入门 行业动态 更新时间:2024-10-18 01:34:22
本文介绍了如何扩展IdentityServer4工作流程以运行自定义代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个基于的基本Identityserver4实现.快速入门示例.

在我的创业公司中,我有以下东西:

In my startup I have the following:

public void ConfigureServices(IServiceCollection services) { // configure identity server with in-memory stores, keys, clients and scopes services.AddIdentityServer() .AddTemporarySigningCredential() .AddInMemoryApiResources(Config.GetApiResources()) .AddInMemoryClients(Config.GetClients()); } public void Configure(IApplicationBuilder app) { ... app.UseIdentityServer(); }

我想扩展IdentityServer4工作流程,以便在生成访问令牌之后,我可以运行业务逻辑(基于访问令牌中的声明),并修改发送给主叫客户端的响应.我尝试创建.NET核心中间件,但是IdentityServer中间件似乎使其余的管道短路(将在UseIdentityServer被执行之后再没有中间件).

I want to extend the IdentityServer4 workflow so that after the access token is generated I can run business logic (based on the claims in the access token) and modify the response send to the calling client. I tried creating a .NET core middleware but it seems the IdentityServer middleware short-circuits the rest of the pipeline (no middleware place after the UseIdentityServer will be executed).

它们是否可以用来始终修改IdentityServer4发出的响应的Identityserver4中的任何扩展方法?我正在使用凭据授予.本质上,一旦IdentityServer4完成其工作流程,我想运行一些业务逻辑来修改发送给客户端的响应

Are they any extension method in Identityserver4 that I can use to always modify the response issued by IdentityServer4? I am using the credentials grant. Essentially I want to run some business logic to modify the response send to the client once IdentityServer4 is done with its workflow

推荐答案

不幸的是,没有办法做到这一点. 当您请求任何IdentityServer端点时,IdentityServer中间件会使其余的管道短路. 您可以检查源代码: IdentityServerMiddleware类.

Unfortunately, there is no way to do that. When you request any IdentityServer endpoint, IdentityServer middleware short-circuits the rest of the pipeline. You can check source code: IdentityServerMiddleware class.

我相信这样做是有原因的.但是,如果您确实需要修改响应,则至少有三个选项:

I believe it was done for a reason. But if you really need to modify the response, you have at least three options:

  • 创建一个叉子,并从中删除 return 运算符 IdentityServerMiddleware Invoke 方法(请小心地将剩余的管道短路,将 return 添加到您的上一个中间件中).
  • 创建自己的 IdentityServerMiddleware , IdentityServerApplicationBuilderExtensions 实现并使用 它们而不是默认值.
  • 将中间件放在UseIdentityServer之前.您的中间件应如下所示:

  • Create a fork and remove return operator from IdentityServerMiddleware Invoke method (be careful to short-circuit the rest of the pipeline adding return into your last middleware).
  • Create your own IdentityServerMiddleware, IdentityServerApplicationBuilderExtensions implementations and use them instead of default.
  • Place your middleware before the UseIdentityServer. Your middleware should look like this: public ResponseBodyEditorMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context) { // get the original body var body = context.Response.Body; // replace the original body with a memory stream var buffer = new MemoryStream(); context.Response.Body = buffer; // invoke the next middleware from the pipeline await _next.Invoke(context); // get the body as a string var bodyString = Encoding.UTF8.GetString(buffer.GetBuffer()); // make some changes bodyString = $"The body has been replaced!{Environment.NewLine}Original body:{Environment.NewLine}{bodyString}"; // update the memory stream var bytes = Encoding.UTF8.GetBytes(bodyString); buffer.SetLength(0); buffer.Write(bytes, 0, bytes.Length); // replace the memory stream with updated body buffer.Position = 0; await buffer.CopyToAsync(body); context.Response.Body = body; }

  • 更多推荐

    如何扩展IdentityServer4工作流程以运行自定义代码

    本文发布于:2023-11-28 13:55:19,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1642675.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:自定义   工作流程   代码

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!