如何从IdentityServer4的授权端点获取access_token,id_token?

问题描述 投票:1回答:2

这个问题实际上是this问题的连续问题。我试图通过使用授权代码流从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
2个回答
1
投票

两个想法:

  1. HTTP 405错误可能是由于Web浏览器的same origin policy。您的客户端看起来像机密客户端而不是基于浏览器的客户端,这意味着相同的源策略不适用,除非您错误地通过Web浏览器发出该请求。
  2. 当您使用不允许的HTTP谓词时,也会发生HTTP 405错误。例如,如果您在URL仅允许POST时使用GET。 100%确定您正在制作GET请求。

1
投票

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

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

2)client_secret不是Authorize端点的参数。也不是grant_type。有关有效参数,请参阅here

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

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

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_tokenaccess_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"
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.