IdentityServer4之Client Credentials(客戶端憑據許可)
IdentityServer4之Client Credentials(客戶端憑據許可)
參考
認證服務端配置
認證服務ApiResource配置
new ApiResource("api1", "api專案 一") { ApiSecrets = { new Secret("api1pwd".Sha256()) } },
認證服務Client配置
//client credentials client new Client { ClientId = "client", // no interactive user, use the clientid/secret for authenticationAllowedGrantTypes = GrantTypes.ClientCredentials,
//Jwt = 0;Reference = 1支援撤銷; AccessTokenType = AccessTokenType.Reference, // secret for authentication ClientSecrets = { new Secret("secret".Sha256()), new Secret("abc".Sha256()) }, // scopes that client has access toAllowedScopes = { "api1" } },
認證服務Startup配置
// configure identity server with in-memory stores, keys, clients and scopes services.AddIdentityServer() .AddDeveloperSigningCredential() .AddInMemoryApiResources(Config.GetApiResources()) .AddInMemoryClients(Config.GetClients());
配置完成啟動訪問http://localhost:5000/.well-known/openid-configuration
資源服務Api配置
資源伺服器Startup配置
services.AddMvcCore() .AddAuthorization() .AddJsonFormatters(); services.AddAuthentication("Bearer") .AddIdentityServerAuthentication(options => { options.Authority = "http://localhost:5000"; options.RequireHttpsMetadata = false; options.ApiName = "api1"; options.ApiSecret = "api1pwd"; //對應ApiResources中的金鑰 });
新增介面
[Route("[controller]")] [Authorize] public class IdentityController : ControllerBase { [HttpGet] public IActionResult Get() { var info = from c in User.Claims select new { c.Type, c.Value }; var list = info.ToList(); list.Add(new { Type = "api1返回", Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }); return new JsonResult(list); } }
Client客戶端
客戶端與授權伺服器進行身份驗證並向令牌端點請求訪問令牌。
授權伺服器對客戶端進行身份驗證,如果有效,頒發訪問令牌。
//credentials client private void btnAuth_Click(object sender, EventArgs e) { // discover endpoints from metadata Task<DiscoveryResponse> discoTask = DiscoveryClient.GetAsync(txtCCAuthSer.Text); discoTask.Wait(); var disco = discoTask.Result; if (disco.IsError) { MessageBox.Show(disco.Error, "提示資訊", MessageBoxButtons.OK, MessageBoxIcon.Information); txtCCResult.Text = string.Empty; txtAccessToken.Text = string.Empty; return; } // request token var tokenClient = new TokenClient(disco.TokenEndpoint, txtCCClient.Text, txtCCSecret.Text); Task<TokenResponse> tokenTask = tokenClient.RequestClientCredentialsAsync(txtCCScopes.Text); tokenTask.Wait(); var tokenResponse = tokenTask.Result; if (tokenResponse.IsError) { MessageBox.Show(tokenResponse.Error, "提示資訊", MessageBoxButtons.OK, MessageBoxIcon.Information); txtCCResult.Text = string.Empty; txtAccessToken.Text = string.Empty; return; } txtCCResult.Text = tokenResponse.Json.ToString(); txtAccessToken.Text = tokenResponse.AccessToken; }
呼叫Api
private void btnCallApi_Click(object sender, EventArgs e) { // call api var client = new HttpClient(); client.SetBearerToken(txtAccessToken.Text); var responseTask = client.GetAsync(txtCCApiUrl.Text); responseTask.Wait(); var response = responseTask.Result; if (!response.IsSuccessStatusCode) { MessageBox.Show(response.StatusCode.ToString(), "提示資訊", MessageBoxButtons.OK, MessageBoxIcon.Information); txtApiResult.Text = string.Empty; } else { var contentTask = response.Content.ReadAsStringAsync(); contentTask.Wait(); var content = contentTask.Result; txtApiResult.Text = JArray.Parse(content).ToString(); } }
獲取token過程解析
Jwt形式獲取access_token
通過IdentityModel傳送請求
監聽請求資料
客戶端身份驗證兩種方式
1、Authorization: Basic czZCaGRSa3F0Mzo3RmpmcDBaQnIxS3REUmJuZlZkbUl3。
POST /connect/token HTTP/1.1 Connection: Keep-Alive Content-Type: application/x-www-form-urlencoded Accept: application/json Authorization: Basic Y2xpZW50OnNlY3JldA== Expect: 100-continue Host: localhost:5000 MS-ASPNETCORE-TOKEN: 08de58f6-58ee-4f05-8d95-3829dde6ae09 X-Forwarded-For: [::1]:12112 X-Forwarded-Proto: http Content-Length: 40 grant_type=client_credentials&scope=api1
2、client_id(客戶端標識),client_secret(客戶端祕鑰)。
Reference形式獲取access_token
將client的AccessTokenType設定為1
再次獲取的access_token不包含Claim資訊。
此時獲取的access_token(加密後)對應PersistedGrants表中的key
呼叫Api資源服務過程解析
Jwt形式獲取access_token呼叫Api
監聽請求資料
api資源服務驗證Jwt形式token會去認證伺服器獲取一次配置資訊。
Reference形式獲取access_token呼叫Api
監聽請求資料
api資源服務驗證Reference形式token每次(可配置快取)會去認證伺服器獲取資訊。參考
相關推薦
IdentityServer4之Client Credentials(客戶端憑據許可)
IdentityServer4之Client Credentials(客戶端憑據許可) 參考 認證服務端配置 認證服務ApiResource配置 new ApiResource("api1", "api專案 一") { ApiSecrets = { new Secret("api1pwd".
IdentityServer4(7)- 使用客戶端認證控制API訪問(客戶端授權模式)
一.前言 目前官方的文件和Demo以及一些相關元件全部是.net core 1.1的,應該是因為目前IdentityServer4目前最新版本只是2.0.0 rc1的原因,官方文件和Demo還沒來更新。我準備使用的是.net core 2.0 所支援的IdentityServer4 2.0.0,官方文件及De
Spring Cloud Config 之svn配置倉庫與動態重新整理(客戶端自動重新整理)
終於進入正文了,上篇完成了手動重新整理,貌似這種手動重新整理不是太好,因為你怎麼知道什麼時候去更新配置?我們的初衷是隻要配置檔案目錄下的檔案改變了,就要重新載入配置,也就是在svn倉庫的config目錄下的配置檔案改動自動重新整理 目前來看就兩個問題: 1:我怎麼確定是哪個
IdentityServer4 之Client Credentials走起來
### 前言 API裸奔是絕對不允許滴,之前專門針對這塊分享了jwt的解決方案([WebApi介面裸奔有風險](http://mp.weixin.qq.com/s?__biz=MzU1MzYwMjQ5MQ==&mid=2247484105&idx=1&sn=9322333e9fc39
Python——socketserver編程(客戶端/服務器)
put mixin cpc 包含 self. nec recv 實例 server 一、socketserver是標準庫中的高級模塊,它的目標是簡化很多多樣板代碼,是創建網絡客戶端和服務器所必須的代碼。(事件驅動) 二、模塊類 BaseServer :包含核心服務器功能和m
叢集HDFS檔案操作(客戶端開發java)
叢集HDFS檔案操作(客戶端開.發java) 1 環境 1.1 ==win10專業版下編譯過的hadoop jar包== 1.2 配置HADOOP_HOME環境變數 1.3 配置Path環境變數 2.測試程式
live555的一點bug修改(客戶端和伺服器)
客戶端主要出錯資訊: MultiFramedRTPSource error: Hit limit when reading incoming packet over TCP. Increase \"MAX_PACKET_SIZE\" RTCPInstance error:
netty做服務端支援ssl協議實現websocket的wss協議(客戶端為瀏覽器)
也是在網上查的資料,整理一下相互學習下第一步:生成SSL證書: 因為是測試,直接使用jdk自帶的keytool工具生成自簽名證書(注:自簽名證書是不被瀏覽器認可的,只能用於測試), --開啟cmd --輸入命令(複製啊):keytool -genkey -ke
gRPC負載均衡(客戶端負載均衡)
### 前言 [上篇](https://bingjian-zhu.github.io/2020/05/14/etcd%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E5%8F%91%E7%8E%B0/)介紹瞭如何使用`etcd`實現服務發現,本篇將基於etcd的服務發現前提下,介紹如
伺服器已拒絕客戶端憑據 the server has rejected the client credentials
我們的WinForm程式同構WCF與部署在伺服器上的服務端相連。今天很多WinForm使用者反映,在使用的時候遇到問題,丟擲異常伺服器已拒絕客戶端憑據,the server has rejected the client credentials. 現在說一說WCF的安全認證
asp.net core IdentityServer4 實現 Client credentials(客戶端憑證)
前言 OAuth 2.0預設四種授權模式(GrantType) 授權碼模式(authorization_code) 簡化模式(implicit) 密碼模式(resource owner password credentials) 客戶端模式(client_credentials) 本章主要介紹客戶端模
(九)springmvc之json的數據請求(客戶端發送json數據到服務端)
index.jsp null 字符串 n-2 func mda 客戶 請求 spring index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncodi
IdentityServer4之Resource Owner Password Credentials(資源擁有者密碼憑據許可)
cti -m error .text json conf ret logs send .h2cls { background: #6fa833 none repeat scroll 0 0 !important; color: #fff; font-family: "微軟雅
【轉】文件下載之斷點續傳(客戶端與服務端的實現)
http協議 當前時間 end box [] ada demo 服務端 sem 【轉】文件下載之斷點續傳(客戶端與服務端的實現) 【轉】文件下載之斷點續傳(客戶端與服務端的實現) 前面講了文件的上傳,今天來聊聊文件的下載。 老規矩,還是從最簡單粗暴的開始。那麽多簡單算簡單
Android專案開發筆記之登入註冊模組實現(客戶端+服務端)
寫在前面 斷斷續續開發了幾個月的App終於告一段落,雖然它可能還很不完美,不過作為上手Android的第一個完整專案,確實從中學到了蠻多,所以開個系列記錄一下~本篇先從基本上每個App都會有的登入註冊講起,包含自動登入、記住密碼功能的實現=w= 實現
Windows C語言 Socket程式設計 client端(客戶端)--斷線重連版
瞭解了最基礎的C語言客戶端的編寫流程,稍稍加以改動即可實現斷線重連。 當伺服器掉線時,客戶端會以固定的頻率不停的重連。 #include <stdio.h> #include <winsock2.h> #pragma comme
WebSphere MQ Java 應用開發簡單例項(上篇:客戶端模式開發)(client mode和binding mode)
1-背景知識 IBM MQ支援多種語言開發,本文主要是考慮Java應用開發,MQ提供了相關的Java類庫,可以很方便整合到Java應用中。 IBM MQ Java相關類庫允許Java應用直接與佇列管理器互動,或者連線MQ伺服器和客戶端進行互動。 類庫主要有
Qt學習之路十三—— 再談TCP/IP(多客戶端連線伺服器)
一、TCP和UDP的區別這裡我會用一個表格來顯示這兩者的區別比較項TCPUDP是否連線面向連線無連線傳輸是否可靠可靠不可靠流量控制提供不提供工作方式全雙工可以是全雙工應用場合大量資料少量資料速度慢快二、incomingConnection函式這個函式和之前講過的newConn
Spring Cloud Config 之svn配置倉庫與動態重新整理(客戶端)
Controller類 @RestController public class MyController { //載入application-test.properties的name屬性注入 @Value("${name}") private String name; @RequestMapp
python實現ftp(客戶端)
index 登錄 容器 socket per add gbk 默認值 interact 該文檔為用python3實現ftp上傳下載等功能。 1 import optparse 2 import socket 3 import json,os 4 import sh