【问题标题】:IdentityServer4 multiple projectsIdentityServer4 多个项目
【发布时间】:2019-02-28 05:08:31
【问题描述】:

我们的解决方案有 3 个项目。我们称它们为 A、B 和 IdentityServer。 每个项目都可以作为单独的服务工作。 项目 A 和 B 是 ASP.Net MVC 项目 IdentityServer 是 .Net Core(它使用 IdentityServer4)

前端连接到项目 A。 数据库连接到项目 B。 项目 A 的控制器调用项目 B 的控制器的方法。 而且效果很好。

我们为项目 A 添加了授权,配置了 IdentityServer,一切正常。 当前端从项目A控制器中调用methode时:

[授权] 公共任务方法(...) {}

它工作正常。它将用户重定向到 IdentityServer,请求登录并授权他。

但我们也必须保护项目 B。 当我将 [Authorize] pragma 添加到项目 B 的方法时,当项目 A 的控制器尝试调用该方法时,它会失败。

[Authorize]
public Task<ActionResult> MethodefromServiceA()
{
//do something
//calling methode from service B and sending User as ClaimsPrincipal
}


[Authorize]
public Task<ActionResult> MethodefromServiceB()
{
//it fails here - don't enter to this line (it enter's here when I remove [Authorise] pragma
}

我们不确切知道如何配置 IdentityServer4 和项目 B,当项目 B 控制器从项目 A 获取声明和令牌时,它应该以某种方式连接 IdentityServer 并授权令牌(但不是用户,因为他已经被授权并且我们有令牌)。

在 IdentityServer 的 Configuration.cs 中,我们有这样的内容:

             new Client {
                ClientId = "A",
                ClientName = "A",
                AllowedGrantTypes = GrantTypes.Implicit,
                AllowedScopes = new List<string>
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    IdentityServerConstants.StandardScopes.Email,
                    "role",
                    "customAPI.write",
                    "customAPI.read"
                },
                RedirectUris = new List<string> {"http://localhost:64898"},
                PostLogoutRedirectUris = new List<string> { "http://localhost:64898/home/welcome" }
            },
              new Client {
                ClientId = "B",
                ClientName = "B",
                AllowedGrantTypes = GrantTypes.Implicit,
                AllowedScopes = new List<string>
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    IdentityServerConstants.StandardScopes.Email,
                    "role",
                    "customAPI.write",
                    "customAPI.read"
                },
                RedirectUris = new List<string> {"http://localhost:57028"},
                PostLogoutRedirectUris = new List<string> { "http://localhost:57028" }
            },

在项目 A 的启动中:

        app.UseCookieAuthentication(new CookieAuthenticationOptions

        {
            AuthenticationType = "Cookies"
        });

        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            ClientId = "A",
            Authority = "https://localhost:44350/",
            RedirectUri = "http://localhost:64898",
            ResponseType = "id_token",
            Scope = "openid email",

            UseTokenLifetime = false,
            SignInAsAuthenticationType = "Cookies",
            PostLogoutRedirectUri = "http://localhost:64898/home/welcome",
        });

我尝试添加类似于项目 B,但它没有改变任何东西。

如果我能找到一些如何使用 IdentityServer4 进行多项目授权的示例,我会很高兴。如何在每个项目中授权令牌等... [Authorize] pragma 是否解决了这样的情况,还是必须以不同的方式或手动完成?

感谢您的帮助。 对不起我的英语

【问题讨论】:

  • 我认为您可以改进您的问题,添加以下代码:“来自项目 A 的控制器调用来自项目 B 控制器的方法”,因为您只放置了 cmets 并添加了以下代码中使用的代码:“我尝试添加类似于项目 B”,因为您只放置了项目 A 中的启动代码。

标签: c# .net oauth-2.0 identityserver4


【解决方案1】:

您需要将访问令牌与您从项目 A 发出的请求一起传递给项目 B。

HttpClient client = new HttpClient();
client.SetBearerToken(bearerTokenString);
client.SendAsync("http://projectB/MehthodB");

项目 A 和 B 的两个后端都应该有一个令牌验证块来验证传入的令牌,就像您对项目 A 所做的那样。

现在您可以重复使用相同的不记名令牌来调用下一个服务,但如果您想要该服务的特定令牌,则必须从 IdentityServer 请求一个。

您可以从具有客户端凭据流或证书的服务请求新令牌,以识别项目 A 并为项目 B 请求访问令牌。

【讨论】:

  • 这可能是解决方案,但我不知道从哪里获取 BearerToken。我有 ClaimsPrincipal 和何时: var accessToken = _principal.FindFirst("access_token"); accessToken 为空。
  • 我使用 nuget 包 IdentityServer3.AccessTokenValidation。它在配置选项 PreserveAccessToken = true 中有一个选项,这将 access_token 作为“令牌”声明添加到您对 ClaimsPrinciple 的声明列表中!
【解决方案2】:

我的想法是将项目 B 作为资源,将项目 A 作为资源和客户端添加到身份服务器,以便项目 A 可以使用身份服务器生成的单独令牌访问项目 B 的资源。只是一个建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-09
    • 2018-08-21
    • 2017-01-28
    • 2018-12-06
    • 2020-01-11
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    相关资源
    最近更新 更多