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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

.Net Core 3.0 IdentityServer4 快速入门

發布時間:2023/12/4 asp.net 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .Net Core 3.0 IdentityServer4 快速入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、簡介

  IdentityServer4是用于ASP.NET Core的OpenID Connect和OAuth 2.0框架。

  將IdentityServer4部署到您的應用中具備如下特點:

  1)、認證服務

  2)、單點登陸

  3)、API訪問控制

  4)、聯合網關

  5)、專注于定制

  6)、成熟的開源系統

  7)、免費和商業支持

二、整體部署

?

  目前大多數的應用程序或多或少看起來是上圖所示這樣的,最常見的交互場景有(瀏覽器與Web應用程序、Web應用程序與WebApi通訊、本地應用程序獄WebApi通訊、基于瀏覽器的應用程序與WebApi 通訊、基本服務器的應用程序與WebApi通訊、WebApi與WebApi通訊)

  前端、中間層、后端各個層級為了保護資源經常要針對相同的用戶倉儲區實現身份認證和授權,但是如果我們把這些基本的安全功能統一頒發給一個安全令牌服務,就可以不必再讓這些應用和端點之間重復實現這些基礎安全功能,重組應用程序以支持安全令牌服務將會引導出以下體系結構和協議

?

  這樣的設計將會把安全問題分為兩個部分:(身份驗證和API訪問)

三、IdentityServer4如何提供幫助

  IdentityServer是將規范兼容的OpenID Connect和OAuth 2.0端點添加到任意ASP.NET Core應用程序的中間件。通常,您構建(或重新使用)包含登錄和注銷頁面的應用程序,IdentityServer中間件會向其添加必要的協議頭,以便客戶端應用程序可以與其對話 使用這些標準協議。

?

四、術語

 

? ? ?1)、Users(用戶):用戶是使用已注冊的客戶端訪問資源的人

 ? ? 2)、Clients(客戶端):客戶端就是從identityserver請求令牌的軟件(你可以理解為一個app即可),既可以通過身份認證令牌來驗證識別用戶身份,又可以通過授權令牌來訪問服務端的資源。但是客戶端首先必須在申請令牌前已經在identityserver服務中注冊過。實際客戶端不僅可以是Web應用程序,app或桌面應用程序(你就理解為pc端的軟件即可),SPA,服務器進程等

  3)、Resources(資源):

  資源就是你想用identityserver保護的東東,可以是用戶的身份數據或者api資源。

  用戶的身份信息實際由一組claim組成,例如姓名或者郵件都會包含在身份信息中(將來通過identityserver校驗后都會返回給被調用的客戶端)。

  API資源就是客戶端想要調用的功能(通常以json或xml的格式返回給客戶端,例如webapi,wcf,webservice),通常通過webapi來建立模型,但是不一定是webapi,我剛才已經強調可以使其他類型的格式,這個要看具體的使用場景了。

  4)、Identity Token(身份令牌):

  一個身份令牌指的就是對認證過程的描述。它至少要標識某個用戶(Called the sub aka subject claim)的主身份信息,和該用戶的認證時間和認證方式。但是身份令牌可以包含額外的身份數據,具體開發者可以自行設定,但是一般情況為了確保數據傳輸的效率,開發者一般不做過多額外的設置,大家也可以根據使用場景自行決定。

  5)、Access Token(訪問令牌):

?  訪問令牌允許客戶端訪問某個 API 資源。客戶端請求到訪問令牌,然后使用這個令牌來訪問 API資源。訪問令牌包含了客戶端和用戶(如果有的話,這取決于業務是否需要,但通常不必要)的相關信息,API通過這些令牌信息來授予客戶端的數據訪問權限。

五、代碼快速入門 (使用客戶端憑據保護)

  1)、IdentityServer

?    a)、定義Api資源和客戶端

      Api 是您系統中要保護的資源,資源的定義可以通過多種方式

      客戶端代碼中的ClientId和ClientSecret你可以視為應用程序本身的登錄名和密碼,它將您的應用程序標識到IdentityServer 服務器,以便它知道哪個應用程序正在嘗試與其連接

