ASP.NET Core分布式项目实战

默认教学计划
1035人加入学习
(33人评价)
价格 ¥398.00
教学计划

  官方文档 https://identityserver4.readthedocs.io/en/latest/

 

1 IdentityServer4 ASP .NET Core 认证中心 搭建

1.1 新建ASP .NET Core 项目 ```nuget IdentityServer4``` 

1.2 新建配置类 IdentityConfig 

public class IdentityConfig
    {
        /// <summary>
        ///   ApiResource 
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<ApiResource> GetResource()
        {
            return new List<ApiResource>() {
            new ApiResource("UserAPI","OrderAPI"){
            } };
        }
        /// <summary>
        /// Client
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Client> GetClients()
        {
            var cilentApp = new Client()
            {
                ClientId = "App1",
                ClientName = "App",
                AllowedGrantTypes = new List<string>() {
                    GrantType.ClientCredentials },
                ClientSecrets = new List<Secret>() {
                    new Secret("Sercrect".Sha256())
                },
                AllowedScopes = { "UserAPI"}
            };
            return new List<Client>() {
                    cilentApp
            };
        }
    }

1.3  Startup.cs配置 IdentityServer 

public void ConfigureServices(IServiceCollection services)
        {
            //依赖注入系统中注册IdentityServer
            services.AddIdentityServer()
            //扩展在每次启动时,为令牌签名创建了一个临时密钥。在生成环境需要一个持久化的密钥
                .AddDeveloperSigningCredential()
                .AddInMemoryApiResources(IdentityConfig.GetResource())//Api 资源
                .AddInMemoryClients(IdentityConfig.GetClients());//Api的客户端
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
         
        }
 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            
            app.UseIdentityServer();//使用服务 中间件被添加到HTTP管道中
        }

1.4 启动站点 输入 ```http://localhost:4000/.well-known/openid-configuration```,可以看到授权的终结点等信息

[展开全文]

http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html

[展开全文]

http://localhost:5000/.well-known/openid-configuration

[展开全文]

.netcore2.1环境:

写成:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseUrls("http://localhost:5000");

 

取消 .build();

[展开全文]

客户端模式:

添加Nuget包

添加Startup配置

添加Config.cs配置

更改identityServer4配置

添加客户端配置

dotnet new webapi --name IdentityServerCenter

添加Nuget包:

ctrl p

>nuget package add

identityserver4 =>2.1.0

cd .\identityservercenter\

dotnet restore

添加Startup配置:

serverces.AddIdentityServer().AddDeveloperSianingCredential();

 

编写Config,cs类:

构造静态方法返回IEnumerable<ApiResource>

ApiResource需要引用IdentityServer4.Models

构造静态方法返回IEnumerable<Client>

Client需要ClientId ,AllowedGrantTypes,ClientSercets

 

 

 

 

 

 

 

 

 

 

          

[展开全文]

测试地址:http://localhost/.well-known/openid-configuration

[展开全文]

IdentityServer4  endPoint查看地址:http://localhost:5000/.well-known/openId-configuration

[展开全文]

授课教师

程序员

课程特色

下载资料(2)
视频(144)
讨论(1)
图文(2)