简体   繁体   English

具有多个.net应用程序的Web API 2.0 Owin授权

[英]Web API 2.0 Owin authorization with multiple .net applications

I have two .net applications. 我有两个.net应用程序。 Both applications have WebAPI 2.O APIs using C#. 两个应用程序都使用C#的WebAPI 2.O API。

Let's say one is parent application another one is a child. 假设一个是父申请,另一个是孩子。 Parent application has Owin authentication and all APIs working as expected with Authorization. 父应用程序具有Owin身份验证,所有API均按预期使用授权。

In child application, I want to use same Authorization provider used in the parent application. 在子应用程序中,我想使用父应用程序中使用的相同授权提供程序。 I don't want to use authentication for child application again. 我不想再次为子应用程序使用身份验证。

Two things I have tried: 我尝试了两件事:

  1. Use of same machine keys in both the applications 在两个应用程序中使用相同的机器密钥

  2. Tried to create a third independent .net application which will provide authentication and authorization for both the applications. 试图创建第三个独立的.net应用程序,它将为这两个应用程序提供身份验证和授权。

First one didn't work. 第一个没用。 I am not sure how I can achieve the second one. 我不确定如何实现第二个。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks. 谢谢。

So, if I understood correctly, you want a way to authenticate a child service, based on the parent service authentication passing authentication between the services. 因此,如果我理解正确,您需要一种方法来验证子服务,基于父服务身份验证传递服务之间的身份验证。

We just need the same thing here, to authenticate the microservices behind our front service (parent service). 我们在这里只需要相同的东西来验证前端服务(父服务)背后的微服务。

We used JWT for that, using it we can solve that, because on the child services (in our case microservices) they trust the parent authentication. 我们使用JWT,使用它我们可以解决这个问题,因为在子服务(在我们的例子中是微服务)中,他们信任父身份验证。

The services work like this, the Parent Service or maybe another Authentication service creates the valid JWT to be used on the Parent Service. 服务的工作方式如下,父服务或其他身份验证服务可以创建在父服务上使用的有效JWT。

When the Parent Service, receveives the JWT they will validate everything that's need to ensure the client is corret. 当父服务收到JWT后,他们将验证确保客户端正确性所需的一切。 When the Parent Service need to call the Child Service, it'll send the same JWT, but on the Child Service the JWT will be not the same, in our case we just validate the Lifetime and Issuer Sign Key. 当父服务需要调用子服务时,它将发送相同的JWT,但在子服务上,JWT将不同,在我们的例子中,我们只是验证Lifetime和Issuer Sign Key。

We end up with a code like this on our Startup.cs file on our child services, our parent service/auth service was kept the same. 我们在我们的子服务上的Startup.cs文件中得到了这样的代码,我们的父服务/ auth服务保持不变。

public static void ConfigureAuth(IServiceCollection services)
{
    services
        .AddAuthentication(o =>
        {
            o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(o =>
        {
            o.SaveToken = true;
            o.TokenValidationParameters = new TokenValidationParameters
            {
                // Must validate the signing key
                ValidateIssuerSigningKey = true,

                // Must validate the life time
                ValidateLifetime = true,

                // The issuer may vary in a multitenant scenario,
                // that's why we not valid the issuer.
                ValidateIssuer = false,
                ValidIssuer = o.ClaimsIssuer,

                // Allowing passing a token among multiple services (audiences).
                ValidateAudience = false,
                ValidAudience = "",

                // Does not require expiration
                RequireExpirationTime = false,
                ClockSkew = TimeSpan.Zero
            };
        });
}

If you still have doubts I recommend you to look for Authentication Between Microservice, maybe that can help. 如果你仍然有疑问,我建议你寻找微服务之间的身份验证,也许这可以帮助。

Store the generated authentication token (along with user identity info if needed) from the Parent application in a secure Redis cache. 将父生应用程序中生成的身份验证令牌(以及用户身份信息,如果需要)存储在安全的Redis缓存中。

You can then get the token from subsequent requests on the Parent API's authorized endpoints, and append it on any calls to your Child API: 然后,您可以从Parent API的授权端点上的后续请求中获取令牌,并将其附加到对您的Child API的任何调用上:

public class ValuesController : ApiController
{
  [Authorize]
  public IHttpActionResult Get()
  {
    var authToken = Request.Headers.Authorization;
    // send authToken with requests to child endpoints
  }
}

Then on the Child API you can get the auth token in a similar manner, and lookup & validate it against the stored Redis tokens. 然后在Child API上,您可以以类似的方式获取身份验证令牌,并根据存储的Redis令牌查找并验证它。

Extra points if you're getting the token in middleware. 如果您在中间件中获取令牌,则需要额外的积分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对多个 Asp.net Web 应用程序的授权 - Authorization for multiple Asp.net web applications Web API / OWIN,SignalR和授权 - Web API / OWIN, SignalR & Authorization ASP.NET Web Api 2.0中的Owin自定义路由MIddleware - Owin Custom Routing MIddleware in ASP.NET Web Api 2.0 在 ASP.net Web API 中发生授权之前尝试使用 OWIN 注入授权标头 - Trying to inject Authorization header using OWIN before authorization happens in ASP.net Web API 用户界面中的OWIN Web API授权 - OWIN Web API authorization in User Interface 具有相同身份验证 Web API 的多个应用程序的授权工作流程是什么? - What would be the authorization workflow for multiple applications with the same authentication web API? 如何使用OWIN,OAuth和Web API获取授权代码? - How to get authorization code with OWIN, OAuth and Web API? ASP.NET Core 2.0 Web API Azure Ad v2令牌授权无效 - ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working 具有授权ASP.NET Core 2.0 Web API的单元测试控制器 - Unit Testing Controller with Authorization ASP.NET Core 2.0 Web API OWIN ASP.NET - 避免在Web Api中没有Identity的同一帐户进行多次登录 - OWIN ASP.NET - Avoid multiple logins for the same account without Identity in Web Api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM