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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

使用Identity Server 4建立Authorization Server (1)

發(fā)布時間:2023/12/4 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Identity Server 4建立Authorization Server (1) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

預(yù)備知識:?學(xué)習(xí)Identity Server 4的預(yù)備知識

本文內(nèi)容基本完全來自于Identity Server 4官方文檔:?https://identityserver4.readthedocs.io/

官方文檔很詳細的.

使用OAuth可以更安全, 這里我們的authorization server和web api 以及網(wǎng)站將分別獨立運行.?

建立authorization server

建立asp.net core 項目使用空模板.

項目建立后, 運行方式改為使用控制臺運行而不是IISExpress, 以便查看各種debug信息.

打開launchSettings.json:


{
?
"profiles": { ?
?
"AuthServer": { ? ?
?
"commandName": "Project", ?
? ?
"launchBrowser": true, ?
? ?
"environmentVariables": { ? ? ?
? ??
"ASPNETCORE_ENVIRONMENT": "Development"}, ?
"applicationUrl": "http://localhost:5000/"}} }

把IISExpress相關(guān)的內(nèi)容刪掉, 然后端口改為5000.

Program.cs里的BuildWebHost也應(yīng)該加上Url:

public static IWebHost BuildWebHost(string[] args) =>WebHost.CreateDefaultBuilder(args) ? ? ? ? ? ? ? ?.UseUrls("http://0.0.0.0:5000").UseStartup<Startup>().Build();

其實不加也好用.

運行就會彈出控制臺:

安裝Identity Server4:

打開nuget, 搜索 identityserver4:

安裝即可.

配置asp.net core 管道

打開startup.cs, 編輯Configure方法:

public void Configure(IApplicationBuilder app, IHostingEnvironment env){app.UseDeveloperExceptionPage(); ? ? ? ? ? ?app.UseIdentityServer();}

就是使用上面這個中間件.?

配置Identity Server

還是Startup.cs,編輯ConfigureServices方法:

這里不僅要把IdentityServer注冊到容器中, 還要至少對其配置三點內(nèi)容:

1. 哪些API可以使用這個authorization server.

2. 那些客戶端Client(應(yīng)用)可以使用這個authorization server.

3. 指定可以使用authorization server授權(quán)的用戶.

首先需要把上面這些做成一個配置文件:

建立Configuration/InMemoryConfiguration.cs:

namespace AuthServer.Configuration { ? ?
? ?
public class InMemoryConfiguration{ ? ?
? ? ? ?
public static IEnumerable<ApiResource> ApiResources(){ ? ? ? ?
? ? ? ?? ?
return new[]{ ? ? ? ? ? ? ? ?new ApiResource("socialnetwork", "社交網(wǎng)絡(luò)")};} ? ? ?

? ? ? ?
public static IEnumerable<Client> Clients(){ ? ? ? ? ? ?return new[]{ ? ? ? ? ? ? ? ?new Client{ClientId = "socialnetwork",ClientSecrets = new [] { new Secret("secret".Sha256()) },AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,AllowedScopes = new [] { "socialnetwork" }}};} ? ? ?

? ? ? ?
public static IEnumerable<TestUser> Users(){ ? ? ? ? ? ?return new[]{ ? ? ? ? ? ? ? ?new TestUser{SubjectId = "1",Username = "mail@qq.com",Password = "password"}};}} }

ApiResources: 這里指定了name和display name, 以后api使用authorization server的時候, 這個name一定要一致, 否則就不好用的.

Clients: Client的屬性太多了, 這里就指定幾個. 其中ClientSecrets是Client用來獲取token用的. AllowedGrantType: 這里使用的是通過用戶名密碼和ClientCredentials來換取token的方式.?ClientCredentials允許Client只使用ClientSecrets來獲取token. 這比較適合那種沒有用戶參與的api動作.?AllowedScopes: 這里只用socialnetwork

Users: 這里的內(nèi)存用戶的類型是TestUser, 只適合學(xué)習(xí)和測試使用, 實際生產(chǎn)環(huán)境中還是需要使用數(shù)據(jù)庫來存儲用戶信息的, 例如接下來會使用asp.net core identity. TestUser的SubjectId是唯一標識.

然后回到StartUp的ConfigureServices:

前一篇文章講過, 我們需要對token進行簽名, 這意味著identity server需要一對public和private key. 幸運的是, 我們可以告訴identity server在程序的運行時候?qū)@項工作進行設(shè)定: AddDeveloperSigningCredential(), 它默認會存到硬盤上的, 所以每次重啟服務(wù)不會破壞開發(fā)時的數(shù)據(jù)同步. 這個方法只適合用于identity server4在單個機器運行, 如果是production farm你得使用AddSigningCredential()這個方法.

public void ConfigureServices(IServiceCollection services) {services.AddIdentityServer().AddDeveloperSigningCredential().AddTestUsers(InMemoryConfiguration.Users().ToList()).AddInMemoryClients(InMemoryConfiguration.Clients()).AddInMemoryApiResources(InMemoryConfiguration.ApiResources()); }


然后運行一下:

沒報錯, 紅線部分是內(nèi)存配置版的一些解釋.

獲取Token

打開postman, 如果你無法安裝postman, 也無法進入Chrome商店, 那么你可以買一個海外服務(wù)器, 使用shadowsocks服務(wù)器和客戶端進行代理, 然后就可以訪問google了.

首先我們發(fā)送一個錯誤的client_id, 然后得到的結(jié)果是: invalid_client. 控制臺的信息如下:

然后我們再發(fā)送一個正確的數(shù)據(jù):

這次獲取到了token. 控制臺信息如下:

由于identity server我們設(shè)置的是?ResourceOwnerPasswordAndClientCredentials 這個GrantType, 所以使用用戶名密碼以及使用ClientCredentials都可以. 那我們把用戶名和密碼去掉, 只發(fā)送Client Credentials:

仍然獲取到了token. 控制臺上的信息與上一個稍有不同, 沒有user相關(guān)的信息了:

使用正經(jīng)的證書:

證書可以通過幾種渠道獲得, 可以購買, 可以使用IIS生成, 也可以使用Openssl這樣的工具生成證書. 我就使用openssl吧.

去openssl的windows官網(wǎng):?https://slproweb.com/products/Win32OpenSSL.html

下載 1.1.0版:?https://slproweb.com/download/Win64OpenSSL-1_1_0f.exe

安裝后, 打開命令行.

openssl req -newkey rsa:2014 -nodes -keyout socialnetwork.key -x509 -days 365 -out socialnetwork.cer

具體的信息就不管了. 這個證書的有效期是365天, 命令參數(shù)里面設(shè)定的.

這是生成的文件:

一個證書和一個key, 然后我們需要給他們倆封裝成一個文件, 以便identity server可以使用它們?nèi)フ_的簽名tokens. 這就需要使用另一個命令:

openssl pkcs12 -export -in socialnetwork.cer -inkey socialnetwork.key -out socialnetwork.pfx

這里發(fā)生了錯誤...那就使用管理員打開命令行:

輸入密碼和確認密碼后, 沒問題了.

pfx就是我們需要的文件.

然后修改一個Startup的ConfigureServices:


?public void ConfigureServices(IServiceCollection services) {services.AddIdentityServer() ? ? ? ? ? ? ? ?// .AddDeveloperSigningCredential() ? ? ? ? ? ? ? ?.AddSigningCredential(new X509Certificate2(@"D:\Projects\test\socialnetwork.pfx", "password")).AddTestUsers(InMemoryConfiguration.Users().ToList()).AddInMemoryClients(InMemoryConfiguration.Clients()).AddInMemoryApiResources(InMemoryConfiguration.ApiResources()); }


現(xiàn)在運行程序的話, 啥也不顯示. 那么接下來, 就

添加像樣的UI

Identity Server 4 提供了一套QuickStart UI :?https://github.com/IdentityServer/IdentityServer4.Quickstart.UI/tree/release

在項目根目錄打開Powershell(可以在項目根目錄, 按住shift, 點擊右鍵的Powershell)

然后輸入命令:

iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/IdentityServer/IdentityServer4.Quickstart.UI/release/get.ps1'))

然后就把UI下載到項目了.

看看生成的文件, 很多:

由于有wwwroot下很多靜態(tài)文件, 所以asp.net core 需要啟用服務(wù)靜態(tài)文件的功能: 修改Startup的Configure方法:


public void Configure(IApplicationBuilder app, IHostingEnvironment env){app.UseDeveloperExceptionPage();app.UseIdentityServer(); ? ? ? ? ? ?app.UseStaticFiles();app.UseMvcWithDefaultRoute();}


使用靜態(tài)文件, 并且使用了MVC.

別忘了在ConfigureServices里面注冊MVC:

public void ConfigureServices(IServiceCollection services){services.AddIdentityServer() ? ? ? ? ? ? ? ?// .AddDeveloperSigningCredential().AddSigningCredential(new X509Certificate2(@"D:\Projects\test\socialnetwork.pfx", "Bx@steel")).AddTestUsers(InMemoryConfiguration.Users().ToList()).AddInMemoryClients(InMemoryConfiguration.Clients()).AddInMemoryApiResources(InMemoryConfiguration.ApiResources()); ? ? ? ? ? ?services.AddMvc();}

然后運行一下試試:

它現(xiàn)在已經(jīng)具備了這些功能!

使用TestUser也可以登陸成功:

當然這個UI可以根據(jù)情況自行定義.

相關(guān)文章:

  • IdentityServer4(OAuth2.0服務(wù))折騰筆記

  • IdentityServer4 實現(xiàn) OpenID Connect 和 OAuth 2.0

  • IdentityServer4 使用OpenID Connect添加用戶身份驗證

  • IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架學(xué)習(xí)保護API

  • IdentityServer4 指定角色授權(quán)(Authorize(Roles="admin"))

  • IdentityServer4 SigningCredential(RSA 證書加密)

  • IdentityServer4 實現(xiàn)自定義 GrantType 授權(quán)模式

  • IdentityServer4 配置負載均衡

  • 學(xué)習(xí)Identity Server 4的預(yù)備知識

原文地址:http://www.cnblogs.com/cgzl/p/7780559.html


.NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關(guān)注

總結(jié)

以上是生活随笔為你收集整理的使用Identity Server 4建立Authorization Server (1)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。