How to get access_token from Identity Server hitting token endpoint, passing credentials from react client using fetch?

て烟熏妆下的殇ゞ 提交于 2019-12-24 11:05:49

问题


I need to get access token from Identity Server through an API call from react client (fetch). I don't want to load the login UI from Identity Server (implicit flow), enter credentials, redirect to the redirect_uri and then get the access_token from the url. I just want to pass the credentials through an API call (fetch) to Token endpoint and get the access token from react client (similar to http://docs.identityserver.io/en/release/endpoints/token.html).

The endpoint is - http://localhost/identityserver/core/connect/token

What are all the other data should I pass to the fetch call?

Following are the response and grant types supported by ids:

response_types_supported: [ "code", "token", "id_token", "id_token token", "code id_token", "code token", "code id_token token" ], grant_types_supported: [ "authorization_code", "client_credentials", "password", "refresh_token", "implicit" ],

May I know how to achieve this using oidc-client package(https://www.npmjs.com/package/oidc-client)? Please let me know if more details are needed so that I can update the question with more information.


回答1:


You would need to post to the Token Endpoint using the Password grant type:

POST /connect/token

client_id=yourclientid& client_secret=yourclientsecret& grant_type=password& username=yourusername&password=yourusernamespassword

This will return an Access Token not an Identity Token. If you need access to the user's information then you can obtain this from the UserInfo Endpoint.

oidc-client helps authenticating a user via the Authorize Endpoint and therefore can't help with the Token Endpoint

This is what the documentation says about the Password Grant:

The spec recommends using the resource owner password grant only for “trusted” (or legacy) applications. Generally speaking you are typically far better off using one of the interactive OpenID Connect flows when you want to authenticate a user and request access tokens.



来源:https://stackoverflow.com/questions/52552274/how-to-get-access-token-from-identity-server-hitting-token-endpoint-passing-cre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!