0

I am using OWIN Oauth Instagram to sign in on my MVC 5 application with the following code:

Startup.Auth.cs

 app.UseInstagramAuthentication(new InstagramAuthenticationOptions()
            {
                ClientId = "myCID",
                ClientSecret = "mySecret",
                SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,

                Provider = new InstagramAuthenticationProvider()
                {
                    OnAuthenticated = (context) =>
                    {
                        context.Identity.AddClaim(new Claim("igAccessToken", context.AccessToken));
                        context.Identity.AddClaim(new Claim("igFullName", context.FullName));
                        context.Identity.AddClaim(new Claim("igProfilePic", context.ProfilePicture));
                        context.Identity.AddClaim(new Claim("igUsername", context.UserName));

                        foreach (var claim in context.User)
                        {
                            var claimType = string.Format("{0}", claim.Key);
                            string claimValue = claim.Value.ToString();

                            if (!context.Identity.HasClaim(claimType, claimValue))
                                context.Identity.AddClaim(new Claim(claimType, claimValue, "XmlSchemaString", "Instagram"));
                        }
                        return Task.FromResult(0);
                    }
                }
            });

Login works and I am able to store this information in AspNetUserRoles table by adding new columns to hold the data respectively but I don't think is the best method nor do I know how to access the info. How do I access this data with something like:

SomeController.cs

    var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                var pic = info.ExternalIdentity.Claims.First(c => c.Type == "igProfilePic").Value;
                var fullName = info.ExternalIdentity.Claims.First(c => c.Type == "igFullName").Value;
                var accessToken = info.ExternalIdentity.Claims.First(c => c.Type == "igAccessToken").Value;

The code above throws a Object reference not set to an instance of an object. and after debugging it, there is nothing in the info context leading me to believe the claim is never stored in the ExternalIdentity context?

Note: There are quite a few similar questions but they seem outdated and don't work for me since a lot of my code is borrowed from these questions.

NuWin
  • 276
  • 5
  • 15
  • Did you try [that](https://stackoverflow.com/a/21405165)? See [Understanding the Owin External Authentication Pipeline](https://coding.abel.nu/2014/06/understanding-the-owin-external-authentication-pipeline/) for more information. – user7217806 Sep 27 '19 at 06:22
  • I tried adding the ienumerable code in the answer from your link and debugged it. I don't see any claims from Instagram just some local authority claims. I also read the article. From my understanding of the article, it says the external cookie data is stored and then converted to application cookie data which should be accessible but its not in my case. I believe thats the problem. Debuging the code when creating the claims, its adding the claims in which I am able to add to the DB and can view it in the DB. – NuWin Sep 27 '19 at 08:14
  • 1
    Could you please describe how you use AspNetUserRoles in this context? Are you able to [get the claims manually](https://stackoverflow.com/questions/22997729/asp-net-identity-doesnt-load-any-claims)? – user7217806 Sep 27 '19 at 19:26
  • 1
    @user7217806 Thanks! your second link helped me figure it out. Initially, I've noticed it before but didn't think it mattered because when looking at the AspNetUserClaims table, there was no data. So after adding the claims in the Startup.Auth I am able to access it from the AccountController ExternalLoginConfirmation method utilizing the ExternalIdentityClaims object which I was able to add to the DB but after moving out of this scope I am not. My workaround was to add the claims again into the UserManager manually which then populates the AspNetUserClaims table which I am able to access. – NuWin Sep 27 '19 at 22:51
  • 1
    if you want to formulate an answer I will upvote and accept. – NuWin Sep 27 '19 at 22:51
  • Thanks for your update, great that you sorted it out! – user7217806 Sep 28 '19 at 19:55

1 Answers1

1

You should be able to access GetExternalLoginInfoAsync in the ExternalLoginConfirmation method of the AccountController, see the documentation. Here, the claims should be available for further processing, e.g. for manually adding claims into the database, should that be necessary.

If the claims have been added into the IdentityUserClaim table, you should be able to access them in other controller actions (also see this answer) using:

IEnumerable<Claim> claims = (ClaimsIdentity)User.Identity.Claims;

Further information can be found in Understanding the Owin External Authentication Pipeline.

user7217806
  • 2,014
  • 2
  • 10
  • 12