gdo*_*ica 4 c# asp.net facebook oauth asp.net-core
我正在使用ASP.NET核心RC1(由于RC2缺乏VS支持,因此无法升级到尚未发布的RC2夜间版本).
我试图从Facebook获得额外的字段(first_name,last_name,email和significant_other).
我使用了Github上建议的代码:
app.UseFacebookAuthentication(options =>
{
options.AppId = Configuration["Authentication:Facebook:AppId"];
options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
options.Scope.Add("email");
options.Scope.Add("user_relationships");
options.BackchannelHttpHandler = new HttpClientHandler();
options.UserInformationEndpoint =
"https://graph.facebook.com/v2.5/me?fields=id,email,first_name,last_name,significant_other";
Run Code Online (Sandbox Code Playgroud)
这个解决方案确实返回了email用户,但是失败了first_name,last_name和significant_other(以及除了名称,id和电子邮件之外我尝试过的任何其他字段).
此外,是否可以获取FB访问令牌?我们可能需要它来进行其他边缘的查询,或者用它来手动查询Facebook,因为ASP.NET Core有一个bug(至少在RC1中).
我需要一种方法,即使不是最干净的.
我正在尝试从Facebook获取其他字段(first_name,last_name,email和significant_other).这个解决方案确实返回了用户的电子邮件,但是使用first_name,last_name和significant_other(以及除了name,id和email之外我尝试过的任何其他字段)都失败了.
在RC1中,Facebook中间件自动将电子邮件存储为声明,但不是名字或姓氏,因此如果您希望能够从应用程序代码中检索,则需要使用事件模型手动提取它们:
app.UseFacebookAuthentication(options => {
options.Events = new OAuthEvents {
OnCreatingTicket = context => {
var surname = context.User.Value<string>("last_name");
context.Identity.AddClaim(new Claim(ClaimTypes.Surname, surname));
return Task.FromResult(0);
}
};
});
Run Code Online (Sandbox Code Playgroud)
在RC2中,自定义代码不是必需的,因为默认情况下现在包含名字/姓氏:https://github.com/aspnet/Security/issues/688.
此外,是否可以获取FB访问令牌?我们可能需要它来进行其他边缘的查询,或者用它来手动查询Facebook,因为ASP.NET Core有一个bug(至少在RC1中).
您可以使用该SaveTokensAsClaims选项将访问/刷新令牌存储为声明(默认情况下在RC1中启用).如果您需要有关此功能的更多信息,可以查看介绍它的PR:https://github.com/aspnet/Security/pull/257.
app.UseFacebookAuthentication(options => {
options.SaveTokensAsClaims = true;
});
Run Code Online (Sandbox Code Playgroud)
您可以像任何其他声明一样检索它:
var token = User.FindFirst("access_token")?.Value
Run Code Online (Sandbox Code Playgroud)
注意:在RC2中,此功能已更新,并且令牌不会存储在声明中,而是存储在身份验证属性中:https://github.com/aspnet/Security/pull/698.
| 归档时间: |
|
| 查看次数: |
1399 次 |
| 最近记录: |