using IdentityServer4.Models; using System.Collections.Generic; namespace IdentityServer { public static class Config { public static IEnumerable<ApiResource> Apis => new List<ApiResource> { new ApiResource("api1","My API") }; public static IEnumerable<Client> Clients => new List<Client> { new Client { ClientId="client", AllowedGrantTypes =GrantTypes.ClientCredentials, ClientSecrets={ new Secret("aju".Sha256()) }, AllowedScopes={ "api1"} } }; } }

????????b)、配置IdentityServer

using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace IdentityServer { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { var builder = services.AddIdentityServer() .AddInMemoryApiResources(Config.Apis) .AddInMemoryClients(Config.Clients); builder.AddDeveloperSigningCredential(); } // 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.UseIdentityServer(); //app.UseRouting(); //app.UseEndpoints(endpoints => //{ // endpoints.MapGet("/", async context => // { // await context.Response.WriteAsync("Hello World!"); // }); //}); } } }

????????c)、測試(如果配置合適,在瀏覽器訪問?http://localhost:5000/.well-known/openid-configuration? 出現如下表示配置OK)

首次啟動時,IdentityServer將為您創建一個開發人員簽名密鑰,該文件名為tempkey.rsa。您無需將該文件簽入源代碼管理中,如果不存在該文件將被重新創建。

    d)、所需的包

  2)、添加Api資源

  ????a)、添加一個名為IdentityController的控制器

using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Linq; namespace Api.Controllers { [Route("identity")] [Authorize] public class IdentityController : ControllerBase { public IActionResult Get() { return new JsonResult(from c in User.Claims select new { c.Type, c.Value }); } } }

????????b)、配置(將身份認證服務添加到DI,并將身份驗證中間件添加到管道)    

using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Api { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddAuthentication("Bearer").AddJwtBearer("Bearer", options => { options.Authority = "http://localhost:5000"; options.RequireHttpsMetadata = false; options.Audience = "api1"; }); } // 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.UseAuthentication();//認證 app.UseAuthorization();//授權 app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }

??????? AddAuthentication:將身份認證服務添加到DI比配置Bearer為默認

   AddAuthentication:將身份認證服務添加到管道中,以便對主機的每次調用都將自動執行身份驗證

   AddAuthentication:添加授權中間件,以確保匿名客戶端無法訪問我們的API資源

   http://localhost:5001/identity?在瀏覽器上訪問應返回401狀態代碼。這意味著您的API需要憑據,并且現在受IdentityServer保護。

  ????????c)、所需的包

3)、創建客戶端(已控制臺的形式)

using IdentityModel.Client; using Newtonsoft.Json.Linq; using System; using System.Net.Http; using System.Threading.Tasks; namespace Client { class Program { static async Task Main(string[] args) { // Console.WriteLine("Hello World!"); var client = new HttpClient(); var disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000"); if (disco.IsError) { Console.WriteLine(disco.Error); return; } var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest { Address = disco.TokenEndpoint, ClientId = "client", ClientSecret = "aju", Scope = "api1" }); if (tokenResponse.IsError) { Console.WriteLine(tokenResponse.Error); return; } Console.WriteLine(tokenResponse.Json); Console.WriteLine("\n\n"); //call api var apiClient = new HttpClient(); apiClient.SetBearerToken(tokenResponse.AccessToken); var response = await apiClient.GetAsync("http://localhost:5001/identity"); if (!response.IsSuccessStatusCode) { Console.WriteLine(response.StatusCode); } else { var content = await response.Content.ReadAsStringAsync(); Console.WriteLine(JArray.Parse(content)); } Console.ReadLine(); } } }

????????a)、所需的包  

 4)、使用客戶端訪問Api資源  

六、參考文獻

  http://docs.identityserver.io/en/latest/index.html

掃碼關注!您將得到及時的文章推送信息

總結

以上是生活随笔為你收集整理的.Net Core 3.0 IdentityServer4 快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。

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