Google ASP.NET 5 MVC 6 中的 OAuth

Google OAuth in ASP.NET 5 MVC 6

我是 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",

我错过了什么?

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

});

如果您正在使用这样的配置存储

Configuration["Authentication:MicrosoftAccount:ClientId"]; 

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

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

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