首页 文章

在 Headers 中传递openid-connect oauth2 bearer token

提问于
浏览
3

Background

我已经实现了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
             }
         })

Assumption

如果我有这个权利,我使用访问令牌进行身份验证 . 然后我根据id_token中的声明进行授权(我不想进行单独的数据库调用 - 我希望它完全自包含) .

Question

我如何通过ajax将此信息传递给我的webapi2 endpoints (假设我已经设置了CORS等)以及我需要连接哪些中间件来验证它? (我猜测其中一个令牌验证器和一个claimManager但是有很多我无法确定哪一个是正确的使用者) .

非常感谢帮助

2 回答

  • 1

    id_token用于客户端 - 它必须由客户端验证(或者如果客户端没有必要的加密库,则由idsrv中的身份令牌验证 endpoints 验证) . 之后,您使用访问令牌来访问资源 .

  • 3

    您似乎使用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);
                }
        });
    

相关问题