ASP.NET 5 MVC 6中的Google OAuth

4

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

我漏掉了什么?
2个回答

2

这里有一个示例: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);
        }
    };

});

0
如果您正在这样使用配置存储:
Configuration["Authentication:MicrosoftAccount:ClientId"]; 

那么,如果您所说的“配置Google OAuth”是指存储值在SecretManager中的部分,您也会错过这一点,正如ASP.NET文档中所述(它们使用Facebook,但您可以将任何密钥放入其中)。这是一个命令行工具,可以避免您像那样在代码中存储密钥。在Google的情况下,您可能需要将其更改为:

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接