c# - Integrate facebooksdk with Identity 2.0 -
i have mvc5 app , have figured out how authenticate facebook using identity 2.0. other adding email scope, default:
var options = new facebookauthenticationoptions() { appid = "##################", appsecret = "######################" }; options.scope.add("email"); app.usefacebookauthentication(options); in section of app, possible user need upgrade facebook token able publish wall (i hoping add options.scope.add("publish_stream");). when cut above code out , place inside catch here (the code below referenced here @ facebooksdk.net):
try { var client = new facebookclient("my_access_token"); dynamic result = client.get("me/friends"); } catch (facebookoauthexception) { // our access token invalid or expired // here need handle this. } it doesn't app. starters , can't figure out replace with. correct way go this? realize there examples on facebooksdk.net whatever token request do, hoping integrate identity 2.0. or matter? i'm not sure i'm understanding identity process correctly , not familiar facebook authentication either, can understand i'm trying , guide me? guess need pass facebook token in logins sql table facebook ask user reauthenticate elevated permissions? if token doesn't exist in logins table, process add there.
for record, realize can request permissions (when user logs in our site facebook first time), we're trying not scare off more savvy users initially. so, idea when click publish article, if opt publish facebook, request upgrade of token first time , never again (which never part may wishful thinking).
thank you.
facebook can confusing. token need depends on action trying achieve. instance,
- you use app token request user token has specific permissions.
- you use user token access facebook api on behalf of user.
in code posted, appears have access token sending facebook access token granted app ... not given user's access token. need capture (and store) user's access token given facebook after having been granted access given user. more on here: https://developers.facebook.com/docs/facebook-login/access-tokens
here sample code (used in startup_auth.cs) uses identity framework. captures access token , persists (as claim) given user. access token can read later access facebook api user @ given time.
// 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 user access token given facebook 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); so retrieve access token user, retrieve (in callback function/url facebook) this:
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(); .... }
Comments
Post a Comment