Search code examples
extendidentityserver3

Can I add a service info / health check endpoint to my Identity Server 3-based service?


I have a set of AspNet WebApi-based web services and an IdentityServer3-based authentication service. All of the web services support a simple service info endpoint that we use for monitoring and diagnosis. It reports the service version and the server name. The only service that currently does not support the service info endpoint is the IdentityServer3-based authentication service.

Is there a way to add a simple endpoint to an IdentityServer3-based service? In GitHub issue 812 Brock Allen says "We have a way to add custom controllers, but it's undocumented, current unsupported, and not really done." I'd rather not take that indocumented, unsupported route.

Is there a way to modify/extend the discovery endpoint to include additional information?


Solution

  • Here's how I ended up coding this up. At a high level, basically I added a Controllers folder, created a AuthenticationServiceInfoController class with a single GET action method and then registered that controller during Startup. As noted in comment above, my solution had some extra complexity because my AuthenticationServiceInfoController inherited from a base ServiceInfoController defined elsewhere, but I've tried to eliminate that from this sample. So, the controller code looks like this:

    [RoutePrefix("api/v1/serviceinfo")]
    public class AuthencticationServiceInfoController : IServiceInfoController
    {
        [Route("")]
        [Route("~/api/serviceinfo")]
        public IHttpActionResult Get()
        {
            try
            {
                ServiceInformation serviceInfo = new ServiceInformation();
                serviceInfo.ServiceVersion = Global.serviceVersion;
    
                return Ok(serviceInfo);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
    }
    

    It implements a simple interface:

    public interface IServiceInfoController
    {
        IHttpActionResult Get();
    }
    

    And in my Startup.Configuration method where I configure Identity Server, I've got:

            var idSrvFactory = new IdentityServerServiceFactory();
            idSrvFactory.Register(new Registration<IServiceInfoController, Controllers.AuthencticationServiceInfoController>());
    

    I think that's all that it took. It's in place and working in my Identity Server 3-based service.