Google 使用 MVC6 进行身份验证
Google authentication with MVC6
按照文档中的说明,我设置了 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"。我没有 signin-google 路径并且 CallbackPath = "/"
在应用程序初始化期间产生未处理的远程故障(无论是什么)所以我删除它并使用 Google 更新应用程序详细信息以允许https://localhost:44391/signin-google
作为重定向路径。
这提示我授予应用程序 "Have offline access",所以我这样做了。
我试过这样设置范围
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"。添加 "email" 和 "profile" 范围不会在授权请求中产生任何差异。添加无效范围会产生错误,这意味着这些是有效的已识别范围。
使用 Google 开发者控制台为应用程序启用 Google+ API 解决了 403 Forbidden 错误,但出于我不明白的原因,我们仍然提示授予权限用于离线访问。
GoogleOptions 对象有一个 属性 AccessType,默认为 null。将其设置为 "online" 似乎没有任何不同。
问题
系统提示我授予离线访问权限。是什么让服务器认为我想要离线访问?需要设置什么来防止这种情况?在 Google 选项对象初始化程序中设置 AccessType = "online"
var go = new GoogleOptions()
{
AccessType = "online",
ClientId = Configuration["Authentication:Google:ClientId"],
ClientSecret = Configuration["Authentication:Google:ClientSecret"],
};
好像没有什么效果。这是正确的值吗?
离线访问是一堆不同范围的默认消息电子邮件和配置文件是其中两个。除了停止请求配置文件和电子邮件范围之外,无法更改消息。
AccessType (online,Offline) 并不是您想象的那样。离线访问将 return 一个刷新令牌,以便您以后可以访问他们的数据。在线访问意味着您只会在他们在那里时访问他们的数据,并且您不需要刷新令牌。
按照文档中的说明,我设置了 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"。我没有 signin-google 路径并且 CallbackPath = "/"
在应用程序初始化期间产生未处理的远程故障(无论是什么)所以我删除它并使用 Google 更新应用程序详细信息以允许https://localhost:44391/signin-google
作为重定向路径。
这提示我授予应用程序 "Have offline access",所以我这样做了。
我试过这样设置范围
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"。添加 "email" 和 "profile" 范围不会在授权请求中产生任何差异。添加无效范围会产生错误,这意味着这些是有效的已识别范围。
使用 Google 开发者控制台为应用程序启用 Google+ API 解决了 403 Forbidden 错误,但出于我不明白的原因,我们仍然提示授予权限用于离线访问。
GoogleOptions 对象有一个 属性 AccessType,默认为 null。将其设置为 "online" 似乎没有任何不同。
问题
系统提示我授予离线访问权限。是什么让服务器认为我想要离线访问?需要设置什么来防止这种情况?在 Google 选项对象初始化程序中设置 AccessType = "online"
var go = new GoogleOptions()
{
AccessType = "online",
ClientId = Configuration["Authentication:Google:ClientId"],
ClientSecret = Configuration["Authentication:Google:ClientSecret"],
};
好像没有什么效果。这是正确的值吗?
离线访问是一堆不同范围的默认消息电子邮件和配置文件是其中两个。除了停止请求配置文件和电子邮件范围之外,无法更改消息。
AccessType (online,Offline) 并不是您想象的那样。离线访问将 return 一个刷新令牌,以便您以后可以访问他们的数据。在线访问意味着您只会在他们在那里时访问他们的数据,并且您不需要刷新令牌。