具有多个合并范围的令牌只能访问单个合并范围/ Api资源

时间:2019-01-29 14:11:42

标签: c# authentication jwt identityserver4

我在使用IdentityServer4验证应用程序时遇到问题。

我有:

  • 用于对多个客户端进行身份验证的IdentityServer4-Server (在localhost:5000上运行)
  • Api,主要用于存储和检索数据 与SignalR(在localhost:5200上运行)
  • 一个Web应用程序,用于从Api读取和写入数据并具有 用户特定的交互(在localhost:80上运行)
  • 和用于从Api读取数据的桌面应用程序

有效方法:

  • 桌面用户可以使用“ ApiClient”将Api与 客户端凭据流。
  • Web用户可以在以下情况下使用“ WebClient”获取具有隐式流的令牌: 登录。

什么不起作用:

  • Web客户端无法使用来自以下位置的令牌在Api上进行身份验证 隐式流,即使他具有Api的作用域。

ApiResources:

public static IEnumerable<ApiResource> Apis = new List<ApiResource>
{
    new ApiResource("WebApplicationResource", ""),
    new ApiResource("ApiServerResource", "")
};

客户:

public static IEnumerable<Client> Clients = new List<Client>
{
    // Web Application (SPA) Client
    new Client
    {
        ClientId = "WebClient",
        AllowedGrantTypes = GrantTypes.Implicit,
        AllowAccessTokensViaBrowser = true,
        RequireConsent = false,
        RedirectUris = {
            "http://localhost:3000/callback"
        },
        PostLogoutRedirectUris = {
            "http://localhost:3000/post_logout"
        },
        AllowedCorsOrigins = {
            "http://localhost:3000"
        },
        AllowedScopes = {
            IdentityServerConstants.StandardScopes.OpenId,
            IdentityServerConstants.StandardScopes.Profile,
            "WebApplicationResource",
            "WebApiResourceScope"
        }
    },
    // Api (Signalr) Client
    new Client
    {
        ClientId = "ApiClient",
        AllowedGrantTypes = GrantTypes.ClientCredentials,
        RequireConsent = false,
        ClientSecrets = {
            new Secret("SecretCredentialsValue".Sha256())
        },
        AllowedScopes = {
            "WebApiResourceScope"
        }
    }
};

启动中的IS4服务器身份验证:

//in ConfigureServices
services.AddAuthentication("Bearer")
    .AddJwtBearer("Bearer", jwt =>
    {
        jwt.Authority = "http://localhost:5000";
        jwt.Audience = "WebApplicationResource";
        jwt.RequireHttpsMetadata = false;
    });
...
//in Configure
app.UseIdentityServer();

Api服务器在启动时添加了IS4身份验证:

services.AddAuthentication("Bearer")
    .AddIdentityServerAuthentication(options =>
    {
        options.Authority = "http://localhost:5000";
        options.RequireHttpsMetadata = false;
        options.ApiName = "WebApiResourceScope";
    });

Api Server SignalR在启动中的实现:

//in ConfigureServices
services.AddSignalR()
...
//in Configure
app.UseSignalR(routes =>
    {
        routes.MapHub<EventHub>("/api");
    });

SignalR Hub身份验证添加有:

[Authorize(AuthenticationSchemes = "Bearer")]

这是测试时的输出:

Api显示桌面客户端可以进行身份​​验证: Api showing that Desktop Client could Authenticate

成功登录后,Web客户端尝试使用其承载令牌在Api上的SignalR服务器上进行身份验证(就像客户端在顶部一样)。 Web Application not authenticating when accessing

发生错误时控制台输出: Console Output of Error

Api的输出,显示未验证承载令牌: Output of Api showing that authentication failed

我是否误解了示波器的某些内容?为什么我可以在Web Client上添加多个作用域,而只能在已声明的Api资源之一上使用它?

我必须为Web客户端获取2个令牌吗?如果是这样,我应该使用什么来获得令牌,在Web到Web的通信中不建议使用客户端凭据。

或者我不了解什么。

0 个答案:

没有答案
相关问题