日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

初探IdentityServer4(客户端模式)

發布時間:2023/12/4 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 初探IdentityServer4(客户端模式) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Oatuth2協議的客戶端模式介紹

    • Client Credentials Grant (客戶端模式)是Oauth2.0協議中,四種模式自建單的一種。它由兩部分構成,客戶端認證服務器。認證服務器確認客戶端無誤后返回一個token,客戶端請求帶著token訪問資源。(一般使用場景是在一個安全的環境下,例如我的同一個系統中,一個api請求另外一個api)。

    • ?這里借用下阮一峰老師畫的圖(博客地址=》http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html)

IdentityServer4客戶端模式實現

  • 首先我們創建一個core的api項目作為認證服務器,添加nuget程序包IdentityServer4,將啟動端口設置為5000。

  • 接下來添加一個類,取名字叫做Config,我們用它來初始化Identityserver(配置要保護的資源和可以訪問該API的客戶端服務器)。
    代碼如下:

/// <summary>/// Idnetity配置,初始化Identityserver/// </summary>public class Config{//定義要保護的資源(webapi)public static IEnumerable<ApiResource> GetApiResources(){return new List<ApiResource>{new ApiResource("api1", "My API")};}//定義可以訪問該API的客戶端public static IEnumerable<Client> GetClients(){return new List<Client>{new Client(){ClientId = "client",AllowedGrantTypes = GrantTypes.ClientCredentials, //設置模式,客戶端模式ClientSecrets ={new Secret("secret".Sha256())},AllowedScopes = { "api1" }}};}}
  • 接下來配置startup,將資源和客戶端的初始信息服務加入到DI容器,同時引用IdentityServer中間件。代碼如下所示:

public void ConfigureServices(IServiceCollection services){services.AddIdentityServer().AddDeveloperSigningCredential().AddInMemoryApiResources(Config.GetApiResources()) //配置資源.AddInMemoryClients(Config.GetClients()); //配置客戶端services.AddMvc();}public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}//使用identityserver中間件app.UseIdentityServer();app.UseMvc();}
  • 再添加一個webapi項目,作為我們的資源服務器。添加nuget包,IdentityServer4.AccessTokenValidation,將啟動端口設置為5001。

  • 2、配置startup,添加認證服務器地址,和apiname &&引用中間件,代碼如下:

public void ConfigureServices(IServiceCollection services){services.AddAuthentication("Bearer").AddIdentityServerAuthentication(options =>{options.Authority = "http://localhost:5000"; //配置Identityserver的授權地址options.RequireHttpsMetadata = false; //不需要httpsoptions.ApiName = "api1"; //api的name,需要和config的名稱相同});services.AddMvc();}public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseAuthentication();// 添加認證中間件app.UseMvc();}
    • 將受保護資源controller添加[Authorize]。(因為資源服務器AddIdentityServerAuthentication 方法的參數和返回值都是AuthenticationBuilder(類似于一個中間件),所以可以多次調用AddIdentityServerAuthentication方法來控制這個api 資源可以讓誰訪問到。)

    • 最開始我們直接訪問資源服務器的api,返回401,因為我們的資源被保護了。

    • 這時候來到IdentityServer4的官網,官網給出了這么一個地址=》

    • 我們訪問這個地址時候,它會返回我們的Config配置=》

    • 其中有一個token_endpoint的url地址,我們帶著Client的配置來訪問它=》

    • 此時拿到Token,再帶著token去訪問我們的資源,爭取獲取到資源數據=》

https://github.com/conanl5566/dotnet-core-Example/tree/master/WebApplication25

總結

以上是生活随笔為你收集整理的初探IdentityServer4(客户端模式)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。