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

标签 asp.net-core oauth-2.0 .net-core

我在 .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/

相关文章:

linux - 使用 FTP 在 Azure Web 应用程序上部署 ASP.NET Core 应用程序

c# - 停止自定义 ASP.NET Core 配置提供程序中的 SqlDependency

python - Google App Engine 中的 OAuth 2.0 提供程序。没有谷歌帐户 - Python

.net-core - 在 .net Framework 而不是 .netcore 中创建 Microsoft Bot 项目?

c# - .net core在linux平台哪里搜索证书

c# - 我们可以将访问 token 设置为不过期吗?

c# - 如何在 .NET Core 2.2 WebApi 和 Azure AD 2.0 中配置 Swagger?

c# - .Net 核心身份和 Azure AD 身份验证

spring - OAuth2 能否用于非社交登录类型的 token 生成?

oauth-2.0 - 用没有客户端密码的授权码替换 OAuth2 隐式授权