加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – ASP .Net核心Google身份验证

发布时间:2020-12-16 09:32:56 所属栏目:asp.Net 来源:网络整理
导读:我的.net核心web api应用程序上的google身份验证有问题. 我的用例很简单,从授权标题中的google put token获取bearer token作为“Bearer {token}”并调用我的web api. 但我不能让它发挥作用.我从以下网址获取谷歌令牌后: https://accounts.google.com/o/oaut
我的.net核心web api应用程序上的google身份验证有问题.
我的用例很简单,从授权标题中的google put token获取bearer token作为“Bearer {token}”并调用我的web api.

但我不能让它发挥作用.我从以下网址获取谷歌令牌后:

https://accounts.google.com/o/oauth2/v2/auth?scope=email openid& include_granted_scopes = true& state = some_test_state& redirect_uri = http:// localhost:53512& 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();
}

解决方法

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读