Integrating External Login (Facebook) In Your MVC Project

As external login is very important and useful to integrate in your project. In one of my article i described why actually we need social Login in our projects. You can check here in the following link.
Here, I am explaining how to integrate Facebook Login in our Project.

Step 1: Create an MVC project. Run the project and go to Login section, In Login section you will find the following view:
 
 

Step 2: Now go to the solution explorer App_Start and StartUp.Auth.cs

 

Step 3: Now open the StartUp.Auth.cs You will find the following commented code. Now as we are going to integrate facebook login  just uncomment the Facebook section as in the following code. 
  1. public partial class Startup  
  2. {  
  3.     // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864  
  4.     public void ConfigureAuth(IAppBuilder app)  
  5.     {  
  6.         // Enable the application to use a cookie to store information for the signed in user  
  7.         app.UseCookieAuthentication(new CookieAuthenticationOptions  
  8.         {  
  9.             AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,  
  10.                 LoginPath = new PathString("/Account/Login")  
  11.         });  
  12.         // Use a cookie to temporarily store information about a user logging in with a third party login provider  
  13.         app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);  
  14.         // Uncomment the following lines to enable logging in with third party login providers  
  15.         //app.UseMicrosoftAccountAuthentication(  
  16.         // clientId: "",  
  17.         // clientSecret: "");  
  18.         //app.UseTwitterAuthentication(  
  19.         // consumerKey: "",  
  20.         // consumerSecret: "");  
  21.         app.UseFacebookAuthentication(appId: "", appSecret: "");  
  22.         // app.UseGoogleAuthentication();  
  23.     }  
  24. }  
Just save your code and run the project and go to Login Section. Now you will see the login option for Facebook is available.

 

Step 4: Now we need the appId and appsecreat for login, so now go to the following url in Facebook as in the following figure.

 

Step 5: Now here click MyApps and Add a New App. Now you will get the following popup page.

 

Now here choose www(website), it will ask for the name as in the following:
 
 

Now here i am giving my website name as CodeX. After that press SkipQuick Start. It will redirect to the following page.

 

Now to check the App Secret click the show button. It may ask to re-enter your password.

 

Now after entering your password you will get your app secret.

 
 
Now copy the appId and App secret and paste there. 
  1. public void ConfigureAuth(IAppBuilder app)  
  2. {  
  3.     app.UseCookieAuthentication(new CookieAuthenticationOptions  
  4.     {  
  5.         AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,  
  6.             LoginPath = new PathString("/Account/Login")  
  7.     });  
  8.     // Use a cookie to temporarily store information about a user logging in with a third party login provider  
  9.     app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);  
  10.     app.UseFacebookAuthentication(appId: "1069827226391314", appSecret: "963d5782b2a66880be952043cd89****");  
  11. }  
Now run the application and you will get the following error:

 

Now check your Localhost URL and set it in the developers.facebook page as in the following:

 

Now just update "Microsoft.Owin.security.Facebook" as in the following:

 

The code for integrating is already provided by identity framework. Just run the project and click the facebook button.

 

Now you will be able to login your application using facebook credentials.

 

Now if you check the details in "ASP.NetUserLogin" table you will get the following information.

 

Here your information is saved.

So in this way we can use Facebook to login with our application. 

Next Recommended Readings