【发布时间】:2020-10-15 02:56:17
【问题描述】:
我正在使用 Azure Functions 作为 API 开发 Blazor wasm。 我已经可以在客户端应用程序上通过 Azure AD B2C 对用户进行身份验证,但是在 Azure Function 中识别用户时,所有请求的 ClaimsPrincipal.Current 都会收到 null,即使用户是 在 blazor 应用中登录。
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.User, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
if (ClaimsPrincipal.Current == null || !ClaimsPrincipal.Current.Identity.IsAuthenticated)
{
log.LogInformation("Claims: Not authenticated");
}
else
{
log.LogInformation("Claims: Authenticated as " + ClaimsPrincipal.Current.Identity.Name);
}
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
我错过了什么?应用程序是否会在每次请求时自动发送令牌? 关于身份验证,我只在 program.cs 中添加了这个。
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAdB2C", options.ProviderOptions.Authentication);
});
如何确定应用程序正在发送令牌? ClaimsPrincipal 是接收令牌和用户身份的正确方法吗?
【问题讨论】:
标签: azure-functions blazor blazor-client-side