zoukankan      html  css  js  c++  java
  • 8-IdentityServer4登录中心

    1-新建webapi  IdentityServer4服务器项目

    E:coding
    etcoreIdentityServerSample>dotnet new webapi --name IdentityServerCenter

    2-增加IdentityServer4 Nuget包, 按下Ctrl+p, 输入>nuget ,增加IdentityServer4包,然后再用dotnet restore保存

    3-增加Config.cs类, 用于提供IdentityServer的ApiResource和Client

    namespace IdentityServerCenter{
        public class Config{
            public static IEnumerable<ApiResource> GetResources(){
                return new List<ApiResource>(){
    
                    new ApiResource("api","My Api")
                };
            }
    
            public static IEnumerable<Client> GetClients(){
                return new List<Client>(){
    
                    new Client(){
                        ClientId="client",
                        AllowedGrantTypes=GrantTypes.ClientCredentials,
    
                        ClientSecrets = {
                            new Secret("secret".Sha256())
                        },
                        AllowedScopes = {"api"}                   
                    }
                };
            }
        }
    }

    4-在Startup.cs 启用时启用IdentifyServer

     public void ConfigureServices(IServiceCollection services)
            {
                services.AddIdentityServer()
                .AddDeveloperSigningCredential()
                .AddInMemoryApiResources(Config.GetResources())
                .AddInMemoryClients(Config.GetClients());
                      
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            }
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                app.UseIdentityServer(); //启用IdentityServer
            }

    5-在Progrom.cs启动类中设置启动的Url

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

    6-输入 http://localhost:5000/.well-known/openid-configuration进行测试IdentityServer4是否起作用,结果如下

  • 相关阅读:
    OAuth2.0 基础概述
    Ubuntu安装Gogs服务
    ASP.NET WebAPI 生成帮助文档与使用Swagger服务测试
    ASP.NET MVC 中的路由
    升级Ghost
    搭建Golang开发环境
    TDD并不是看上去的那么美
    .NET Framework 源码查看与调试
    在 ASP.NET MVC 中使用异步控制器
    SpringMVC+FreeMarker+Mybatis 整合
  • 原文地址:https://www.cnblogs.com/qinzb/p/9461465.html
Copyright © 2011-2022 走看看