Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在Blazor WASM Standalone应用程序上使用Google授权登录或获取访问令牌_C#_Asp.net Core_Blazor_Openid Connect_Blazor Webassembly - Fatal编程技术网

C# 无法在Blazor WASM Standalone应用程序上使用Google授权登录或获取访问令牌

C# 无法在Blazor WASM Standalone应用程序上使用Google授权登录或获取访问令牌,c#,asp.net-core,blazor,openid-connect,blazor-webassembly,C#,Asp.net Core,Blazor,Openid Connect,Blazor Webassembly,我在使用谷歌OIDC作为我的Blazor WASM独立应用程序的身份提供商登录时遇到问题。这是我的appsettings.json。我当前将响应类型设置为code。这就是导致它破裂的原因。如果我将其设置为id\u token,我可以登录,但无法在我的组件中获取访问令牌 "Local": { "Authority": "https://accounts.google.com/", "ClientId"

我在使用谷歌OIDC作为我的Blazor WASM独立应用程序的身份提供商登录时遇到问题。这是我的appsettings.json。我当前将
响应类型设置为
code
。这就是导致它破裂的原因。如果我将其设置为
id\u 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();
    }

}
@使用Microsoft.AspNetCore.Components.WebAssembly.Authentication;
@第页“/项目”
@注入AuthenticationStateProvider\u AuthenticationStateProvider
@注入IAccessTokenProvider\u tokenProvider
项目
@代码{
受保护的重写异步任务OnInitializedAsync()
{
var authstate=wait_authenticationStateProvider.getauthenticationstatesync();
var accessTokenResult=wait_tokenProvider.RequestAccessToken();
}
}
Program.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();
    }
}
公共类程序
{
公共静态异步任务主(字符串[]args)
{
var builder=WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add(“#app”);
addScope(sp=>newHttpClient{BaseAddress=newURI(builder.HostenEnvironment.BaseAddress)});
builder.Services.AddOidcAuthentication(选项=>
{
//在此处配置身份验证提供程序选项。
//有关详细信息,请参阅https://aka.ms/blazor-standalone-auth
builder.Configuration.Bind(“Local”、options.ProviderOptions);
});
等待builder.Build().RunAsync();
}
}

问题在于您为
响应类型设置的值。OIDC允许使用混合流和多种类型。如果希望能够使用IAccessTokenProvider访问访问令牌,则需要将
令牌添加到响应类型中

以下工作将起作用:

 "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"
  }
有关此问题的更多信息,请查看以下答案: