M U*_*M U 5 c# asp.net-identity .net-core identityserver4
我正在努力使用 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
},
};
}
Run Code Online (Sandbox Code Playgroud)
在我的启动中:
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()) ;
Run Code Online (Sandbox Code Playgroud)
{"displayMode":null,"uiLocales":null,"error":"unauthorized_client","errorDescription":"Unknown client or client not enabled","requestId":"0HLPL86NBMDRG:00000001","redirectUri":null,"responseMode":null,"clientId":"spa"}
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么这不起作用。演示服务器上的相同客户端与 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"}
Run Code Online (Sandbox Code Playgroud)
更新 2:它与配置 JSON 中定义的客户端“工作”,但仅使用根据文档预定义的模板,但不可能(或未记录可能性)禁用 PKCE 以使其工作,例如与 Postman 等一起工作。
您没有定义client_secret. 根据您在客户端配置上提供的代码,您没有设置客户端密钥,因此如果未指定客户端密钥,则您的客户端无法直接向您的授权机构(IDserver)证明其真实性。这就是 PKCE 派上用场的时候,至少你可以保证同一个系统正在执行这两个请求。
我看到您要求禁用 PKCE,这应该是不可能的(我不确定是否可以做到,但您绝对不应该这样做),因为您正在使用 SPA 的代码身份验证授权。(这是当前推荐的做事方式)
由于 SPA 是非机密客户端(无法保证秘密安全),这意味着任何其他应用程序都可以使用您的 client_idspa向令牌端点发出请求。为了防止这种情况,我们结合了两件事:
重定向 URI:这会强制将响应代码令牌重定向到先前已知的地址,该地址应该是您的客户端(除非使用主机文件取代您的站点)
PKCE:一种机制,旨在保证/authorize和/token请求都来自同一个客户端,因此即使有人设法拦截代码,他/她也不应该能够使用它来交换令牌,因为不知道原始秘密用于 PKCE。
| 归档时间: |
|
| 查看次数: |
5136 次 |
| 最近记录: |