【发布时间】:2021-08-26 00:25:37
【问题描述】:
我想调用 api,并在函数中根据用户的角色决定显示/返回的信息级别。 有人可以举例说明如何在 Azure 静态 Web 应用上的 Azure Function 中获取登录用户的角色吗?
通过“Function App”部署 Azure Function 时,我可以获取角色和当前用户名,但使用“Static Web App”我还没有弄清楚。
namespace Function1
{
public class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ClaimsPrincipal principal)
{
IEnumerable<string> roles = principal.Claims.Where(e => e.Type.Equals("roles")).Select(e => e.Value);
string name = principal.Identity.Name;
string responseMessage = $"Hello, {name}. This HTTP triggered function executed successfully. {string.Join(',', roles)}";
return new OkObjectResult(responseMessage);
}
}
}
【问题讨论】:
标签: azure azure-static-web-app