我正在尝试检索作为 OnAuthenticated 上下文返回的用户属性,并在此示例之后添加为声明: 如何使用 ASP.NET 身份 (OWIN) 访问 Facebook 私人信息?
我可以看到我期望的数据在登录时被返回,并被添加为 Starup.Auth.cs 中的声明。但是,当我在 Account Controller 中时,出现在 UserManager 或 UserStore 中的唯一声明是由 LOCAL AUTHORITY 发布的。找不到 Facebook(或其他外部提供商)的声明。添加到上下文中的声明在哪里结束?(我正在使用 VS2013 RTM。)
Azure 上的完整源代码和实时站点链接在这里:https ://github.com/johndpalm/IdentityUserPropertiesSample/tree/VS2013rtm
这是我在 Startup.Auth.cs 中的内容:
var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions()
{
AppId = ConfigurationManager.AppSettings.Get("FacebookAppId"),
AppSecret = ConfigurationManager.AppSettings.Get("FacebookAppSecret"),
Provider = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string";
foreach (var x in context.User)
{
var claimType = string.Format("urn:facebook:{0}", x.Key);
string claimValue = x.Value.ToString();
if (!context.Identity.HasClaim(claimType, claimValue))
context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, XmlSchemaString, "Facebook"));
}
context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, XmlSchemaString, "Facebook"));
return Task.FromResult(0);
}
}
};
facebookOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookOptions);
捕获外部登录属性的另一种方法是为访问令牌添加一个声明并使用属性填充它:
const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string";
var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions
{
AppId = ConfigurationManager.AppSettings.Get("FacebookAppId"),
AppSecret = ConfigurationManager.AppSettings.Get("FacebookAppSecret"),
Provider = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
var claim = new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, XmlSchemaString, "Facebook");
foreach (var x in context.User)
{
string key = string.Format("urn:facebook:{0}", x.Key);
string value = x.Value.ToString();
claim.Properties.Add(key, value);
}
context.Identity.AddClaim(claim);
return Task.FromResult(0);
}
}
};
注意 - 此示例不起作用:尽管传递带有属性的单个声明会很好。外部 cookie 似乎注意到了声明属性。稍后从身份中检索属性时,这些属性为空。