Ami*_*ian 5 .net-core identityserver3 asp.net-core identityserver4
我正在研究IdentityServer 4(1.0.0-beta5)。
默认情况下,身份验证的端点为:'/ connect / token'。
如何将IdentityServer中的默认端点更改为例如“ / api / login”?
谢谢
一旦在启动时设置了Identity Server 4,就可以使用此“ hack”并更新端点路径:
var builder = services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients());
builder.Services
.Where(service => service.ServiceType == typeof(Endpoint))
.Select(item => (Endpoint)item.ImplementationInstance)
.ToList()
.ForEach(item => item.Path = item.Path.Value.Replace("/connect", ""));
Run Code Online (Sandbox Code Playgroud)
基本上-端点(例如TokenEndpoint,AuthorizeEndpoint类)在您调用AddIdentityServer时会在内部注册-当它调用AddDefaultEndPoints方法时。现在,在接收到每个请求以匹配所请求的URL时,将迭代端点。因此更改路径将立即生效。
请注意,在上面的示例中-我从所有带有前缀的路径中删除了所有“ / connect”值。
现在这是一个有点老的问题,这只是另一种方式,看起来不像黑客
IdentityServer4 提供了一项名IEndpointRouter为此服务的服务,如果被您的自定义逻辑覆盖,将允许您将客户端请求的路径映射到 IdentityServer4 端点之一。基于IEndpointRouter(顺便说一句,这是内部的)的默认实现,我编写了这个类来自己进行映射。
internal class CustomEndpointRouter : IEndpointRouter
{
const string TOKEN_ENDPOINT = "/oauth/token";
private readonly IEnumerable<Endpoint> _endpoints;
private readonly IdentityServerOptions _options;
private readonly ILogger _logger;
public CustomEndpointRouter (IEnumerable<Endpoint> endpoints, IdentityServerOptions options, ILogger<CustomEndpointRouter > logger)
{
_endpoints = endpoints;
_options = options;
_logger = logger;
}
public IEndpointHandler Find(Microsoft.AspNetCore.Http.HttpContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (context.Request.Path.Equals(TOKEN_ENDPOINT, StringComparison.OrdinalIgnoreCase))
{
var tokenEndPoint = GetEndPoint(EndpointNames.Token);
return GetEndpointHandler(tokenEndPoint, context);
}
//put a case for all endpoints or just fallback to IdentityServer4 default paths
else
{
foreach (var endpoint in _endpoints)
{
var path = endpoint.Path;
if (context.Request.Path.Equals(path, StringComparison.OrdinalIgnoreCase))
{
var endpointName = endpoint.Name;
_logger.LogDebug("Request path {path} matched to endpoint type {endpoint}", context.Request.Path, endpointName);
return GetEndpointHandler(endpoint, context);
}
}
}
_logger.LogTrace("No endpoint entry found for request path: {path}", context.Request.Path);
return null;
}
private Endpoint GetEndPoint(string endPointName)
{
Endpoint endpoint = null;
foreach (var ep in _endpoints)
{
if (ep.Name == endPointName)
{
endpoint = ep;
break;
}
}
return endpoint;
}
private IEndpointHandler GetEndpointHandler(Endpoint endpoint, Microsoft.AspNetCore.Http.HttpContext context)
{
if (_options.Endpoints.IsEndpointEnabled(endpoint))
{
var handler = context.RequestServices.GetService(endpoint.Handler) as IEndpointHandler;
if (handler != null)
{
_logger.LogDebug("Endpoint enabled: {endpoint}, successfully created handler: {endpointHandler}", endpoint.Name, endpoint.Handler.FullName);
return handler;
}
else
{
_logger.LogDebug("Endpoint enabled: {endpoint}, failed to create handler: {endpointHandler}", endpoint.Name, endpoint.Handler.FullName);
}
}
else
{
_logger.LogWarning("Endpoint disabled: {endpoint}", endpoint.Name);
}
return null;
}
}
internal static class EndpointOptionsExtensions
{
public static bool IsEndpointEnabled(this EndpointsOptions options, Endpoint endpoint)
{
switch (endpoint?.Name)
{
case EndpointNames.Authorize:
return options.EnableAuthorizeEndpoint;
case EndpointNames.CheckSession:
return options.EnableCheckSessionEndpoint;
case EndpointNames.Discovery:
return options.EnableDiscoveryEndpoint;
case EndpointNames.EndSession:
return options.EnableEndSessionEndpoint;
case EndpointNames.Introspection:
return options.EnableIntrospectionEndpoint;
case EndpointNames.Revocation:
return options.EnableTokenRevocationEndpoint;
case EndpointNames.Token:
return options.EnableTokenEndpoint;
case EndpointNames.UserInfo:
return options.EnableUserInfoEndpoint;
default:
// fall thru to true to allow custom endpoints
return true;
}
}
}
public static class EndpointNames
{
public const string Authorize = "Authorize";
public const string Token = "Token";
public const string DeviceAuthorization = "DeviceAuthorization";
public const string Discovery = "Discovery";
public const string Introspection = "Introspection";
public const string Revocation = "Revocation";
public const string EndSession = "Endsession";
public const string CheckSession = "Checksession";
public const string UserInfo = "Userinfo";
}
Run Code Online (Sandbox Code Playgroud)
然后你只需要CustomEndpointRouter像下面这样注册这个服务
services.AddTransient<IEndpointRouter, CustomEndpointRouter>();
Run Code Online (Sandbox Code Playgroud)
请注意,此更新的路径不会出现在发现文档中
| 归档时间: |
|
| 查看次数: |
2418 次 |
| 最近记录: |