asp.net core IdentityServer4 實現 Client credentials(客戶端憑證)

前言

OAuth 2.0默認四種授權模式(GrantType)

本章主要介紹客戶端模式(client credentials)
,他主要是由兩部分構成客戶端和認證服務器.
認證服務器在確定客戶端信息無誤後向客戶端返回token,客戶端請求資源時帶着該token進行訪問.(在這種模式中用戶可直接向客戶端註冊,客戶端再以自己的名義請求認證服務器)

搭建認證服務器

創建一個Api項目工程,端口設置爲5000

Package

PM> Install-package IdentityServer4 -version 2.5.3

創建一個類Config(配置要保護的資源和可以訪問該API的客戶端服務器)

    /// <summary>
    ///     Identity配置
    /// </summary>
    public class Config
    {
        /// <summary>
        ///     定義要保護的資源
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<ApiResource> GetApiResources() {
            return new List<ApiResource>
            {
                new ApiResource("api1", "My API")
            };
        }
        /// <summary>
        ///     定義授權客戶端
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Client> GetClients() {
            return new List<Client>
            {
                new Client()
                {
                    ClientId = "client",
                    AllowedGrantTypes = GrantTypes.ClientCredentials, //設置模式,客戶端模式
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    AllowedScopes = { "api1" }
                }
            };
        }
    }
配置Startup

在ConfigureServices方法中注入IdentityServer4服務

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddIdentityServer()//IdentityServer4服務
               .AddDeveloperSigningCredential()
               .AddInMemoryApiResources(Config.GetApiResources()) //配置資源
               .AddInMemoryClients(Config.GetClients());//把配置文件的Client配置資源放到內存
        }

在Configure方法中添加IdentityServer4服務中間件

app.UseIdentityServer();

搭建 Api Resource

創建一個客戶端工程項目,端口設置爲5001

Package

PM> Install-package IdentityServer4.AccessTokenValidation -version 2.7.0

配置Startup

在ConfigureServices添加認證服務器地址

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication("Bearer")
               .AddIdentityServerAuthentication(options =>
               {
                   options.Authority = "http://localhost:5000";//授權服務器地址
                    options.RequireHttpsMetadata = false;//不需要https    
                    options.ApiName = "api1";
                });
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

在Configure方法中添加IdentityServer4服務中間件

app.UseIdentityServer();

測試

在客戶端values控制器上面增加[Authorize]

直接訪問資源服務器http://localhost:5001/api/values
1098068-20190927090016128-1190400194.png

訪問受限了 code 401

啓動授權服務器

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

發現端點可通過/.well-known/openid-configuration

1098068-20190927090033128-490612874.png

獲取token

啓動後我們通過token_endpoint獲取token
1098068-20190927090050225-1212836742.png

client_id爲我們在授權服務器配置的clientid,
client_secret爲配置中的secret,
grant_type爲授權模式此處爲客戶端模式(client_credentials),
請求後返回憑證信息,
我們通過access_token再去訪問資源服務器
使用這種授權類型,會向token 。

1098068-20190927090104167-1277964465.png

code 200

概要

示例地址:https://github.com/fhcodegit/IdentityServer4.Samples
IdentityServer4敘述:https://www.cnblogs.com/yyfh/p/11590383.html

發佈了55 篇原創文章 · 獲贊 57 · 訪問量 4643
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章