Raj*_*Raj 5 asp.net facebook claims-based-identity oauth-2.0 asp.net-web-api
我正在创建一个简单的WebApi,允许用户与Facebook连接.当我从facebook获取accessToken时,我正在调用RegisterExternal来创建Asp.Net Identity记录并存储来自令牌的声明.这些声明还包括我稍后查询facebook图表所需的访问令牌.到目前为止,一切似乎都很好.
我遇到的问题是阅读索赔.我可以看到他们在我的数据库中我只是想弄清楚如何查询这些数据.我试过了
var claimsIdentity = User.Identity as ClaimsIdentity;
Run Code Online (Sandbox Code Playgroud)
但这让我得到了2个声明a)" http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name "b)角色
这两个都是发行人LOCAL AUTHORITY(说实话,我不确定它们何时被创建,因为我没有明确添加这些).因此,我认为他们要么将我对数据库的索赔再次保存在错误类型的发行人身上,要么是混乱
await userManager.AddClaimAsync(user.Id, new Claim("urn:facebook:access_token", accessTokenClaim.Value, ClaimValueTypes.String, "LOCAL AUTHORITY"));
Run Code Online (Sandbox Code Playgroud)
或者我访问索赔的代码不正确.
任何人都可以对此有所了解吗?
在将声明添加到您的身份时:
// Get the claims identity
ClaimsIdentity claimsIdentity =
await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
if (claimsIdentity != null)
{
// Retrieve the existing claims
var currentClaims = await UserManager.GetClaimsAsync(user.Id);
// Get the list of access token related claims from the identity
var tokenClaims = claimsIdentity.Claims
.Where(c => c.Type.StartsWith("urn:tokens:"));
// Save the access token related claims
foreach (var tokenClaim in tokenClaims)
{
if (!currentClaims.Contains(tokenClaim))
{
await UserManager.AddClaimAsync(user.Id, tokenClaim);
}
}
}
Run Code Online (Sandbox Code Playgroud)
要将这些声明保留到数据库,您必须为用户调用 SignIn:
// Sign in and redirect the user
await SignInAsync(user, isPersistent: false);
Run Code Online (Sandbox Code Playgroud)
要稍后检索声明,您只需使用:
var claimsIdentity = HttpContext.User.Identity as ClaimsIdentity;
if (claimsIdentity != null)
var claims = claimsIdentity.Claims;
Run Code Online (Sandbox Code Playgroud)
此代码由本文的片段组成:http://www.jerriepelser.com/blog/get-the-twitter-profile-image-using-the-asp-net-identity
如果您想查看完整的示例,我建议您仔细阅读它。我自己使用了本文中的代码,它在我的项目中对于 Twitter 和 Facebook 外部声明都非常有效。
| 归档时间: |
|
| 查看次数: |
1795 次 |
| 最近记录: |