gpt4 book ai didi

asp.net-core - ASP .Net Core 谷歌身份验证

转载 作者:行者123 更新时间:2023-12-02 03:05:34 29 4
gpt4 key购买 nike

我在 .net core Web api 应用程序上遇到 google 身份验证问题。我的用例很简单,从 google 获取不记名 token ,将 token 作为“Bearer {token}”放入授权 header 中,然后调用我的 Web api。

但我无法让它发挥作用。当我从谷歌获得以下网址的 token 后:

https://accounts.google.com/o/oauth2/v2/auth?scope=email%20openid&include_granted_scopes=true&state=some_test_state&redirect_uri=http%3A%2F%2Flocalhost%3A53512&response_type=token&client_id={someClientID}

我将使用 header 调用我的 api:

Authorization: Bearer {TokenValue}

但每次我都会收到 401 Unauthorized。

这是我的启动类:

public static IConfigurationRoot Configuration { get; private set; }

// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

// Pull in any SDK configuration from Configuration object
services.AddDefaultAWSOptions(Configuration.GetAWSOptions());

// Add S3 to the ASP.NET Core dependency injection framework.
services.AddAWSService<Amazon.S3.IAmazonS3>();

IocConfig.Configure(services);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddLambdaLogger(Configuration.GetLambdaLoggerOptions());

var googleOptions = new GoogleOptions
{
AuthenticationScheme = "Google",
ClientId = "clientid",
ClientSecret = "cs",
SignInScheme = "Google"
};

app.UseGoogleAuthentication(googleOptions);
app.UseDeveloperExceptionPage();
app.UseMvc();
}

最佳答案

这是因为您的身份验证方案是“Google”,但如果您想使用不记名 token ,您需要将其添加到您的startup.cs

  services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
// here's your options
})

并使用此身份验证方案而不是“Google”

关于asp.net-core - ASP .Net Core 谷歌身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44566998/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com