1
Reply

How to login from facebook into our asp.net web application

Gurupadagouda Karakanagoudar

Gurupadagouda Karakanagoudar

Feb 9 2015 5:26 AM
930
How to login from facebook into our asp.net web application , i have loaded javascript sdk , and written all the required code to execute it but the facebook login button is working for only Facebook to get logged in , but it not logging into my application , what should be done to make it login into my app. I am using visual studio 2010 to build my app ... ,
 
in simple problem is  iam able to login into facebook account by clicking Facebook login button , but unable to use it to login into my site..
 
here is the code..
 
 
<script type="text/javascript" src="layout/scripts/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="layout/scripts/all.js"></script>
<script type="text/javascript">
$("document").ready(function () {
// Initialize the SDK upon load
FB.init({
appId: '890425210989293', // App ID
channelUrl: '//' + window.location.hostname + '/Home.aspx', // Path to your Channel File
scope: 'id,name,gender,user_birthday,email', // This to get the user details back from Facebook
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true, // parse XFBML
apikey: "306c09edf6e7405c5b37f50d5650cd53"
});
// listen for and handle auth.statusChange events
// FB.Event.subscribe('auth.statusChange', OnLogin)
FB.Event.subscribe('auth.authResponseChange', function (response) {
if (response.status === 'connected') {
alert("Logged in.. Redirecting you now...");
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
// TODO: Handle the access token
// Do a post to the server to finish the logon
// This is a form post since we don't want to use AJAX
var form = document.createElement("form");
form.setAttribute("method", 'post');
form.setAttribute("action", '/FacebookLogin.ashx');
var field = document.createElement("input");
field.setAttribute("type", "hidden");
field.setAttribute("name", 'accessToken');
field.setAttribute("value", accessToken);
form.appendChild(field);
document.body.appendChild(form);
form.submit();
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
});
FB.login(function (response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
console.log('Good to see you, ' + response.name + '.'+response.accessToken+'.'+response.email+'.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
// This method will be called after the user login into facebook.
function OnLogin(response) {
if (response.authResponse) {
FB.api('/me?fields=id,name,gender,email,birthday', LoadValues);
// window.open(http://localhost:55160/WithFlyingColors/Home.aspx);
}
}
//This method will load the values to the labels
function LoadValues(me) {
if (me.name) {
document.getElementById('displayname').innerHTML = me.name;
document.getElementById('FBId').innerHTML = me.id;
document.getElementById('DisplayEmail').innerHTML = me.email;
document.getElementById('Gender').innerHTML = me.gender;
document.getElementById('DOB').innerHTML = me.birthday;
document.getElementById('auth-loggedin').style.display = 'block';
}
}
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected')
{
// Logged into your app and Facebook.
{
FB.api('/me', { fields: 'first_name' }, function (response) {
console.log(response);
// window.open(http://localhost:55160/WithFlyingColors/Home.aspx);
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML ='Thanks for logging in, ' + response.name + '!';
});
}
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
function checkLoginState() {
FB.getLoginStatus(function (response) {
statusChangeCallback(response);
});
}
FB.getLoginStatus(function (response) {
statusChangeCallback(response);
});
</script>
 
<div id="Div1"></div> <!-- This initializes the FB controls-->
<div class="fb-login-button" id="fb_button" data-max-rows="1" data-size="xlarge" data-show-faces="false" data-auto-logout-link="true" scope="user_birthday,email">
Login with Facebook
</div> <!-- FB Login Button -->
<!-- Details -->
<div id="Div2">
<div id="Div3" style="display: none">
Hi, <span id="Span1"></span><br/>
Your Facebook ID : <span id="Span2"></span><br/>
Your Email : <span id="Span3"></span><br/>
Your Sex:, <span id="Span4"></span><br/>
Your Date of Birth :, <span id="Span5"></span><br/>
</div>
</div>
 

Answers (1)