.net core 中 identity server 4 之Server简单示例

Steps:

  • 1.新建一个ASP.NET Core Web项目,SigmalHex.IdentityServer;

  • 2.安装包

Install-Package IdentityServer4
  • 3.Startup中加入
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients());
} public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Debug);
app.UseDeveloperExceptionPage(); app.UseIdentityServer();
}
}
    • AddIdentityServer

      注册Identity Server 服务到DI系统。
    • AddTemporarySigningCredential

      每次启动的时候,生成一个临时的签名证书
  • 4.Config
using IdentityServer4.Models;
using System.Collections.Generic; namespace SigmalHex.IdentityServer
{
public class Config
{
// scopes define the API resources in your system
public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("api1", "My API")
};
} // client want to access resources (aka scopes)
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ClientCredentials, ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
}
};
}
}
}
  • 5.启动,即完成了IdentityServer内存版的搭建。
using Microsoft.AspNetCore.Hosting;
using System;
using System.IO; namespace SigmalHex.IdentityServer
{
public class Program
{
public static void Main(string[] args)
{
Console.Title = "IdentityServer"; var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://localhost:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); host.Run();
}
}
}
上一篇:Scala的映射和元组操作


下一篇:第9章 使用客户端凭据保护API - Identity Server 4 中文文档(v1.0.0)