我正在尝试为IdentityServer4创建一个管理客户端(在此处发布时查看完整代码:https://github.com/TheMagnificent11/identity-server-admin/tree/0.0.1)。
我已按照此处概述的标准步骤设置了ID服务器:http://docs.identityserver.io/en/latest/quickstarts/7_entity_framework.html。唯一的区别是,我已将数据访问层移至单独的.Net Standard库中。
我创建了另一个使用客户端凭据的网站。客户端是在ID Server站点启动时配置的(在调试配置中运行时)。这是代码:
public static void InitializeDatabase(
this IApplicationBuilder app,
string adminApiName,
string clientId,
string clientSecret)
{
#if DEBUG
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var appContext = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
appContext.Database.Migrate();
var grantContext = serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>();
grantContext.Database.Migrate();
var configContext = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
configContext.Database.Migrate();
SeedAdminClient(adminApiName, clientId, clientSecret, configContext);
}
#endif
}
private static void SeedAdminClient(string adminApiName, string clientId, string clientSecret, ConfigurationDbContext configContext)
{
if (!configContext.IdentityResources.Any())
{
foreach (var resource in DefaultData.IdentityResources)
{
configContext.IdentityResources.Add(resource.ToEntity());
}
}
if (!configContext.ApiResources.Any())
{
var apiResource = new ApiResource(adminApiName, "Identity Server Admin");
configContext.ApiResources.Add(apiResource.ToEntity());
}
if (!configContext.Clients.Any())
{
var adminClient = new Client
{
ClientName = "Identity Server Admin",
ClientId = clientId,
ClientSecrets =
{
new Secret(clientSecret.Sha256())
},
AllowedScopes =
{
adminApiName
},
AllowedGrantTypes = GrantTypes.ClientCredentials,
Claims =
{
new Claim(AdminClientClaims.ManageUsersType, AdminClientClaims.ManageUsersValue)
}
};
configContext.Clients.Add(adminClient.ToEntity());
}
configContext.SaveChanges();
}
我可以使用客户端凭据获得令牌。但是,使用令牌调用客户端API会意外收到404(请求的邮递员集合可在此处找到:https://github.com/TheMagnificent11/identity-server-admin/blob/0.0.1/postman_collection.json)。
这是客户端API的API输出:
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 POST https://localhost:4001/users application/json 151
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint 'IdentityServer.Controllers.Users.UsersController.Post (IdentityServer.Admin)'
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Route matched with {action = "Post", controller = "Users"}. Executing action IdentityServer.Controllers.Users.UsersController.Post (IdentityServer.Admin)
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
Authorization failed.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3]
Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
info: Microsoft.AspNetCore.Mvc.ChallengeResult[1]
Executing ChallengeResult with authentication schemes ().
info: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[12]
AuthenticationScheme: Identity.Application was challenged.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action IdentityServer.Controllers.Users.UsersController.Post (IdentityServer.Admin) in 69.589ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint 'IdentityServer.Controllers.Users.UsersController.Post (IdentityServer.Admin)'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 284.0746ms 302
info: Microsoft.AspNetCore.Server.Kestrel[32]
Connection id "0HLL47CTA76NM", Request id "0HLL47CTA76NM:00000001": the application completed without reading the entire request body.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET https://localhost:4001/Account/Login?ReturnUrl=%2Fusers
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 25.5762ms 404
有人知道我在做什么错吗?
答案 0 :(得分:2)
外观与此问题非常相似:https://github.com/IdentityServer/IdentityServer4/issues/2406
我把它拉下来,尝试了这个:
Compiled Successfully!
3
让我得到403而不是404
答案 1 :(得分:0)
如果收到404响应,请确保使用正确的GET / POST / PUT请求。即使路由URL正确,在仅允许POST请求的路由上使用GET方法也会导致404。