c# - Facebook Login works in localhost but not in webhost -
i have class listed below:
public class facebookscopedclient : iauthenticationclient { private string appid; private string appsecret; private string scope; private const string baseurl = "https://www.facebook.com/dialog/oauth?client_id="; public const string graphapitoken = "https://graph.facebook.com/oauth/access_token?"; public const string graphapime = "https://graph.facebook.com/me?"; private string gethtml(string url) { string connectionstring = url; try { var myrequest = (httpwebrequest)webrequest.create(connectionstring); myrequest.credentials = credentialcache.defaultcredentials; //// response webresponse webresponse = myrequest.getresponse(); stream respstream = webresponse.getresponsestream(); //// var iostream = new streamreader(respstream); string pagecontent = iostream.readtoend(); //// close streams iostream.close(); respstream.close(); return pagecontent; } catch (exception) { } return null; } private idictionary<string, string> getuserdata(string accesscode, string redirecturi) { string token = gethtml(graphapitoken + "client_id=" + appid + "&redirect_uri=" + httputility.urlencode(redirecturi) + "&client_secret=" + appsecret + "&code=" + accesscode); if (string.isnullorempty(token)) { return null; } string access_token = token.substring(token.indexof("access_token=", stringcomparison.ordinal), token.indexof("&", system.stringcomparison.ordinal)); token = access_token.replace("access_token=", string.empty); string data = gethtml(graphapime + "fields=id,name,email,username,gender,link&" + access_token); // dictionary must contains var userdata = jsonconvert.deserializeobject<dictionary<string, string>>(data); userdata.add("access_token", token); return userdata; } public facebookscopedclient(string appid, string appsecret, string scope) { this.appid = appid; this.appsecret = appsecret; this.scope = scope; } public string providername { { return "facebook"; } } public void requestauthentication(system.web.httpcontextbase context, uri returnurl) { string url = baseurl + appid + "&redirect_uri=" + httputility.urlencode(returnurl.tostring()) + "&scope=" + scope; context.response.redirect(url); } public authenticationresult verifyauthentication(system.web.httpcontextbase context) { string code = context.request.querystring["code"]; string rawurl = context.request.url.originalstring; //from need remove code portion rawurl = regex.replace(rawurl, "&code=[^&]*", ""); idictionary<string, string> userdata = getuserdata(code, rawurl); if (userdata == null) return new authenticationresult(false, providername, null, null, null); string id = userdata["id"]; string username = userdata["username"]; userdata.remove("id"); userdata.remove("username"); var result = new authenticationresult(true, providername, id, username, userdata); return result; } }
the above class registered in authconfig.cs so
oauthwebsecurity.registerclient( new facebookscopedclient("blablabla", "blablabla", "read_stream,status_update,publish_actions,offline_access,user_friends"), "facebook", facebooksocialdata);
and use during authentication so
[allowanonymous] public actionresult externallogincallback(string returnurl) { authenticationresult result = oauthwebsecurity.verifyauthentication(url.action("externallogincallback", new { returnurl = returnurl })); if (!result.issuccessful) { return redirecttoaction("externalloginfailure"); } if (result.extradata.keys.contains("access_token")) { session["token"] = result.extradata["access_token"]; } if (oauthwebsecurity.login(result.provider, result.provideruserid, createpersistentcookie: false)) { return redirecttolocal(returnurl); } if (user.identity.isauthenticated) { // if current user logged in add new account oauthwebsecurity.createorupdateaccount(result.provider, result.provideruserid, user.identity.name); return redirecttolocal(returnurl); } // user new, ask desired membership name string logindata = oauthwebsecurity.serializeprovideruserid(result.provider, result.provideruserid); viewbag.providerdisplayname = oauthwebsecurity.getoauthclientdata(result.provider).displayname; viewbag.returnurl = returnurl; var client = new computerbeacon.facebook.graph.user("me", session["token"].tostring()); var firstname = client.firstname; var lastname = client.lastname; var username = client.email; return view("externalloginconfirmation", new registerexternalloginmodel { username = result.username, firstname = firstname, lastname = lastname, externallogindata = logindata }); }
now works 100% expected in localhost, when upload remote server, not work strange reason.
authenticationresult result = oauthwebsecurity.verifyauthentication(url.action("externallogincallback", new { returnurl = returnurl }));
is never successful. please doing wrong. have updated neccessary url's @ developers.facebook.com
thanks
well saw problem.
public authenticationresult verifyauthentication(system.web.httpcontextbase context) { string code = context.request.querystring["code"]; string rawurl = context.request.url.originalstring; if (rawurl.contains(":80/")) { rawurl = rawurl.replace(":80/", "/"); } if (rawurl.contains(":443/")) { rawurl = rawurl.replace(":443/", "/"); } //from need remove code portion rawurl = regex.replace(rawurl, "&code=[^&]*", ""); idictionary<string, string> userdata = getuserdata(code, rawurl); if (userdata == null) return new authenticationresult(false, providername, null, null, null); string id = userdata["id"]; string username = userdata["username"]; userdata.remove("id"); userdata.remove("username"); var result = new authenticationresult(true, providername, id, username, userdata); return result; }
thanks http://savvydev.com/authenticating-facebook-users-with-mvc-4-oauth-and-obtaining-scope-permissions/ these came anyway
thanks contributing.
Comments
Post a Comment