Hi friends,
I'm using fb login oAuth using javascript. here we use the code and get data in json form.
<script>
window.fbAsyncInit = function () {
FB.init({
appId: 'XXXXXXXXXXXXXX',
status: true,
cookie: true,
xfbml: true,
oauth: true,
});
};
(function (d) {
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; }
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
function fb_login() {
FB.login(function (response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
access_token = response.authResponse.accessToken;
user_id = response.authResponse.userID;
FB.api('/me', function (response) {
if (response.name) {
var userName = response.name;
var r = splitUsername(userName);
var SNfirstName = r.firstName;
var SNlastName = r.lastName;
var SNuserId = response.id;
var SNuserEmail = response.email;
var args = {
"SNfirstName": SNfirstName, "SNuserId": SNuserId, "SNlastName": SNlastName, "SNuserEmail": SNuserEmail
}
$.ajax({
type: "POST",
url: "edRegisterPage.aspx/SetSession",
data: JSON.stringify(args),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
var url = "/edRegisterPage.aspx";
window.location.href = url;
},
error: function () {
alert("Fail");
}
});
}
});
} else {
alert('Login Failed!');
}
}, { scope: 'publish_stream,email' });
}
</script>
use this code to where you want to redirect user after login,suppose we redirect user to register page.because user details fill automatically after login with facebook.
[WebMethod(EnableSession = true)]
public static void SetSession(string SNfirstName, string SNuserId, string SNlastName, string SNuserEmail)
{
HttpContext.Current.Session["SNfirstName"] = SNfirstName;
HttpContext.Current.Session["SNuserId"] = SNuserId;
HttpContext.Current.Session["SNlastName"] = SNlastName;
HttpContext.Current.Session["SNuserEmail"] = SNuserEmail;
}
If user success then redirect user to home screen else,throw massage unable to login.
Hope,this code will help you.