How do I get access_token when I sign in with Facebook on MVC5?

2

When I create a project MVC5 the Visual Studio automatically creates a site for me with a login option via Facebook, Google ... I just need to enable "Startup.Auth.cs" .

But it does the direct login and I do not know how to get the access_token , I would like to save the token to be able to make requests.

How do I get access_token ?

    
asked by anonymous 19.12.2013 / 21:40

1 answer

2

You need to create an instance of FacebookAuthenticationOptions and set Provider . The Provider contains an event called OnAuthenticated that occurs when you are authenticated.

var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions
{
    Provider = new FacebookAuthenticationProvider()
    {
        OnAuthenticated = (context) =>
        {
            context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, ClaimValueTypes.String, "Facebook"));

            return Task.FromResult(0);
        }
    },

    // pode informar o id e secret diretamente ou incluir no AppSettings
    AppId = ConfigurationManager.AppSettings["facebook:AppId"],
    AppSecret = ConfigurationManager.AppSettings["facebook:AppSecret"]
};

app.UseFacebookAuthentication(facebookOptions);

In the code above I'm accessing the access_token by context.AccessToken and adding in the Claims of the user who logged in. This is a good place to store user information while logged in.

To read this value somewhere in your code use:

var owinContext = HttpContext.GetOwinContext();
var authentication = owinContext.Authentication;
var user = autentication.User;
var claim = (user.Identity as ClaimsIdentity).FindFirst("urn:facebook:access_token");

string accessToken;
if (claim != null)
    accessToken = claim.Value;

To simplify this whole process you can create properties in a BaseController and make all your Controllers inherit from this new Controller .

The BaseController can be:

public class BaseController : Controller
{
    public IOwinContext CurrentOwinContext
    {
        get
        {
            return HttpContext.GetOwinContext();
        }
    }

    public IAuthenticationManager Authentication
    {
        get
        {
            return CurrentOwinContext.Authentication;
        }
    }

    public new ClaimsPrincipal User
    {
        get
        {
            return Authentication.User;
        }
    }

    public ClaimsIdentity Identity
    {
        get
        {
            return Authentication.User.Identity as ClaimsIdentity;
        }
    }

    public string FacebookAccessToken
    {
        get
        {
            var claim = Identity.FindFirst("urn:facebook:access_token");

            if (claim == null)
                return null;

            return claim.Value;
        }
    }
}

And to use access_token on your Controller you will just need to access the FacebookAccessToken property.

string accessToken = FacebookAccessToken;

You can get other values like

context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:username",
    context.User.Value<string>("username"), ClaimValueTypes.String, "Facebook"));

context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:name",
    context.User.Value<string>("name"), ClaimValueTypes.String, "Facebook"));

Note that some values will not be present if Scope is not informed. To get the email you need to include Scope

facebookOptions.Scope.Add("email");
    
19.12.2013 / 21:40