【问题标题】:Google OAuth in ASP.NET 5 MVC 6ASP.NET 5 MVC 6 中的 Google OAuth
【发布时间】:2016-02-05 03:59:29
【问题描述】:

我是 ASP.NET vNext 的新手,我不知道如何配置 Google OAuth。

我已取消注释 Startup.cs 中的行:

 app.UseGoogleAuthentication();

但是我应该在哪里配置它?我试图复制模式:

services.Configure<MicrosoftAccountAuthenticationOptions>(options =>
{
    options.ClientId = Configuration["Authentication:MicrosoftAccount:ClientId"];
    options.ClientSecret = Configuration["Authentication:MicrosoftAccount:ClientSecret"];
});

但是

services.Configure<GoogleOAuth2AuthenticationOptions>

即使依赖项存在于 project.json 中也无法识别:

"Microsoft.AspNet.Authentication.Google": "1.0.0-beta5",

我错过了什么?

【问题讨论】:

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


    【解决方案1】:

    https://github.com/aspnet/Security/blob/dev/samples/SocialSample/Startup.cs 有一个示例。

    我没试过,但是看起来你是用 app.UseGoogleAuthentication() 配置的:

    app.UseGoogleAuthentication(options =>
    {
        options.ClientId = "560027070069-37ldt4kfuohhu3m495hk2j4pjp92d382.apps.googleusercontent.com";
        options.ClientSecret = "n2Q-GEw9RQjzcRbU3qhfTj8f";
        options.Events = new OAuthEvents()
        {
            OnRemoteError = ctx =>
    
            {
                ctx.Response.Redirect("/error?ErrorMessage=" + UrlEncoder.Default.Encode(ctx.Error.Message));
                ctx.HandleResponse();
                return Task.FromResult(0);
            }
        };
    
    });
    

    【讨论】:

      【解决方案2】:

      如果你使用这样的配置存储

      Configuration["Authentication:MicrosoftAccount:ClientId"]; 
      

      那么您还缺少什么(如果这就是“配置 Google OAuth”的意思)是您将值存储在 SecretManager 中的部分,如 the ASP.NET docs 中所述(他们使用 facebook,但您可以放任何东西你想要的钥匙在那里)。它是一个命令行工具,它可以避免您将密钥存储在这样的代码中。在 Google 的情况下,您可能希望将其更改为:

      Configuration["Authentication:Google:ClientID"];
      Configuration["Authentication:Google:ClientSecret"];
      

      但它可以是任何你想要的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多