【问题标题】:Receive authentication token in Azure Function sent by Blazor wasm在 Azure Function 中接收 Blazor wasm 发送的身份验证令牌
【发布时间】: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


    【解决方案1】:

    当BaseAddressAuthorizationMessageHandler 处理程序附加到HttpClient 时,令牌由Authorization 标头中的http 请求发送。
    要使用此处理程序创建 HttpClient,您可以在 HttpClientFactory 中注册一个客户端,如下所示:

    services
        .AddHttpClient("azure-function")
        .ConfigureHttpClient(httpClient =>
        {
            var azureUri = new Uri("{path to azure}");
            httpClient.BaseAddress = azureUri;
        })
        .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
    

    并通过注入 http 客户端工厂在您的组件或服务中使用此客户端:

    @using System.Net.Http
    @inject IHttpClientFactory _factory
    
    @code {
        protected override async Task OnInitializedAsync()
        {
            var httpClient = _factory.CreateClient("azure-function");
            var result = await httpClient.GetAsyn<Result>("{endpoint path}");
        }
    }
    

    【讨论】:

    • 感谢您的回答。由于我的 blazor 是独立的 (wasm),因此我将按照此处的说明进行操作:docs.microsoft.com/en-us/aspnet/core/blazor/security/…您知道如何使用 AddMsalAuthentication 执行此操作吗?
    • @CageE 也这样做。它与帐户类型无关。
    • 绑定builder.Services.AddHttpClient("AuthorizedClient", client => client.BaseAddress = new Uri(builder.Configuration[ApiBaseAddress])) .AddHttpMessageHandler(); Fiddler 中的 Auth 标头仍然为空。
    • @CageE 提出问题
    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 2021-07-24
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多