• IdentityServer4(三)


    1、新建空的asp.net core 项目

     目录结构

    2、添加 IdentityServer4 相关类库 

    3、添加Config类

     public static class Config
        {
            public static IEnumerable<IdentityResource> IdentityResources =>
                new IdentityResource[]
                {
                    new IdentityResources.OpenId()
                };
            public static IEnumerable<ApiScope> ApiScopes =>
            new ApiScope[]
            {
                    new ApiScope("scope1"),
                    new ApiScope("scope2"),
            };
    
            public static IEnumerable<Client> Clients =>
                new Client[]
                {
                    new Client
                    {
                        ClientId="myclient",
                        ClientSecrets=new []{new Secret("secret".Sha256()) },
                        AllowedGrantTypes=GrantTypes.ResourceOwnerPasswordAndClientCredentials,
                        AllowedScopes=new [] { "scope1" }
                    }
                };
    
            public static List<TestUser> TestUsers =>
                new List<TestUser>
                {
                    new TestUser
                    {
                        Username="pc",
                        Password="123",
                        SubjectId="1"
                    }
                };
    
        }

    4、修改Startup文件

     public void ConfigureServices(IServiceCollection services)
            {
                var builder = services.AddIdentityServer()
                   .AddTestUsers(Config.TestUsers)
                   .AddInMemoryApiScopes(Config.ApiScopes)
                   .AddInMemoryClients(Config.Clients);
    
                // not recommended for production - you need to store your key material somewhere secure
                builder.AddDeveloperSigningCredential();
    
                services.AddAuthentication();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseRouting();
    
    
                app.UseIdentityServer();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapGet("/", async context =>
                    {
                        await context.Response.WriteAsync("Hello World!");
                    });
                });
            }

    5、启动程序

    6、Postman访问

  • 相关阅读:
    函数嵌套 lisp表达式求值
    初涉时间间隔问题
    高精度算法-带小数大数相加
    12/10 C语言程序设计竞赛 后五题
    字符串头尾连接问题-木棒连接
    ZJGSU-ACM OJ 心得
    高精度算法-大数乘法
    趣味两题-(简单追及问题、两直线相交问题)
    struts2基于注解的action
    spring中常用的注解
  • 原文地址:https://www.cnblogs.com/lhwpc/p/15040452.html
Copyright © 2020-2023  润新知