【问题标题】:How to get access_token, id_token from authorize endpoint of IdentityServer4?如何从 IdentityServer4 的授权端点获取 access_token、id_token?
【发布时间】:2017-05-19 02:16:44
【问题描述】:

这个问题实际上是我的this SO question的一个连续问题。我正在尝试使用授权代码流从 Identityserver4 获取 access_token 和 id_token。

但是,如果我尝试访问“授权”端点,则会收到 405(不允许方法)HTTP 错误。

HTTP GET 请求

http://localhost:2000/connect/authorize?
client_id=client  
&client_secret=secret
&grant_type=authorization_code
&username=admin
&password=admin
&response_type=id_token+token
&scope=openid+profile+offline_access

客户:

new Client
{
  ClientId = "client", 
  ClientSecrets = { new Secret("secret".Sha256())},                   
  AllowedGrantTypes = new List<string> { "authorization_code" },
  AccessTokenType = AccessTokenType.Jwt,
  AllowedScopes = { StandardScopes.OpenId.Name, "api1" }
}

用户:

  new InMemoryUser
  {
    Subject = "1",
    Username = "admin",
    Password = "admin"
  }

我的问题是,如何调用授权端点来获取 access_token 和 id_token?我的“客户端”和“用户”配置有什么问题?

【问题讨论】:

  • 您是否启用了日志记录?这将帮助您发现许多配置问题。

标签: asp.net-core-1.0 openid-connect identityserver4


【解决方案1】:

两个想法:

  1. HTTP 405 错误可能是由 Web 浏览器的 same origin policy 引起的。但是,您的客户端看起来像一个机密客户端,而不是基于浏览器的客户端,这意味着相同的来源策略不适用,除非您错误地通过 Web 浏览器发出该请求。

  2. 当您使用不允许的 HTTP 动词时,也会发生 HTTP 405 错误。例如,如果您在 URL 仅允许 GET 时使用 POST。确保 100% 确定您正在发出 GET 请求。

【讨论】:

    【解决方案2】:

    您有几个问题。您正在混合多个流程。

    1) 如果您想从授权端点(而不是令牌端点)返回 id_token,您需要使用混合流...而不是授权码流。见here。因此,您需要相应地更改响应类型。如果您的客户是 SPA,您可以使用隐式流并从 Authorize 端点获取 id_token 和 access_token - 但不是授权代码流。

    2) client_secret 不是授权端点的参数。 grant_type 也不是。有效参数见here。

    3) 在任何情况下,您都不会将用户名和密码发送到授权端点。如果您使用的是资源所有者流程,您会将它们发送到令牌端点 - 但永远不会授权。请参阅上面的链接以及有效参数的说明。

    因此您可以切换到混合流并将您的代码更改为:

    http://localhost:2000/connect/authorize?
    client_id=client  
    &redirect_uri=<add redirect uri>
    &response_type=code+id_token+token
    &scope=openid+profile+api1
    &state=...
    

    此调用的响应将包括id_token 和access_token。

    new Client
    {
        ClientId = "client",
        ClientName = "Your Client",
        AllowedGrantTypes = GrantTypes.Hybrid,
    
        ClientSecrets =
        {
            new Secret("secret".Sha256())
        },
    
            RedirectUris           = { "<add redirect uri>" },
            PostLogoutRedirectUris = { "<add post logout redirect uri>" },
    
        AllowedScopes =
        {
            IdentityServerConstants.StandardScopes.OpenId,
            IdentityServerConstants.StandardScopes.Profile,
            "api1"
        }
    };
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 2017-05-16
    • 2017-04-04
    • 2017-05-19
    • 2017-03-24
    相关资源
    最近更新 更多