Я пытаюсь создать клиент администратора для 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 (коллекция Postman для запросов доступна здесь: 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
Кто-нибудь знает, что я делаю не так?