ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

IdentityServer4(二)

2021-08-01 07:31:48  阅读:168  来源: 互联网

标签:IEnumerable static services new Config public IdentityServer4


1、新建空的Identity项目

 

 

 

2、访问localhost:5001/.well-known/openid-configuration

 

 

 3、访问localhost:5001/connect/token

 

 

 4、需要用postman 访问localhost:5001/connect/token,要添加参数,不然会报错,错误信息: "error": "invalid_request"或者"error": "invalid_scope"

注意:要在 x-www-form-urlencoded 中添加参数,在form-data 添加参数还是会报错

 

 

 

 

5、需要修改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"
                }
            };

    }

6、需要修改Startup类的ConfigureServices方法

  public void ConfigureServices(IServiceCollection services)
        {
            // uncomment, if you want to add an MVC-based UI
            //services.AddControllersWithViews();

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

 

7、另一种方式,不添加scopes

Config类修改

public static class Config
    {
        public static IEnumerable<IdentityResource> IdentityResources =>
            new IdentityResource[]
            {
                new IdentityResources.OpenId()
            };
        public static IEnumerable<Client> Clients =>
            new Client[]
            {
                new Client
                {
                    ClientId="myclient",
                    ClientSecrets=new []{new Secret("secret".Sha256()) },
                    AllowedGrantTypes=GrantTypes.ResourceOwnerPasswordAndClientCredentials,
                    AllowedScopes=new [] { "openid" }
                }
            };

        public static List<TestUser> TestUsers =>
            new List<TestUser>
            {
                new TestUser
                {
                    Username="pc",
                    Password="123",
                    SubjectId="1"
                }
            };
    }

Startup类修改

    public void ConfigureServices(IServiceCollection services)
        {
            // uncomment, if you want to add an MVC-based UI
            //services.AddControllersWithViews();

            var builder = services.AddIdentityServer()
                .AddInMemoryIdentityResources(Config.IdentityResources)
                .AddInMemoryClients(Config.Clients)
                .AddTestUsers(Config.TestUsers);

            // not recommended for production - you need to store your key material somewhere secure
            builder.AddDeveloperSigningCredential();
            services.AddAuthentication();
        }

 

备注:不添加ApiScopes  时,使用 IdentityResource 的openid

 

标签:IEnumerable,static,services,new,Config,public,IdentityServer4
来源: https://www.cnblogs.com/lhwpc/p/15040086.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有