簡體   English   中英

在標題中傳遞openid-connect oauth2 bearer token

[英]pass openid-connect oauth2 bearer token in header

背景

我已經實現了Thinktecture.IdentityServer.V3(openID Connect one)。 我已經將OAuth2持有者令牌返回到我的javascript客戶端(隱式流),格式如下:

{
  "id_token": "eyJ0eXAiOiJKV1QiLCJh...",  // JWT
  "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1Ni..", // JWT
  "token_type": "Bearer",
  "expires_in": "3600",
  "scope": "openid profile read write email",
  "state": "1299139105028949"
}

但是在所有示例中,它們僅在調用服務時將access_token傳遞給資源提供者。

 $.ajax({
         url: 'http://localhost:2727/Account/123/Get',
         headers: {
              Authorization: "Bearer " + $scope.response.access_token
             }
         })

假設

如果我有這個權利,我使用訪問令牌進行身份驗證。 然后我根據id_token中的聲明進行授權(我不想進行單獨的數據庫調用 - 我希望它完全自包含)。

我如何通過ajax將此信息傳遞給我的webapi2端點(假設我已經設置了CORS等)以及我需要連接哪些中間件來驗證它? (我猜測其中一個令牌驗證器和一個claimManager但是有很多我無法確定哪一個是正確的使用者)。

非常感謝幫助

id_token用於客戶端 - 它必須由客戶端驗證(或者如果客戶端沒有必要的加密庫,則由idsrv中的身份令牌驗證端點驗證)。 之后,您使用訪問令牌來訪問資源。

您似乎使用AngularJS,因此您可以使用$http服務在標頭中設置令牌

例如:

$http.post("/login", credentials).then(function(response) {
    $httpProvider.defaults.headers.common["Authorization"] = "Bearer " + $scope.response.access_token;
});

每次會話都必須這樣做一次。

UPDATE

使用jQuery這樣的東西

     //This repesent the token you got after login
     var authToken = {
                     "id_token": "eyJ0eXAiOiJKV1QiLCJh...",  // JWT
                     "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1Ni..", // JWT
                     "token_type": "Bearer",
                     "expires_in": "3600",
                     "scope": "openid profile read write email",
                     "state": "1299139105028949"
                     }
     $.ajax({
            url: "http://localhost:2727/Account/123/Get",
            type: "get",
            dataType: "json",
            beforeSend: function (request)
            {
                request.setRequestHeader("Authorization", authToken.token_type + " " + authToken.access_token);
            }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM