簡體   English   中英

無法在 Blazor WASM 獨立應用程序上使用 Google 授權登錄或獲取訪問令牌

[英]Cannot log in or get access token with Google Authorization on Blazor WASM Standalone app

我在使用 Blazor WASM 獨立應用程序作為身份提供者使用 Google OIDC 登錄時遇到問題。 這是我的 appsettings.json。 我目前將ResponseType設置為code 這就是導致它破裂的原因。 如果我將它設置為id_token我可以登錄,但無法在我的組件中獲取訪問令牌。

 "Local": {
    "Authority": "https://accounts.google.com/",
    "ClientId": "4....apps.googleusercontent.com",
    "PostLogoutRedirectUri": "https://localhost:44380/authentication/logout-callback",
    "RedirectUri": "https://localhost:44380/authentication/login-callback",
    "ResponseType": "code"
  }

我的組件:

@using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
@page "/projects"
@inject AuthenticationStateProvider _authenticationStateProvider
@inject IAccessTokenProvider _tokenProvider
<h3>Projects</h3>

@code {

    protected override async Task OnInitializedAsync()
    {
        var authstate = await _authenticationStateProvider.GetAuthenticationStateAsync();
        var accessTokenResult = await _tokenProvider.RequestAccessToken();
    }

}

程序.cs

public class Program
{
    public static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
        builder.RootComponents.Add<App>("#app");

        builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

        builder.Services.AddOidcAuthentication(options =>
        {
            // Configure your authentication provider options here.
            // For more information, see https://aka.ms/blazor-standalone-auth
            builder.Configuration.Bind("Local", options.ProviderOptions);
            
        });

        await builder.Build().RunAsync();
    }
}

問題在於您為ResponseType設置的值。 OIDC 允許使用混合流和多種類型。 如果您希望能夠使用 IAccessTokenProvider 訪問訪問令牌,則需要將token添加到您的響應類型。

以下將起作用:

 "Local": {
    "Authority": "https://accounts.google.com/",
    "ClientId": "4....apps.googleusercontent.com",
    "PostLogoutRedirectUri": "https://localhost:44380/authentication/logout-callback",
    "RedirectUri": "https://localhost:44380/authentication/login-callback",
    "ResponseType": "id_token token"
  }

有關此問題的更多信息,請查看此答案: OpenIDConnect 響應類型混淆

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM