c# - Identity Server 4 - 未经授权的客户端
问题描述
我正在努力使用 Net Core 3.0 和 React 进行 Identity Server 4 的基本设置(但这几乎无关紧要)。
我已经生成了新的应用程序dotnet new react -au Individual
,更新了依赖项等,创建的配置基本上是从演示服务器复制的,内容如下:
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
// JavaScript Client
new Client
{
Enabled = true,
ClientId = "spa",
ClientName = "SPA (Code + PKCE)",
RequireClientSecret = false,
RequireConsent = false,
RedirectUris = { "https://notused" },
PostLogoutRedirectUris = { "https://notused" },
AllowedGrantTypes = GrantTypes.Code,
AllowedScopes = { "openid", "profile", "email", "api" },
AllowOfflineAccess = true,
RefreshTokenUsage = TokenUsage.ReUse
},
};
}
在我的启动中:
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer(o =>
{
o.UserInteraction.ErrorUrl = "/myErrorsHandler";
o.Events.RaiseErrorEvents = true;
o.Events.RaiseFailureEvents = true;
o.Events.RaiseInformationEvents = true;
o.Events.RaiseSuccessEvents = true;
})
.AddInMemoryApiResources(Config.GetApis())
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>()
.AddInMemoryClients(Config.GetClients()) ;
{"displayMode":null,"uiLocales":null,"error":"unauthorized_client","errorDescription":"Unknown client or client not enabled","requestId":"0HLPL86NBMDRG:00000001","redirectUri":null,"responseMode":null,"clientId":"spa"}
我真的不明白为什么这不起作用。演示服务器上的相同客户端与 Postman 对话框中的相同客户端可以正常工作。
更新:我找到了这个文档:https ://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-3.0#application-profiles 但我仍然无法让它工作。它识别客户端,但尽管配置(SPA,IdentityServerSPA)抛出:
{"displayMode":null,"uiLocales":null,"error":"invalid_request","errorDescription":"code challenge required","requestId":"0HLPL8VD22382:00000001","redirectUri":"http://localhost:5000/authentication/login-callback?error=invalid_request&error_description=code%20challenge%20required#_=_","responseMode":"query","clientId":"spa"}
更新 2:它与配置 JSON 中定义的客户端“工作”,但仅使用根据 doc 的预定义模板,但不可能(或没有记录的可能性)禁用 PKCE 以使其与 Postman 等一起工作。
解决方案
你没有定义client_secret
. 根据您在客户端配置中提供的代码,您没有设置客户端密码,因此如果未指定客户端密码,则您的客户端无法直接向您的授权 (IDserver) 证明其真实性。这是 PKCE 派上用场的时候,至少您可以保证同一个系统正在执行这两个请求。
我看到您要求禁用 PKCE,这应该是不可能的(我不确定是否可以这样做,但您绝对不应该这样做),因为您正在为 SPA 使用代码身份验证授权。(这是目前推荐的做事方式)
由于 SPA 是非机密客户端(无法保持机密安全),这意味着任何其他应用程序都可以使用您的 client_idspa
向令牌端点发出请求。为了防止这种情况,我们结合了两件事:
重定向 URI:这会强制将响应代码令牌重定向到以前已知的地址,该地址应该是您的客户端(除非使用主机文件取代您的站点)
PKCE:一种旨在保证
/authorize
和/token
请求都来自同一个客户端的机制,因此即使有人设法截获代码,他/她也不应该能够使用它来换取令牌,因为不知道原始秘密在 PKCE 中使用。
推荐阅读
- python - 为什么我不能遍历 get_queryset 中的对象?
- c++ - 如何在 Windows 上将 std::vector 固定/取消固定到实际的、未压缩的硬件内存?
- azure - 自动续订期间是否会克隆 keyvault 标签?
- numpy - 不等式的最小二乘线性规划
- algorithm - 线性代数,几何。点积的值为 0
- swift - Swift GCD:为什么信号处理程序在函数中不起作用
- scala - 使用枢轴旋转单行 Spark 数据框
- python - 如何模拟在类属性中调用的函数?
- python - Python:通过行中的非零值获取列名
- javascript - amCharts (V4):无年份的 DateAxis 中的第一个月格式