我在Azure Web App中托管了Angular4应用程序,在Azure API App中托管了.NET核心Web API。
使用Azure Active Directory保护API。目前,我使用ng2-adal获取访问令牌,我将其注入标头以执行我的API调用。
现在,我尝试使用相同的 ClientId(如API)删除ng2-adal模块并使用身份验证/授权功能保护我的Web App。当我浏览我的网站时,我被重定向到AAD登录,在我成功登录后,我被重定向到我的网站。现在,我不想在我的Web App中调用API(使用相同的ClientId进行保护),但无法找到检索令牌的方法。
在这种情况下,有没有办法在我的Angular App中检索访问令牌? 看起来令牌在AppServiceAuthSession Cookie中加密存储:
答案 0 :(得分:6)
System.identityHashCode()
是与令牌不同的Cookie。在这种情况下,您需要修改Azure应用程序的配置,以使其获取Web API的access_token。
我们可以使用Resource Explore修改如下设置:
1。找到有角度的网络应用程序
2。找到config-> authsettings(资源是Azure应用程序的clientId,用于保护您的应用程序)
AppServiceAuthSession
3。为Azure应用程序配置redirect_uri,如下所示:
"additionalLoginParams": [
"response_type=code id_token",
"resource=3fa9607b-63cc-4050-82b7-91e44ff1df38"
],
然后在登录角度应用程序后,您可以通过端点获取 access_token :
https://appfei.azurewebsites.net/.auth/login/aad/callback
然后我们需要使用高级Azure Active设置来保护Web API,如下图所示,以启用access_token可以调用Web API:
答案 1 :(得分:2)
我已经为此工作了一个星期。 所以,我想分享我是如何得到它的。
我可以使用AAD对我的应用进行身份验证。
我的Cookie存储空间中有AppServiceAuthSession。
然后在我的应用程序中,我调用了auth / me API。
https://yourwebsite.azurewebsites.net/.auth/me
所以,它就像:
this.$http
.get('https://yourwebsite.azurewebsites.net/.auth/me').then(response => {
console.log(".auth/me", response)
}, err => {
console.log("Error: ", err)
})
我实际上正在使用Vue。调用您的HTTP可能会有所不同。 就是这样。
我基本上调用了auth / me API来检索我需要的信息。
P.S。当然,您需要进行身份验证。
答案 2 :(得分:0)
找到了这个解决方案:
只需要在以下位置添加您的应用程序网址 身份验证/授权->允许外部重定向URL
所以webapi将自动接受这些cookie。
ps:确保您的ajax请求在其上传递了这些cookie。