繁体   English   中英

Blazor WASM Static WebApp 与 Google Auth 和 Azure 功能

[英]Blazor WASM Static WebApp with Google Auth and Azure Functions

我使用带有个人身份验证的标准 blazorwasm 模板。 我设法使用 OIDC 使用 Google 身份验证进行身份验证。

程序.cs

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped<CustomAuthorizationMessageHandler>();

builder.Services.AddOidcAuthentication(options =>
{
    builder.Configuration.Bind("google", options.ProviderOptions);
    options.ProviderOptions.DefaultScopes.Add("email");
});

builder.Services.AddHttpClient("azure-function", client => client.BaseAddress = new Uri("https://someblog.azurewebsites.net/"))
       .AddHttpMessageHandler<CustomAuthorizationMessageHandler>();

builder.Services.AddTransient(sp => sp.GetRequiredService<IHttpClientFactory>()
                                      .CreateClient("azure-function"));

await builder.Build().RunAsync();

CustomAuthorizationMessageHandler.cs

public class CustomAuthorizationMessageHandler : AuthorizationMessageHandler
{
    public CustomAuthorizationMessageHandler(IAccessTokenProvider provider, NavigationManager navigationManager)
        : base(provider, navigationManager)
    {
        ConfigureHandler(
            new[] { "https://someblog.azurewebsites.net/api/" });
    }
}

接下来,我还使用 Google 身份验证创建了一个 Azure Function 应用程序。 如果我 go 到 function URL 我被重定向到登录,然后 ZC1C4F145268E17A938 工作。

函数应用身份验证设置

函数应用 Cors 设置

I would like to know how do I call the Azure Function TestAuth from Blazor Client side because even though I'm authenticated on the Blazor App I still get a 401 for the Function App. 我需要以某种方式委托身份验证或以其他方式进行。 我不想仅使用 Google 帐户使用 AAD 或 B2C。

TestAuth.razor

@page "/testAuth"
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@inject HttpClient httpClient
@inject IAccessTokenProvider _tokenProvider

<p>test auth</p>
@if (response != null)
{
<p>@response.StatusCode</p>
<p>@response.Content.ReadAsStringAsync().GetAwaiter().GetResult()</p>
}
@code {
    private HttpResponseMessage response = null;
    protected override async Task OnInitializedAsync()
    {
        response = await httpClient.GetAsync("TestAuth?code=IHBjq3cyHxyg3cQVKSTl0pnIFzff093//PE9gavHD5MWjDoUeB5vGA==");
    }
}

据我了解,因为我添加了CustomAuthorizationMessageHandler它应该将承载令牌附加到对我的授权 URL 发出的请求,即 Function 应用程序,但是查看开发工具我看不到正在添加的令牌

请求标头

我错过了一些简单的东西吗? 任何指针将不胜感激。

我认为这与您的代码无关,而是您配置身份验证流程的方式应该关注。 当 Google 发布令牌时,受众将静态添加到返回的令牌中。 In your case, Google returns a token for your application not for your Azure Function, thats why you cannot use it for your Azure Function.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM