【发布时间】:2018-07-19 13:01:52
【问题描述】:
我希望有人能指出我正确的方向。
我试图让 javascript 客户端在引用令牌的帮助下与 api 进行通信。我正在使用身份服务器 4。
一切正常:
登录时,javascript 客户端/webapplicatoin 被路由到身份服务器以进行用户和密码验证 成功后,用户将被路由回 javascript/webapplication 并拥有有效的 IdentityToken
出了什么问题:
当 javascript 客户端/web 应用程序调用我的 Api 时,该 api 会收到请求。然后api想用Identity Server检查接收到的身份令牌以获取访问令牌,但无法联系服务器并在控制台中出现以下错误:
fail: IdentityServer4.Validation.ApiSecretValidator[0] API validation failed. fail: IdentityServer4.Endpoints.IntrospectionEndpoint[0] API unauthorized to call introspection endpoint. aborting.
很清楚 Api 不允许与 te 服务器通信...
我想我在某个地方有配置错误,但我就是看不到在哪里。尝试了各种不同的设置,我没有尝试或改变的想法......
这是我到目前为止所拥有的:
对于 javascript 客户端:
我使用oidc-redux包,我使用的配置:
var userManagerConfig = {
authority: "http://localhost:50289",
redirect_uri: "http://localhost:3000/callback",
client_id : "js",
response_type : "id_token",
scope :"openid",
};
Identity Server 4 配置
客户端定义:
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "js",
ClientName = "my js client",
AllowedGrantTypes = GrantTypes.Implicit,
RequireClientSecret = false,
RequireConsent = false,
RedirectUris = { "http://localhost:3000/callback" },
PostLogoutRedirectUris = { "http://localhost:3000" },
AllowAccessTokensViaBrowser = false,
AllowedCorsOrigins = { "http://localhost:3000" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"myApi"
}
}
};
}
API 资源定义:
public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("myApi")
{
ApiSecrets =
{
new Secret("superweaksecret")
}
}
};
}
在配置服务中,我将它们全部添加到容器中:
// configure identity server with in-memory stores, keys, clients and scopes
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddProfileService<CustomClaimService>()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<ApplicationUser>();
API 设置
基于 .net Core,在 ConfigureService 方法中我有以下内容:
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:50289";
options.RequireHttpsMetadata = false;
options.ApiSecret = "superweaksecret";
options.ApiName = "myApi";
options.EnableCaching = false;
});
希望有人看到我的错误。
如果我错过了合适的遮阳篷所需的信息或代码,请告诉我。
亲切的问候, 罗尔
【问题讨论】:
-
我不明白这个问题。根据您的描述(和代码),您的 api 应该充当受保护的资源而不是客户端。在这种情况下,它不应该检索令牌。它应该需要它们。如果我误解了什么,请纠正我。
-
设置由三个部分组成,javascript 客户端、Identity Server 和 Api。 javascript 客户端和身份服务器按应有的方式进行通信。这意味着,从我的 web 应用程序中,我被路由到 Identity Server 进行登录,并且在成功登录后我有一个 IdentityToken。然后,javascript/webapp 客户端请求 api 上的端点。然后发生错误,它发生在 api 尝试验证/检索身份服务器上的客户端身份验证所需的信息时。 Api 本身不允许与 Identity Server 通信
-
好的,你什么时候收到这个错误?
-
我还更新了原始问题,试图更清楚一点...
-
你是故意要这样吗?因为我看到您只从 IDS 请求 id_token (response_type : "id_token",)。你为什么不请求 access_token (response_type : "id_token token")?然后你只需要在调用api时发送access_token就可以了。或者,如果您有其他想法……?
标签: javascript api jwt identityserver4 openid-connect