首页 文章

如何通过“password”grant_type从identityserver4获取id_token和access_token?

提问于
浏览
9

我正在尝试使用identityserver4构建身份提供程序应用程序;目前,我正在使用“资源所有者密码凭据”流,它从令牌 endpoints 返回access_token和refresh_token .

Code Snippet for calling TokenEndpoint from Client

var tokenClient = new TokenClient(<TokenEndpoint>, <ClientId>, <ClientSecret>);           
var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync(<UserName>, <password>, <Scopes>);

我的问题是,如何通过使用相同的“资源所有者密码凭据”流程获得“id_token”以及“access_token”和“refresh_token”?

1 回答

  • 12

    如何使用相同的“资源所有者密码凭据”流程获取“id_token”以及“access_token”和“refresh_token”?

    你没有 .

    在IdentityServer4中,资源所有者密码凭据流仅提供访问令牌 . 如果您还需要id令牌,请使用授权码流,隐式代码流或混合流 .

    access_token   id_token   refresh_token
    
    Resource Owner Password Credentials        yes           -           yes
    
    Authorization Code                         yes          yes          yes 
    
    Implicit Flow                              yes          yes           -
    

    由于您需要所有三种令牌类型,并且由于您似乎使用服务器端代码,因此授权码流最适合 . Some kinds of Hybrid Flow也适合你 .

    From the docs

    OAuth 2.0资源所有者密码授予允许客户端向令牌服务发送用户名和密码,并获取代表该用户的访问令牌 .

    From a GitHub issue:

    OpenID Connect不指定资源所有者流 - 仅授权服务器上的交互式登录(如代码或隐式流) . 所以[换句话说,]没有身份令牌 - 只能访问令牌 .

相关问题