我的webapp是使用.NET Core开发的,并部署在Azure中 . 我已启用Azure应用服务身份验证并将其配置为使用Azure Active Directory . 当我访问webapp时,我会被重定向到正确的登录页面 . 登录后,我可以浏览到 endpoints .auth / me,看到我的用户存在声明 . 我还可以验证下面的请求标头是否存在值:
-
X-MS-TOKEN-AAD-ID-TOKEN
-
X-MS-TOKEN-AAD-ACCESS-TOKEN
-
X-MS-TOKEN-AAD-EXPIRES-ON
-
X-MS-TOKEN-AAD-REFRESH-TOKEN
但是我无法在控制器中检索这些声明 . 使用User.Identity.isAuthenticated始终为false,User.Identity.Claims为空 .
如何让用户进行身份验证并检索声明?
从理论上讲,我可以检查请求标头(X-MS-TOKEN-AAD-ID-TOKEN)是否存在然后检索 endpoints 上存在的声明.auth / me但这看起来并不是正确的方式去?
编辑:我是否绊倒了这里讨论的同一个问题? (Trouble getting ClaimsPrincipal populated when using EasyAuth to authenticate against AAD on Azure App Service in a Asp.Net Core web app)
1 回答
如Working with user identities in your application所述:
正如Chris Gillum建议您可以调用
/.auth/me
endpoints 并检索用户声明 . 您可以编写自定义中间件来检查X-MS-CLIENT-PRINCIPAL-ID
标头并调用/.auth/me
endpoints 并手动设置用户声明 . 这是详细的代码示例,您可以参考这个类似的issue .此外,您可以修改您的应用程序并显式添加身份验证中间件,而不是使用AppS身份验证/授权(EasyAuth)作为evilSnobu评论 . 对于这种方法,您可以使用ASP.Net Core OpenID Connect中间件,详细信息可以按照以下教程进行操作:
Integrating Azure AD (v2.0 endpoint) into an ASP.NET Core web app
Integrating Azure AD into an ASP.NET Core web app