Search code examples
.net-coreidentityserver4

IdentityServer 4 as a Console Application


I'm trying to run IdentityServer4 as console application. But I'm unsure if I'm doing right. I created Console Application (.NET Core) instead of ASP.NET Core Web Application (.NET Core).

My setup of WebHostBuilder is pretty straightforward:

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

Also the Startup class is very tiny:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentityServer()
                .AddTemporarySigningCredential()
                .AddInMemoryIdentityResources(Config.GetIdentityResources())
                .AddInMemoryApiResources(Config.GetApiResources())
                .AddInMemoryClients(Config.GetClients())
                .AddTestUsers(Config.GetUsers());
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole();
        app.UseIdentityServer();
    }
}

I'm just concerned if I'm missing some pieces which could lead to problems later. Am I on the right way?


Solution

  • Here are the steps to create Identity Server v4 executable using .NET Core version 1.1.

    Create project with Console Application (.NET Core). In the Mainmethod implement following code:

    public class Program
    {
        public static void Main(string[] args)
        {
            Console.Title = "IdentityServer";
    
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseStartup<Startup>()
                .Build();
    
            host.Run();
        }
    }
    

    In the Startup class implement following code:

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            // configure identity server with in-memory stores, keys, clients and scopes
            services.AddIdentityServer()
                .AddTemporarySigningCredential()
                .AddInMemoryApiResources(Config.GetApiResources())
                .AddInMemoryClients(Config.GetClients())
                .AddTestUsers(Config.GetUsers());
        }
    
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(LogLevel.Debug);
            app.UseDeveloperExceptionPage();
    
            app.UseIdentityServer();
        }
    }
    

    To generate executable, you must not have a "type": "platform" in your project.json. For example my project.json looks like this:

    {
      "dependencies": {
        "IdentityServer4": "1.1.1",
        "Microsoft.AspNetCore.Diagnostics": "1.1.0",
        "Microsoft.AspNetCore.Hosting": "1.1.0",
        "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
        "Microsoft.Extensions.Logging.Console": "1.1.0",
        "Microsoft.NETCore.App": {
          "version": "1.1.0"
        }
      },
    
      "frameworks": {
        "netcoreapp1.1": {
        }
      },
    
      "runtimes": {
        "win7-x64": {},
        "win8-x64": {},
        "win10-x64": {}
      }
    }
    

    Than you can generate executable with dotnet publish command like below.

    dotnet publish -c release -r win7-x64
    dotnet publish -c release -r win8-x64
    dotnet publish -c release -r win10-x64

    Disclaimer: This is minimal running version, maybe you will need extended configuration, depends on what you need.