Facebookdk integration with Identity 2.0

I have an MVC5 application and figured out how to authenticate with facebook using Identity 2.0. Besides adding email to the scope, basically this is the default value:

        var options = new FacebookAuthenticationOptions()
        {
            AppId = "##################",
            AppSecret = "######################"
        };
        options.Scope.Add("email");
        app.UseFacebookAuthentication(options);

In another section of my application, it is possible that the user will need to update the facebook token so that he can publish on the wall (I was hoping to just add it options.Scope.Add("publish_stream");). When I cross out the above code and put it inside catchhere ( the facebookdk.net code below ):

    try {
    var client = new FacebookClient("my_access_token");
    dynamic result = client.Get("me/friends");
} catch (FacebookOAuthException) {
    // Our access token is invalid or expired
    // Here we need to do something to handle this.
}

app. , , . ? , facebookdk.net , Identity 2.0. ? , Identity facebook, , , - , ? , facebook SQL Logins facebook, ? "", .

, ( facebook), . , , , , facebook, , , , ( ).

.

0
1

Facebook . , . ,

  • , , .
  • TOKEN facebook api .

, , , facebook, , ... . ( ) , facebook , . : https://developers.facebook.com/docs/facebook-login/access-tokens

( startup_auth.cs), Identity. ( ) . facebook api .

// Facebook integration
var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions()
{
    AppId = ConfigurationManager.AppSettings["Facebook.appID"],
    AppSecret = ConfigurationManager.AppSettings["Facebook.appSecret"],
    Provider = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationProvider()
    {
        OnAuthenticated = (context) =>
        { 
            // STORE THE USER ACCESS TOKEN GIVEN TO US BY FACEBOOK FOR THIS USER
            context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, XmlSchemaString, "Facebook"));
            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 Claim(claimType, claimValue, XmlSchemaString, "Facebook"));

             }
             return Task.FromResult(0);
        }
    }
 };


 facebookOptions.Scope.Add("email");
 facebookOptions.Scope.Add("publish_actions");
 app.UseFacebookAuthentication(facebookOptions);

, , , ( callback/url facebook), :

public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
    var accessToken = loginInfo.ExternalIdentity.Claims
                    .Where(c => c.Type.EndsWith("facebook:access_token"))
                    .FirstOrDefault();
    ....
}
0

All Articles