【问题标题】:Introspection of token using offline_access scope in identityserver4在 identityserver4 中使用 offline_access 范围内省令牌
【发布时间】:2019-10-11 16:06:08
【问题描述】:

在使用自己定义的范围时,我设法自省了访问令牌。我只需要使用基本授权发布到端点,用户名是我的范围名称,密码是我的范围 api 密码。但是offline_access是IdentityServer定义的默认范围,我该如何自省呢?有什么方法可以使用 api 密钥自定义 offline_access 范围?我需要使用 offline_access,因为只有这个范围才能为我提供刷新令牌。

【问题讨论】:

    标签: .net identityserver4 openid-connect


    【解决方案1】:

    我找到了一种内省offline_access 范围令牌的方法。我希望这可以帮助像我一样坚持的人。说一下我的代码

        public static IEnumerable<ApiResource> GetApis()
        {
            return new List<ApiResource>
            {
                new ApiResource("api1", "My API")
                {
                    ApiSecrets = { new Secret("secret".Sha256()) }
                }
            };
        }
    

    我的客户代码是

        public static IEnumerable<Client> GetClients()
        {
            return new List<Client>
            {
                // resource owner password grant client
                new Client
                {
                    ClientId = "ro.client",
                    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
    
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    AllowedScopes = new List<string>
                    {
                        "api1",
                       IdentityServerConstants.StandardScopes.OfflineAccess,
                    },
                    AllowOfflineAccess = true,
                    AccessTokenType = AccessTokenType.Reference, // revocation endpoint work with reference access token only
                }
            };
        }
    

    请求访问令牌时,将 api1 和 offline_access 一起包含,

    请求刷新令牌时,

    进行自省时,注意用户名api1和密码secret将在基本授权期间使用

    在撤销引用令牌或刷新令牌时,请注意用户名 ro.client 和密码 secret 将在基本授权期间使用,

    【讨论】:

      【解决方案2】:

      你可以像这样创建一个刷新令牌。

      创建您的声明。

      var issuer = HttpContext.GetIdentityServerIssuerUri();
      
                      var tokenObj = new Token
                      {
                          Issuer = issuer,
                          CreationTime = DateTime.UtcNow,
                          Lifetime = client.AccessTokenLifetime,
                          ClientId = "9a7519a1e0224b18bb28d0fe0a00d038",
                          Claims = claims,
                          AccessTokenType = AccessTokenType.Reference,
                      };
      
      var client = _clientStore.FindClientByIdAsync("{clientId}").Result;
      
      var refereshToken = _refreshTokenService.CreateRefreshTokenAsync(claimsPrincipal, tokenObj, client).Result;
      

      IRefreshTokenService _refreshTokenService; IClientStore _clientStore;

      这两个都被注入到构造函数中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-26
        • 1970-01-01
        • 2018-06-25
        • 2019-12-23
        • 2021-02-19
        • 2021-10-04
        • 1970-01-01
        • 2017-10-25
        相关资源
        最近更新 更多