[原創(chuàng)] IdentityServer4權(quán)限控制---客戶端授權(quán)模式之IDS4認(rèn)證服務(wù)器搭建(二)
今天我們的實驗任務(wù)主要是搭建一臺IDS4用戶認(rèn)證服務(wù)器,來對我們的資源服務(wù)器(API接口服務(wù)器)的資源進行保護,開始的前提是你得有一臺資源服務(wù)器,以方便后面的測試,如果還沒有,請參看這篇文章搭建:
使用客戶端憑證保護API資源:[原創(chuàng)] IdentityServer4權(quán)限控制---客戶端授權(quán)模式之API服務(wù)器搭建(一)
廢話不多說,開始我們的正文吧。我們在電腦建立以下目錄D:\WEB\ID4\IDS4Server,然后創(chuàng)建我們的IDS4服務(wù)器,和上次的步驟差不多,我們用VS新建一個項目,選擇“ASP.NET Core WEB應(yīng)用”,然后下一步,項目名稱指定為:IDS4Server,位置指定為:D:\WEB\ID4\IDS4Server\,其它默認(rèn)。選擇.NET 6.0 ,配置HTTPS打勾。一路下來,我們的項目就創(chuàng)建好了。現(xiàn)在我們引入最關(guān)鍵的IdentityServer4包。
當(dāng)我們啟動它的時候是VS模板的默認(rèn)樣式,我們給它稍微改一下View樣式
@{ ViewData["Title"] = "IdentityServer4"; } <div class="text-center"> <h1 class="display-4">IdentityServer4</h1> <br /> <br /> <p>身份認(rèn)證服務(wù)器正在運行....</p> </div>
再刪除布局頁的頭和尾,這一步省略,大家自己修改,不是難事。接下來我們再將引入的包加載起來。打開program.cs,修改代碼如下:
using IdentityServer4.Models; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using System; using IDS4Server; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddIdentityServer() .AddDeveloperSigningCredential() //This is for dev only scenarios when you don’t have a certificate to use. .AddInMemoryApiScopes(Config.ApiScopes) .AddInMemoryClients(Config.Clients); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseDeveloperExceptionPage(); app.UseIdentityServer(); app.MapRazorPages(); app.Run();
編譯器有紅色提示,然后項目中加入Config.cs ,代碼如下:
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. using IdentityServer4.Models; using System.Collections.Generic; namespace IDS4Server { public static class Config { public static IEnumerable<ApiScope> ApiScopes => new List<ApiScope> { new ApiScope("api1", "My API") }; public static IEnumerable<Client> Clients => new List<Client> { new Client { ClientId = "client", // no interactive user, use the clientid/secret for authentication AllowedGrantTypes = GrantTypes.ClientCredentials, // secret for authentication ClientSecrets = { new Secret("secret".Sha256()) }, // scopes that client has access to AllowedScopes = { "api1" } } }; } }
按照官網(wǎng)的理解,我們是在認(rèn)證服務(wù)器上添加了一個與真實客戶端想對應(yīng)的認(rèn)證記錄,因為是演示程序,所以這里是全部加載到服務(wù)器內(nèi)存中了,真實環(huán)境下肯定是用數(shù)據(jù)庫永久化儲存來代替的,這些都不是今天的學(xué)習(xí)任務(wù),我們的目標(biāo)主要是將認(rèn)證服務(wù)器搭建起來。別忘記,我們再把認(rèn)證服務(wù)器的啟動端口改一下,最終項目結(jié)構(gòu)如下圖所示:
然后我們運行一下看看
注意,大家如果運行的是官方的demo,這一步啟動以后是沒有界面的,但是這并不代表你的部署失敗了,我們可以用這個方法檢查一下,就是在后面跟一堆網(wǎng)址,如下:https://localhost:5001/.well-known/openid-configuration 這樣,如果能看到返回的頁面,說明服務(wù)已經(jīng)正常運行沒問題了。OK,我們這節(jié)課的內(nèi)容相對比較簡單,源代碼這里下載: IdentityServer4
總結(jié)一下:我們第一節(jié)課搭建了一個含有三個API接口的服務(wù)器,當(dāng)有客戶端要訪問接口資源的時候,我們需要TOKEN去驗證客戶身份,驗證是JWT方式進行的,并且在資源服務(wù)器上指定了遠(yuǎn)程驗證的地址,也就是這節(jié)課我們搭建的IDS4驗證服務(wù)器。這兩步都準(zhǔn)備好了,我們下一節(jié)課就準(zhǔn)備一個客戶端程序Client.exe 去訪問一下我們的API資源,模擬未登錄和登錄狀態(tài)下對資源的訪問,以及驗證過程,達(dá)到資源保護的目的。感謝你耐著性子看到這里,如果你還沒有準(zhǔn)備關(guān)電腦,就請接著看我們的第三課:
[原創(chuàng)] IdentityServer4權(quán)限控制---客戶端創(chuàng)建、獲取TOKEN及訪問API資源(三)
原創(chuàng)文章,轉(zhuǎn)載請注明出處:http://www.maiyt.com/article-28