【问题标题】:ASP .Net Core Google AuthenticationASP .Net Core Google 身份验证
【发布时间】:2017-06-15 12:02:45
【问题描述】:

我的 .net 核心 Web api 应用程序上的 google 身份验证存在问题。 我的用例很简单,从谷歌获取不记名令牌,将授权标头中的令牌作为“不记名{token}”并调用我的网络 api。

但我不能让它工作。在我通过以下网址从谷歌获得令牌后:

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}

我将使用标题调用我的 api:

Authorization: Bearer {TokenValue}

但是每次我得到 401 Unauthorized.

这是我的 Startup 课程:

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();
}

【问题讨论】:

  • 大家找到答案了吗?我自己也在挣扎。
  • 是的,我自己写了中间件。

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2020-03-09
    • 1970-01-01
    • 2021-06-15
    • 2017-09-11
    • 2023-04-01
    • 2019-08-17
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    相关资源
    最近更新 更多