Facebook login button in asp.net

Posted by Prakashrajmca under ASP.NET on 1/18/2012 | Points: 10 | Views : 2590 | Status : [Member] | Replies : 1
Hi All,

I'm going to implement "logging in a webpage using facebook account".
Any user with facebook account can log-in the webpage.
How to implement it in VB.net
It will be better if you give me step by step flow, since i'm new to asp.net

Thanks in advance.


Posted by: Hemanthlaxmi on: 1/19/2012 [Member] Starter | Points: 25

First,create a application in facebook
for this click the below url


Next enter your application name site name textbox

Next,enter the localhost url in siteurl

Next,click on the Create application button

Then it displays AppID,AppName,AppURL just copy the codes

Next,open your aspnet web application project

Next,Open the Login.aspx.cs page and add these two namespaces

using System.Text.RegularExpressions;

using System.Net;

Next,Add this below code

private string FacebookLogin()
string appId = "Your App ID";
string cookieValue, accessToken, uid, url, userInformation, email = null;
Regex Values;
Match infoMatch;
MatchCollection valuesCollection;
WebClient client;

// Get the cookie
cookieValue = Request.Cookies["fbs_" + appId].Value;

// Get the values
Values = new Regex(@"(?<==)[^&]+");
valuesCollection = getValues.Matches(cookieValue);

/* The access_token and uid parameters are the first,
respectively last, in the cookie */
accessToken = valuesCollection[0].ToString();
uid = valuesCollection[5].ToString().Replace(@"""", "");

// Build the URL and download it
url = "https://graph.facebook.com/" + uid + "?access_token=" + accessToken;
client = new WebClient();
userInformation = client.DownloadString(url);

// Get the email address
Values = new Regex("(?<=\"email\":\")(.+?)(?=\")");
infoMatch = Values.Match(userInformation);
email = infoMatch.Value;
catch (Exception) { }

return email;

Next,Open the Design Page of Login.aspx and add the below code

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">

<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<p><fb:login-button autologoutlink="true">Login via Facebook</fb:login-button></p>

<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({appId: 'your app ID', status: true, cookie: true,
xfbml: true});
(function() {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol +
e.async = true;

If this helps you .
Please "Mark as Answer"

Prakashrajmca, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response