【问题标题】:How to determine which client was used in IdentityServer4如何确定 IdentityServer4 中使用了哪个客户端
【发布时间】:2018-06-22 02:29:53
【问题描述】:

我按照快速入门实现了大部分 IdentityServer4 和 .NET Core 2,并且运行良好。 但是,我希望将访问与应用程序和在同一 Web 上运行的 API 分开。我有一个应用程序和一个 API 在同一个 Web 上运行,我想在应用程序还需要登录的同时提供对这个 API 的安全访问。

所以在网站上,我希望用户能够访问应用程序以及 API。 但是对于应该只访问 API 的外部应用程序,我希望他们不能访问应用程序的其余部分。

我知道我可以通过使用 2 个不同的 Web 来做到这一点:一个用于应用程序,一个用于 API,但我希望它们都在同一个 URL 上。

所以我想我只需要 2 个不同的客户端连接到同一个用户数据库(使用 ASP.NET 身份):

 new Client
 {
     ClientId = "sapi",
     ClientName = "Secure API",
     AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

     RequireConsent = true,

     ClientSecrets =
     {
         new Secret("secret".Sha256())
     },
     RedirectUris = { "http://localhost:5002/signin-oidc" },
     PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

     AllowedScopes = { "sapi" }
 },

 // OpenID Connect hybrid flow and client credentials client (MVC)
 new Client
 {
     ClientId = "mvc",
     ClientName = "MVC Client",
     AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

     RequireConsent = false,

     ClientSecrets =
     {
         new Secret("secret".Sha256())
     },

     RedirectUris = { "http://localhost:5002/signin-oidc" },
     PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

     AllowedScopes =
     {
         IdentityServerConstants.StandardScopes.OpenId,
         IdentityServerConstants.StandardScopes.Profile,
         "sapi"
     },
     AllowOfflineAccess = true
 }

所以想法是客户端mvc可以访问应用程序和API,而sapi客户端只能访问API而不能访问应用程序。任何用户(存储在 ASP.NET 标识中)都可以与客户端 mvcsapi 一起使用。

问题是我如何判断用户是使用客户端 mvc 还是使用客户端 sapi 进行了身份验证?

【问题讨论】:

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


    【解决方案1】:

    当用户登录后,在声明中您将有一个名为aud 的声明。它的值应该是clientId。然后,您可以有一些自定义授权来拒绝或允许对应用程序的授权。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多