Asp.net web forms, Asp Identity - how to store cla

2019-02-26 17:14发布

This request is based upon the new Visual Studio 2013 integration of Asp.net Identity stuff. I have seen some of the posts regarding this question for MVC, but for the life of me cannot get it to work for standard Web Forms. What I'm trying to do is populate the AspNetUserClaims table from the claims that I get back from Facebook (or other service). I actually can see the values coming back in the OnAuthenticated below, but cannot for the life of me figure out how to add these claims to the context of the currently logged in user?

There are literally hundreds of MVC examples surrounding this, but alas no Web Forms examples. This should be completely straightforward, but for some reason I cannot match up the context of the currently logged in user to the claims and credentials coming back from Facebook.

Currently after the OnAuthenticated fires, it obviously returns me to the page (RegisterExternalLogin.aspx) as the built-in example provides. However, the claims are gone, the context of the login to Facebook is gone, and I can't do anything else at this point.

So the ultimate question is, HOW does one populate the claims FROM Facebook into the AspNetUserClaims table based upon the context of the currently logged in user WITHOUT using MVC?

        var fboptions = new FacebookAuthenticationOptions();
        fboptions.AppId = "xxxxxxxxxxxxxxxxxxx";
        fboptions.AppSecret = "yyyyyyyyyyyyyyyyyyyyyy";
        fboptions.Scope.Add("email");
        fboptions.Scope.Add("friends_about_me");
        fboptions.Scope.Add("friends_photos");

        fboptions.Provider = new FacebookAuthenticationProvider()
        {
            OnAuthenticated = (context) =>
            {
                foreach (var v in context.User)
                {
                    context.Identity.AddClaim(new System.Security.Claims.Claim(v.Key, v.Value.ToString()));
                }
                context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
                return Task.FromResult(0);
            },
        };
        app.UseFacebookAuthentication(fboptions);

1条回答
地球回转人心会变
2楼-- · 2019-02-26 17:37

Ok, I know you said you want to know how to do WITHOUT MVC, and while I cannot answer that directly, I can tell you that I had similar issues with MVC, and how I resolved it. Hopefully that points you in the right direction in Web Forms.

The key bits was the following:

After you have logged in using the external provider you are directed to a page where you register the new user. After the postback from that I had to add a line to first of all get the ClaimsIdentity again:

ClaimsIdentity claimsIdentity =
            await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);

Without the call to this line above it did not work for me at all, so maybe that is where your problem lies.

Once I had the ClaimsIdentity you can iterate through the collection of claims on the Claims property of the identity. This should include the Facebook token you added in the OnAuthenticated callback delegate. All you have to do then is call UserManager.AddClaimAsync() to add it to the Claims database table.

For more info look at my AccountController class at https://github.com/beabigrockstar/AspNetIdentitySocialProfileImage/blob/master/Controllers/AccountController.cs

Look specifically at the method ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) and the call to StoreAuthTokenClaims(), and then of course the process I follow in StoreAuthTokenClaims()

Hope that this can somehow help you in WebForms.

查看更多
登录 后发表回答