You can easily login to your site using Facebook, Twitter, Google or Microsoft with just a few lines of code.
Procedure on how it is done
Before you can add a Facebook login to your web application you will need to register a new Facebook application. Head over to the Facebook Developer page and select “Create a New App” from the Apps menu at the top of the page.
Create App
Create an app using the following procedure.
Step 1
Click "Add a New App".
Step 2
Choose the following option depending on your requirements.
Step 3
Provide App Name.
Step 4
Choose Category.
Step 5
Provide the Site URL.
Step 6
Click on the “Settings” menu in the left hand navigation bar.
Step 7
Take note of the values for the “App ID” and “App Secret” fields since you will need these when enabling the Facebook login in your ASP.NET MVC application. To view the App Secret click on the “Show” button next to the app secret and add your Contact Email.
Now Enabling Facebook authentication in your ASP.NET MVC Application
The next step is to add the Facebook login to your ASP.NET MVC application. For this we will create a new ASP.NET MVC application using Visual Studio. Go to "File" -> "New" -> "Project..." and select the template for a new “ASP.NET Web Application” and click “OK”.
Next, select the Internet Application template.
Now open the AuthConfig.cs file.
Copy and paste the appid and secret into the authconfig file.

Run the application. Click on Login.
Click on the Facebook Button and you are logged in perfectly. But you are not getting the Email of the user. That is the most important thing so scope=email while redirecting to Facebook of the dialog. If you need additional fields that require user permission then them as parameters.
In the second function I am creating my own AuthenticationResult by calling functions from my custom Facebook class.
Facebook.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using MvcApplication1Demo.Extensions;
- using MvcApplication1Demo.Include;
- using System.Web.Mvc;
- namespace MvcApplication1Demo.Include
- {
- public class Facebook
- {
- public string Facebook_GraphAPI_Token = "https://graph.facebook.com/oauth/access_token?";
- public string Facebook_GraphAPI_Me = "https://graph.facebook.com/me?";
- public string AppID = "YOUR APP ID";
- public string AppSecret = "YOUR SECRET KEY";
- public IDictionary<string, string> GetUserData(string accessCode, string redirectURI)
- {
- string token = Web.GetHTML(Facebook_GraphAPI_Token + "client_id=" + AppID + "&redirect_uri="+ HttpUtility.HtmlEncode(redirectURI) + "%3F__provider__%3Dfacebook" + "&client_secret=" + AppSecret +"&code=" + accessCode);
- if (token == null || token == "")
- {
- return null;
- }
- string data = Web.GetHTML(Facebook_GraphAPI_Me +"fields=id,name,email,username,gender,link&access_token=" + token.Substring("access_token=", "&"));
- // this dictionary must contains
- var userData = new Dictionary<string, string>();
- userData.Add("id", data.Substring("\"id\":\"", "\""));
- userData.Add("username", data.Substring("username\":\"", "\""));
- userData.Add("name", data.Substring("name\":\"", "\""));
- userData.Add("link", data.Substring("link\":\"", "\"").Replace("\\/","/"));
- userData.Add("gender", data.Substring("gender\":\"", "\""));
- userData.Add("email", data.Substring("email\":\"", "\"").Replace("\\u0040", "@"));
- userData.Add("accesstoken", token.Substring("access_token=", "&"));
- return userData;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Net;
- using System.IO;
- namespace MvcApplication1Demo.Include
- {
- public static class Web
- {
- //
- // Get HTML
- // Author : Jitendra Gangwar
- // Date : Dec 18 2014
- // Modified : Dec 18 2014
- // Changed To Static Method
- //
- public static string GetHTML(string URL)
- {
- string connectionString = URL;
- try
- {
- System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(connectionString);
- myRequest.Credentials = CredentialCache.DefaultCredentials;
- //// Get the response
- WebResponse webResponse = myRequest.GetResponse();
- Stream respStream = webResponse.GetResponseStream();
- ////
- StreamReader ioStream = new StreamReader(respStream);
- string pageContent = ioStream.ReadToEnd();
- //// Close streams
- ioStream.Close();
- respStream.Close();
- return pageContent;
- }
- catch (Exception)
- {
- }
- return null;
- }
- }
- }

Lodhi SohilkhanPosted Jan 16, 2023, 9:32 AM
Please working source Code link
Shekhar ShetePosted Aug 6, 2020, 8:51 AM
How to get accessCode and call the method GetUserData() ?
sheshagiri pattarPosted Oct 9, 2018, 7:54 AM
How to call facebook class for extended authentication sir? please expalin in brief with example thanking you
Shubham JainPosted Apr 15, 2017, 4:16 PM
Ice article at: http://nobreakpoint.com/facebook-authentication-asp-net-mvc/
Shailesh BankarPosted Jan 12, 2017, 9:14 AM
Hi, Please tell me how to get user email after log in to facebook as you've said using- "scope=email"?
Anu VPosted Dec 17, 2016, 5:51 AM
Nice article thanks for sharing..
Manav PandyaPosted Dec 9, 2016, 12:39 PM
What is Extention and Include
Manav PandyaPosted Dec 9, 2016, 12:36 PM
Getting error in Facebook.cs class file Jitendra Gangwar sir
Vithal WadjePosted Dec 25, 2014, 10:06 PM
good start keep it up
Jitendra GangwarPosted Dec 25, 2014, 1:52 PM
Thanks......
K P Singh ChundawatPosted Dec 25, 2014, 11:54 AM
Nice article
Guest UserPosted Dec 24, 2014, 8:48 PM
Welcome Jitendra! Good start...
Jitendra GangwarPosted Dec 24, 2014, 12:39 PM
Thanks.....
Dinesh BeniwalPosted Dec 24, 2014, 2:03 AM
Good start Jitendra, Welcome to C# Corner.