IdentityServer 4作为控制台应用程序

时间:2017-02-17 14:32:36

标签: .net-core identityserver4

我正在尝试将IdentityServer4作为控制台应用程序运行。但我不确定我是否做得对。我创建了控制台应用程序(.NET Core)而不是 ASP.NET核心Web应用程序(.NET Core)

我对WebHostBuilder的设置非常简单:

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

        host.Run();
    }
}

Startup 类也非常小:

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();
    }
}

我只是担心如果我错过了一些可能导致问题的部分。我是在正确的方式吗?

3 个答案:

答案 0 :(得分:2)

以下是使用 .NET Core 1.1版创建 Identity Server v4 可执行文件的步骤。

使用控制台应用程序(.NET Core)创建项目。在Main方法中实现以下代码:

public class Program
{
    public static void Main(string[] args)
    {
        Console.Title = "IdentityServer";

        var host = new WebHostBuilder()
            .UseKestrel()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

Startup类实现以下代码:

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();
    }
}

要生成可执行文件, project.json 中的不得 "type": "platform"。例如,我的 project.json 如下所示:

{
  "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": {}
  }
}

您可以使用 dotnet publish 命令生成可执行文件,如下所示。

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

免责声明:这是最小的运行版本,可能需要扩展配置,具体取决于您的需求。

答案 1 :(得分:1)

每个.NET Core应用程序都是一个控制台应用程序。因此,您使用的模板没有区别。

但我认为在你的主要内容 - 你错过了对UseContentRoot的电话。

答案 2 :(得分:0)