How to protect Asp.net core 2.1 and Vue.js single page application with Identity Server 4

…衆ロ難τιáo~ 提交于 2021-02-10 14:35:23

问题


Actually I am having a small confusion related to architect as I have 2 separated application and 1 Identity Server4 Application, one of AspNet core 2.1 API solution which is running on different domain and server and same with other 2. Now I have below points:

  1. Have added my Client Application (which is Vue.js SPA) as client in Identity Server4 with Implicit mode as it is Js client
  2. Do I need to add The API application as a client in IDP Server.
    1. If yes then How to call any API from VUE.js with Axios as when login from Vue.js application it will get the Token for this client and that will be used to show hide the button or my section of the web page.
    2. I am bit confused that should I use the Claims which i got for the vue.js client to validate the API also.
    3. If I register the API a client in Identity Server 4 the How to invoke the api from the vue.js client Application.

回答1:


  1. You should add the API as an ApiResource not as a Client and then register it in the Startup. You can check the quickstart for JS application where everything is configured here : https://github.com/IdentityServer/IdentityServer4.Samples/tree/master/Quickstarts/6_JavaScriptClient .

  2. In the javascript client you need to call the Identity Server and authenticate. There's a library that does this for you: https://github.com/IdentityModel/oidc-client-js . It is also used in the quickstart so you can see how it is done.

  3. On every call to the API the authorization header must be set to "Bearer " with the access token you got from the Identity Server. Also don't forget to setup Jwt Bearer Authentication in the API.

There is example code for everything I said in the Quickstart I mentioned before.



来源:https://stackoverflow.com/questions/55210705/how-to-protect-asp-net-core-2-1-and-vue-js-single-page-application-with-identity

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