Confusion regarding LOCAL AUTHORITY applications and external suppliers

I am creating a simple WebApi that allows users to connect to Facebook. When I get the accessToken back from facebook, I call RegisterExternal to create an Identity Asp.Net entry and store the claims from the token. These claims also include the access token that I need for a subsequent facebook schedule request. Everything seems to be fine.

The problem I am facing is reading the claims. I see that they are in my database. I just can't figure out how to request this data. I tried

var claimsIdentity = User.Identity as ClaimsIdentity; 

But this returns me 2 claims a) " http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name " b) role

Both of them belong to the publisher of LOCAL AUTHORITY (to be honest, I'm not sure when they are created, since I am not adding them explicitly). Therefore, I believe that they either confuse me, keeping the claims in the database for the wrong type of issuer

 await userManager.AddClaimAsync(user.Id, new Claim("urn:facebook:access_token", accessTokenClaim.Value, ClaimValueTypes.String, "LOCAL AUTHORITY")); 

or my claim code is incorrect.

Can anyone shed some light on this?

+5
source share
2 answers

When it comes to adding claims to your Identity:

 // 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); } } } 

To store these claims in the database, you must call SignIn for the user:

 // Sign in and redirect the user await SignInAsync(user, isPersistent: false); 

To receive the application later, you simply use:

 var claimsIdentity = HttpContext.User.Identity as ClaimsIdentity; if (claimsIdentity != null) var claims = claimsIdentity.Claims; 

This code consists of snippets from this article: http://www.jerriepelser.com/blog/get-the-twitter-profile-image-using-the-asp-net-identity

I would recommend reading it if you want to see a complete example. I myself used the code in this article, and it did a great job in my project for the external requirements of Twitter and Facebook.

0
source

I had the same problem when I renamed a cookie. So I had 2 different users in 2 cookies. After I deleted the old one, the problem disappeared.

0
source

Source: https://habr.com/ru/post/981559/


All Articles