• 1.客户端模式


    OAuth2 客户端的授权模式分为4种:
    1.     授权码模式(authorization code)
    2.     简化模式(implicit)
    3.     密码模式(resource owner password credentials)
    4.     客户端模式(client credentials)

    下面我们就总结一下,如何使用IdentityServer来实现客户端模式.
    客户端模式是里面最简单的模式
    客户端模式指客户端以自己的名义,而不是以用户的名义,向服务提供商进行认证。

    上图表示了客户端请求的流程

    再向授权服务器发送请求的时候,客户端发出的HTTP请求,包含以下参数:
    • granttype :表示授权类型,此处的值固定为"clientcredentials",必选项
    • scope:表示权限范围,可选项




    授权服务器必须以某种方式,验证客户端身份
    授权服务器向客户端发送访问令牌

    下面开始用代码实现IdentityServer ClientCredentials

    首先用vs创建一个net core webapi项目,nuget IdentityServer包。
    创建一个Config.cs类
    public class Config
        {
            public static IEnumerable<ApiResource> GetResources()
            {
                return new List<ApiResource>
                {
                    new ApiResource("api1","my api")
                };
            }
            
            public static IEnumerable<Client> GetClients()
            {
                return new List<Client>
                {
                    new Client
                    {
                         AllowedGrantTypes=GrantTypes.ClientCredentials, //客户端模式
                        //客户端有权访问的范围
                         AllowedScopes={ "api1" },
                         ClientId="mvc",
                        //用于认证的密码
                         ClientSecrets={ new Secret("secret".Sha256()) },
                    }
                };
            }
        }

    在Startup类的ConfigureServices方法中注入IdentityServer和在Configure添加IdentityServer中间件
         public void ConfigureServices(IServiceCollection services)
            {
                // 使用内存存储,密钥,客户端和资源来配置身份服务器。
                services.AddIdentityServer()
                        .AddDeveloperSigningCredential()
                        .AddInMemoryApiResources(Config.GetResources())
                        .AddInMemoryClients(Config.GetClients());
                services.AddMvc();
            }
    
     public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                //添加IdentityServer中间件
                app.UseIdentityServer();
                app.UseMvc();
            }
    完成以后编译运行,在浏览器输入http://localhost:5000/.well-known/openid-configuration
    {
        "issuer": "http://localhost:5000",
        "jwks_uri": "http://localhost:5000/.well-known/openid-configuration/jwks",
        "authorization_endpoint": "http://localhost:5000/connect/authorize",
        "token_endpoint": "http://localhost:5000/connect/token",  //获取token的地址
        "userinfo_endpoint": "http://localhost:5000/connect/userinfo",
        "end_session_endpoint": "http://localhost:5000/connect/endsession",
        "check_session_iframe": "http://localhost:5000/connect/checksession",
        "revocation_endpoint": "http://localhost:5000/connect/revocation",
        "introspection_endpoint": "http://localhost:5000/connect/introspect",
        "frontchannel_logout_supported": true,
        "frontchannel_logout_session_supported": true,
        "backchannel_logout_supported": true,
        "backchannel_logout_session_supported": true,
        "scopes_supported": [
            "api1",
            "offline_access"
        ],
        "claims_supported": [],
        "grant_types_supported": [
            "authorization_code",
            "client_credentials",
            "refresh_token",
            "implicit"
        ],
        "response_types_supported": [
            "code",
            "token",
            "id_token",
            "id_token token",
            "code id_token",
            "code token",
            "code id_token token"
        ],
        "response_modes_supported": [
            "form_post",
            "query",
            "fragment"
        ],
        "token_endpoint_auth_methods_supported": [
            "client_secret_basic",
            "client_secret_post"
        ],
        "subject_types_supported": [
            "public"
        ],
        "id_token_signing_alg_values_supported": [
            "RS256"
        ],
        "code_challenge_methods_supported": [
            "plain",
            "S256"
        ]
    }

    再创建一个API程序
    Nuget IdentityServer4.AccessToken.Validation
    需要再Startup.cs里面配置
     public void ConfigureServices(IServiceCollection services)
            {
                services.AddAuthentication("Bearer")
                        .AddIdentityServerAuthentication(options => {
                            options.Authority = "http://localhost:5000"; //授权的服务器
                            options.RequireHttpsMetadata = false;
                            options.ApiName = "api1";
                        }); 
                services.AddMvc();
            }
    
     public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                //添加中间件
                app.UseAuthentication();
                app.UseMvc();
            }

    在要访问的控制器加上Authorize标签

    使用PostMan

    获取AccessToken 

    再用PostMan去http://localhost:5001/api/values 访问









    附件列表

    • 相关阅读:
      理解FreeRTOS的任务状态机制
      stm32【按键处理:单击、连击、长按】
      stm32f4单片机 硬件浮点运算
      stm32 HAL库 串口无法接收数据的问题
      Single Sign On —— 简介(转)
      关于第三方库安装时很慢或者读取超时问题处理
      设计模式》状态机模式
      设计模式》责任链模式
      设计模式》访问者模式
      设计模式》策略者模式
    • 原文地址:https://www.cnblogs.com/changfutao/p/9122384.html
    Copyright © 2020-2023  润新知