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=", "&"));
-
-
- 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;
- }
- }
- }
Class file web to read HTML from URL:
- 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
- {
-
-
-
-
-
-
-
- public static string GetHTML(string URL)
- {
- string connectionString = URL;
-
- try
- {
- System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(connectionString);
- myRequest.Credentials = CredentialCache.DefaultCredentials;
-
- WebResponse webResponse = myRequest.GetResponse();
- Stream respStream = webResponse.GetResponseStream();
-
- StreamReader ioStream = new StreamReader(respStream);
- string pageContent = ioStream.ReadToEnd();
-
- ioStream.Close();
- respStream.Close();
- return pageContent;
- }
- catch (Exception)
- {
- }
- return null;
- }
- }
- }
Note: I hope this article is helpful for you. Please Like and Share it.