【发布时间】:2022-11-18 00:04:42
【问题描述】:
我们有一个静态 Web 应用程序,以及一个关联的 C# 函数应用程序(使用自带函数又名“链接后端”方法)。静态 Web 应用程序和函数应用程序都与相同的 Azure AD 应用程序注册相关联。
当我们使用 Azure AD 进行身份验证并转到静态 Web 应用程序中的身份验证端点时:/.auth/me,我们看到:
{
"clientPrincipal": {
"identityProvider": "aad",
"userId": "d9178465-3847-4d98-9d23-b8b9e403b323",
"userDetails": "johnny_reilly@hotmail.com",
"userRoles": ["authenticated", "anonymous"],
"claims": [
// ...
{
"typ": "http://schemas.microsoft.com/identity/claims/objectidentifier",
"val": "d9178465-3847-4d98-9d23-b8b9e403b323"
},
{
"typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress",
"val": "johnny_reilly@hotmail.com"
},
{
"typ": "name",
"val": "John Reilly"
},
{
"typ": "roles",
"val": "OurApp.Read"
},
// ...
{
"typ": "ver",
"val": "2.0"
}
]
}
}
注意那里的声明。其中包括我们针对 Azure AD 应用程序注册配置的自定义声明,例如带有 OurApp.Read 的角色。
这样我们就可以在静态 Web 应用程序(前端)中成功访问声明。但是,关联的 Function App 确实不是有权访问索赔。
可以通过在我们的 Azure Function App 中实现一个显示角色的函数来看到这一点:
[FunctionName("GetRoles")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "GetRoles")] HttpRequest req
)
{
var roles = req.HttpContext.User?.Claims.Select(c => new { c.Type, c.Value });
return new OkObjectResult(roles);
}
当访问这个 /api/GetRoles 端点时,我们会看到:
[
{
"Type": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
"Value": "d9178465-3847-4d98-9d23-b8b9e403b323"
},
{
"Type": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
"Value": "johnny_reilly@hotmail.com"
},
{
"Type": "http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
"Value": "authenticated"
},
{
"Type": "http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
"Value": "anonymous"
}
]
乍一看,这似乎很棒;我们有索赔!但当我们再次审视时,我们意识到我们拥有的权利比我们希望的要少得多。至关重要的是,我们的自定义声明/应用程序角色(如 OurApp.Read)丢失了。
【问题讨论】:
标签: azure azure-active-directory azure-functions claims azure-static-web-app