• <bdo id='fHRxx'></bdo><ul id='fHRxx'></ul>
    <legend id='fHRxx'><style id='fHRxx'><dir id='fHRxx'><q id='fHRxx'></q></dir></style></legend>

      <tfoot id='fHRxx'></tfoot>

      <small id='fHRxx'></small><noframes id='fHRxx'>

      <i id='fHRxx'><tr id='fHRxx'><dt id='fHRxx'><q id='fHRxx'><span id='fHRxx'><b id='fHRxx'><form id='fHRxx'><ins id='fHRxx'></ins><ul id='fHRxx'></ul><sub id='fHRxx'></sub></form><legend id='fHRxx'></legend><bdo id='fHRxx'><pre id='fHRxx'><center id='fHRxx'></center></pre></bdo></b><th id='fHRxx'></th></span></q></dt></tr></i><div id='fHRxx'><tfoot id='fHRxx'></tfoot><dl id='fHRxx'><fieldset id='fHRxx'></fieldset></dl></div>
    1. 使用 Asp.net 核心创建另一个 Web api 的代理

      时间:2023-06-08

          1. <i id='UxBvq'><tr id='UxBvq'><dt id='UxBvq'><q id='UxBvq'><span id='UxBvq'><b id='UxBvq'><form id='UxBvq'><ins id='UxBvq'></ins><ul id='UxBvq'></ul><sub id='UxBvq'></sub></form><legend id='UxBvq'></legend><bdo id='UxBvq'><pre id='UxBvq'><center id='UxBvq'></center></pre></bdo></b><th id='UxBvq'></th></span></q></dt></tr></i><div id='UxBvq'><tfoot id='UxBvq'></tfoot><dl id='UxBvq'><fieldset id='UxBvq'></fieldset></dl></div>
          2. <legend id='UxBvq'><style id='UxBvq'><dir id='UxBvq'><q id='UxBvq'></q></dir></style></legend>

            <small id='UxBvq'></small><noframes id='UxBvq'>

              <tbody id='UxBvq'></tbody>
              <tfoot id='UxBvq'></tfoot>
                <bdo id='UxBvq'></bdo><ul id='UxBvq'></ul>
                本文介绍了使用 Asp.net 核心创建另一个 Web api 的代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在开发一个 ASP.Net Core Web 应用程序,我需要为另一个(外部)Web 服务创建一种身份验证代理".

                I'm developing an ASP.Net Core web application where I need to create a kind of "authentication proxy" to another (external) web service.

                我所说的身份验证代理的意思是,我将通过我的 Web 应用程序的特定路径接收请求,并且必须检查这些请求的标头以获取我之前发布的身份验证令牌,然后重定向所有向外部 Web API 发出具有相同请求字符串/内容的请求,我的应用将通过 HTTP Basic auth 对其进行身份验证.

                What I mean by authentication proxy is that I will receive requests through a specific path of my web app and will have to check the headers of those requests for an authentication token that I'll have issued earlier, and then redirect all the requests with the same request string / content to an external web API which my app will authenticate with through HTTP Basic auth.

                这是整个过程的伪代码

                • 客户通过向我之前发送给他的唯一 URL 发送 POST 请求令牌
                • 我的应用会向他发送一个唯一令牌以响应此 POST
                • 客户端向我的应用程序的特定 URL 发出 GET 请求,例如 /extapi 并在 HTTP 标头中添加 auth-token
                • 我的应用收到请求,检查身份验证令牌是否存在且有效
                • 我的应用向外部 Web API 发出相同的请求,并使用 BASIC 身份验证对请求进行身份验证
                • 我的应用从请求中接收结果并将其发送回客户端
                • Client requests a token by making a POST to a unique URL that I sent him earlier
                • My app sends him a unique token in response to this POST
                • Client makes a GET request to a specific URL of my app, say /extapi and adds the auth-token in the HTTP header
                • My app gets the request, checks that the auth-token is present and valid
                • My app does the same request to the external web API and authenticates the request using BASIC authentication
                • My app receives the result from the request and sends it back to the client

                这就是我现在所拥有的.它似乎工作正常,但我想知道这是否真的应该这样做,或者是否没有更优雅或更好的解决方案?从长远来看,该解决方案是否会为扩展应用程序带来问题?

                Here's what I have for now. It seems to be working fine, but I'm wondering if it's really the way this should be done or if there isn't a more elegant or better solution to this? Could that solution create issues in the long run for scaling the application?

                [HttpGet]
                public async Task GetStatement()
                {
                    //TODO check for token presence and reject if issue
                
                    var queryString = Request.QueryString;
                    var response = await _httpClient.GetAsync(queryString.Value);
                    var content = await response.Content.ReadAsStringAsync();
                
                    Response.StatusCode = (int)response.StatusCode;
                    Response.ContentType = response.Content.Headers.ContentType.ToString();
                    Response.ContentLength = response.Content.Headers.ContentLength;
                
                    await Response.WriteAsync(content);
                }
                
                [HttpPost]
                public async Task PostStatement()
                {
                    using (var streamContent = new StreamContent(Request.Body))
                    {
                        //TODO check for token presence and reject if issue
                
                        var response = await _httpClient.PostAsync(string.Empty, streamContent);
                        var content = await response.Content.ReadAsStringAsync();
                
                        Response.StatusCode = (int)response.StatusCode;
                
                        Response.ContentType = response.Content.Headers.ContentType?.ToString();
                        Response.ContentLength = response.Content.Headers.ContentLength;
                
                        await Response.WriteAsync(content);
                    }
                }
                

                _httpClient 是一个在其他地方实例化的 HttpClient 类,并且是一个单例,并且具有 http://someexternalapp 的 BaseAddress.com/api/

                _httpClient being a HttpClient class instantiated somewhere else and being a singleton and with a BaseAddressof http://someexternalapp.com/api/

                另外,是否有比手动创建更简单的令牌创建/令牌检查方法?

                Also, is there a simpler approach for the token creation / token check than doing it manually?

                推荐答案

                我最终实现了一个受 a 启发的代理中间件Asp.Net 的 GitHub 中的项目.

                I ended up implementing a proxy middleware inspired by a project in Asp.Net's GitHub.

                它基本上实现了一个中间件,它读取收到的请求,从中创建一个副本并将其发送回配置的服务,从服务读取响应并将其发送回调用者.

                It basically implements a middleware that reads the request received, creates a copy from it and sends it back to a configured service, reads the response from the service and sends it back to the caller.

                这篇关于使用 Asp.net 核心创建另一个 Web api 的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何访问 ASP.NET Core 中任何类的配置? 下一篇:MVC 6 Controller 中的 ControllerContext 和 ViewEngines 属性在哪里?

                相关文章

                1. <small id='TyymH'></small><noframes id='TyymH'>

                    <bdo id='TyymH'></bdo><ul id='TyymH'></ul>

                  1. <tfoot id='TyymH'></tfoot>

                    <i id='TyymH'><tr id='TyymH'><dt id='TyymH'><q id='TyymH'><span id='TyymH'><b id='TyymH'><form id='TyymH'><ins id='TyymH'></ins><ul id='TyymH'></ul><sub id='TyymH'></sub></form><legend id='TyymH'></legend><bdo id='TyymH'><pre id='TyymH'><center id='TyymH'></center></pre></bdo></b><th id='TyymH'></th></span></q></dt></tr></i><div id='TyymH'><tfoot id='TyymH'></tfoot><dl id='TyymH'><fieldset id='TyymH'></fieldset></dl></div>
                    <legend id='TyymH'><style id='TyymH'><dir id='TyymH'><q id='TyymH'></q></dir></style></legend>