【问题标题】:ASP.NET Core 2.0 APIs and IdentityServer4 AuthorizationASP.NET Core 2.0 API 和 IdentityServer4 授权
【发布时间】:2018-09-29 15:12:58
【问题描述】:

我们正在为外部合作伙伴开发 ASP.NET Core 2.0 中的 API。例如,保险领域有各种产品,如汽车、旅行、家庭等。

现在,我们将 IdentityServer4 配置为身份验证类型“ResourseOwnerPassword”,并将 API 授权配置为 -

服务列表:

  1. https://domain:port/Motor/api/v1/CalculatePremium
  2. https://domain:port/Motor/api/v1/ProposalSync
  3. https://domain:port/Motor/api/v1/InstaProposalSync

  4. https://domain:port/Travel/api/v1/CalculatePremium

  5. https://domain:port/Travel/api/v1/ProposalSync

授权@API:

 services.AddAuthorization(options =>
    {
        options.AddPolicy("AuthPolicy", builder =>
        {
          builder.RequireScope("MotorApi","TravelApi","PaymentApi");
        });
    });

问题: 我们如何授权指定具有所需范围的服务,意味着 Partner-A 只能访问“Motor/api/v1/CalculatePremium”,不能访问其他产品服务。

合作伙伴-B 只能访问旅游服务,不能访问其他产品服务。

请协助正确的方法来实现这一点。

提前致谢

【问题讨论】:

标签: asp.net-core-2.0 identityserver4 asp.net-authorization


【解决方案1】:

您可以在用户登录时为其分配角色并使用如下中间件:

app.Use(async (context, next) => {

    if (context.User.Identity.IsAuthenticated)
    {

        if (context.Request.Path == "/Motor/api/v1/CalculatePremium"
                    && !context.User.IsInRole("Partner-A"))
        {
            context.Response.StatusCode = 403;
        }
        else await next();
    }
    else await next();

});

如果您愿意,可以通过将中间件设置为单独的类来更简洁地设置中间件。

【讨论】:

  • 感谢您的回复,这绝对有帮助。然而,目前我们在所有产品和 5 多个合作伙伴中拥有近 70 多项服务。将来会有更多的合作伙伴或可能需要撤销访问权限。因此,从可维护性的角度来看,配置必须是临时/动态的。请建议我们如何使用 DB 或任何资源文件中的身份服务器维护此授权配置。
猜你喜欢
  • 2018-03-06
  • 2018-05-02
  • 2018-04-03
  • 1970-01-01
  • 2020-09-25
  • 2019-05-27
  • 2020-05-18
  • 1970-01-01
  • 2018-06-02
相关资源
最近更新 更多