更改IdentityServer 4中的默认端点

时间:2016-08-27 22:57:40

标签: asp.net-core .net-core identityserver3 identityserver4

我正在使用IdentityServer 4(1.0.0-beta5)。

默认情况下,身份验证的终端是:'/ connect / token'

如何更改IdentityServer中的默认端点,例如:'/ api / login'?

由于

5 个答案:

答案 0 :(得分:5)

在启动时设置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", ""));

基本上 - 一旦你调用 AddIdentityServer ,当它调用 TokenEndpoint , AuthorizeEndpoint 类等端点会在内部注册> AddDefaultEndPoints 方法。现在,在接收到与请求的Url匹配的每个请求时,迭代Endpoint;所以改变路径会立即生效。

  

请注意,在上面的示例中 - 我删除了所有内容   " /连接"来自任何前缀的路径的值。

答案 1 :(得分:4)

目前您无法更改协议端点的端点URL。如果您认为这是必要的,请在github上打开一个问题。

答案 2 :(得分:1)

这是一个有点老的问题了,这只是另一种方式,看起来不像是骇客

IdentityServer4提供了一个名为IEndpointRouter的服务,如果该服务被您的自定义逻辑所覆盖,则将允许您将客户端请求的路径映射到IdentityServer4端点之一。 基于IEndpointRouter的默认实现(这是内部btw),我编写了此类自己进行映射。

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";
}

然后,您只需要像下面一样注册此CustomEndpointRouter服务

services.AddTransient<IEndpointRouter, CustomEndpointRouter>();

请注意,此更新的路径不会出现在发现文档中

答案 3 :(得分:0)

您可以尝试一下 services.AddIdentityServer(options => options.PublicOrigin =“ URL”)

检查此链接。 http://amilspage.com/set-identityserver4-url-behind-loadbalancer/

答案 4 :(得分:-1)

只需将此添加到 Startup.cs

Tag.all