【发布时间】:2017-03-01 17:53:13
【问题描述】:
按照文档中的说明,我设置了 Facebook 身份验证,如下所示:
"dependencies": {
//blah blah snip
"Microsoft.AspNetCore.Authentication.Facebook": "1.0.0-rc2-final"
},
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//blah blah snip
app.UseIdentity();
// Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
app.UseFacebookAuthentication(new FacebookOptions()
{
AppId = Configuration["Authentication:Facebook:AppId"],
AppSecret = Configuration["Authentication:Facebook:AppSecret"],
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
在从 Facebook 获取 AppId 和 AppSecret 并使用 SecretManager 存储它们后,我启动了它,然后成功登录 Facebook,非常高兴。
在成功的鼓舞下,我尝试按照同样的思路设置 Google 身份验证。
"dependencies": {
//blah blah snip
"Microsoft.AspNetCore.Authentication.Facebook": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Authentication.Google": "1.0.0-rc2-final"
},
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//blah blah snip
app.UseIdentity();
// Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
app.UseFacebookAuthentication(new FacebookOptions()
{
AppId = Configuration["Authentication:Facebook:AppId"],
AppSecret = Configuration["Authentication:Facebook:AppSecret"],
});
app.UseGoogleAuthentication(new GoogleOptions()
{
ClientId = Configuration["Authentication:Google:ClientId"],
ClientSecret = Configuration["Authentication:Google:ClientSecret"],
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
它几乎有效,但The redirect URI in the request, https://localhost:44391/signin-google, does not match the ones authorized for the OAuth client.
一项小研究表明,重定向 URL 由 GoogleOptions 对象的 CallbackPath 属性的值提供,并且默认为“signin-google”。我没有登录谷歌路径,CallbackPath = "/" 在应用程序初始化期间产生未处理的远程故障(无论是什么),所以我将其删除并使用 Google 更新了应用程序详细信息以允许 https://localhost:44391/signin-google 作为重定向路径。
这提示我授予应用“具有离线访问权限”,所以我做到了。
我尝试像这样设置范围
var go = new GoogleOptions()
{
ClientId = Configuration["Authentication:Google:ClientId"],
ClientSecret = Configuration["Authentication:Google:ClientSecret"],
};
go.Scope.Add("email");
go.Scope.Add("profile");
app.UseGoogleAuthentication(go);
有趣的是,默认范围是“openid”。添加“电子邮件”和“个人资料”范围不会在授权请求中产生任何差异。添加无效范围会产生错误,这意味着这些是有效的可识别范围。
使用 Google 开发者控制台为该应用启用 Google+ API 可解决 403 Forbidden 错误,但出于我无法理解的原因,我们仍会提示授予离线访问权限。
GoogleOptions 对象有一个属性 AccessType,默认为 null。将其设置为“在线”似乎并没有什么不同。
问题
系统提示我授予离线访问权限。是什么让服务器认为我想要离线访问?需要设置什么来防止这种情况发生?在 GoogleOptions 对象初始化器中设置 AccessType = "online"
var go = new GoogleOptions()
{
AccessType = "online",
ClientId = Configuration["Authentication:Google:ClientId"],
ClientSecret = Configuration["Authentication:Google:ClientSecret"],
};
似乎没有任何作用。这是正确的值吗?
【问题讨论】:
-
离线访问是一堆不同范围的默认消息,电子邮件和配置文件是其中的两个。除了停止请求配置文件和电子邮件范围之外,无法更改消息。
-
我会得出同样的结论。你应该提出这个作为答案。
-
我怎样才能从外部提供商那里检索索赔并预填注册表?
标签: authentication asp.net-core-mvc google-oauth