【问题标题】:Google authentication with cookies (without DB) in ASP.Net Core 2在 ASP.Net Core 2 中使用 cookie(无 DB)进行 Google 身份验证
【发布时间】:2018-03-07 01:50:45
【问题描述】:

我想使用 Google 对用户进行身份验证,并使用 cookie 保持身份验证。

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddMediatR(typeof(UpdateClientsCommandHandler));
        services
            .AddEntityFrameworkSqlServer()
            .AddDbContext<DbContext>(options =>
                {
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                        sqlOptions =>
                        {
                            sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
                            sqlOptions.EnableRetryOnFailure(10, TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
                        });
                    options.EnableSensitiveDataLogging();
                }
            );
        services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = "YourCustomScheme";
                options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
            })
            .AddCookie("YourCustomScheme")
            .AddGoogle(options =>
            {
                options.ClientId = "client-id";
                options.ClientSecret = "client-secret";
                options.CallbackPath = new PathString("/AuthCallback/IndexAsync");
                options.SignInScheme = "YourCustomScheme";
            });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
           routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

        app.UseAuthentication();
    }
}

用户已成功重定向到 Google Auth,我可以看到创建了 cookie .AspNetCore.YourCustomScheme。但随后用户只是被重定向回 Google 登录页面。

我错过了什么吗?

【问题讨论】:

  • 将 options.DefaultAuthenticateScheme 更改为 YourCustomScheme
  • @Tratcher 没有帮助 :(
  • 你的控制器是什么样的? [Authorize]?
  • 是的,只需在控制器上使用[Authorize]。控制器有一个简单的动作。
  • 如果将DefaultAuthenticateScheme = GoogleDefaults.AuthenticationScheme 替换为DefaultScheme = "YourCustomScheme" 会更好吗?另外,您是否在任何地方都有 AddIdentity,它会覆盖这些设置。

标签: asp.net asp.net-mvc asp.net-core asp.net-core-mvc google-oauth


【解决方案1】:

看来我的问题是因为我把UseAuthentication放在UseMvc之后

【讨论】:

  • 你还有这方面的工作示例吗?我正在尝试做同样的事情,当我使用[Authorize] 访问控制器时,它会执行看似登录的操作,但每次都会将用户重定向到 /signin-google。
猜你喜欢
  • 2017-03-30
  • 1970-01-01
  • 2018-05-13
  • 2022-01-23
  • 1970-01-01
  • 2018-12-30
  • 2018-02-18
  • 2014-07-31
  • 2020-05-07
相关资源
最近更新 更多