Introduction

This article explains how to access the social providers in a MVC based ASP.NET Web Application. Facebook is used for the example of a social provider. There are various types of social providers available to be accessed, such as Google, Twitter and Microsoft. You can access your information in your social account such as you can access your friend's photos in your Facebook account.

In that context, I am creating a MVC based ASP.NET Web Application and I am also assuming that you have the knowledge necessary to configure the social login provider in your Web Application. You can visit Configure the External Login Options in MVC as a reference to access the external authentication for your ASP.NET Web Application.

Prerequisites

The following are the prerequisites:

Now let's follow the sections mentioned below to proceed:

Facebook Authentication

To authenticate the Facebook provider, use the following procedure.

Step 1: In your Solution Explorer, open the Startup authentication file as shown below:

Startup Authentication File

Step 2: Find the ConfigureAuth() method and modify your code with the following code:

  1. //app.UseTwitterAuthentication(
  2. // consumerKey: "",
  3. // consumerSecret: "");
  4. List<string> Social = new List<string>() { "email", "friends_about_me", "friends_photos" };
  5. var FbFriends = new FacebookAuthenticationOptions();
  6. FbFriends.Scope.Add("email");
  7. FbFriends.Scope.Add("friends_about_me");
  8. FbFriends.Scope.Add("friends_photos");
  9. FbFriends.AppId = "App ID Value";
  10. FbFriends.AppSecret="App Secret Value";
  11. FbFriends.Provider=new FacebookAuthenticationProvider()
  12. {
  13. OnAuthenticated = async FbContext =>
  14. {
  15. FbContext.Identity.AddClaim(
  16. new System.Security.Claims.Claim("FacebookAccessToken", FbContext.AccessToken));
  17. }
  18. }; FbFriends.SignInAsAuthenticationType=DefaultAuthenticationTypes.ExternalCookie;
  19. app.UseFacebookAuthentication(FbFriends);
  20. //app.UseGoogleAuthentication();

In the code above, FacebookAuthenticationProvider() is called each time the user authenticates with Facebook and the data is to be stored in the FbContext.FacebookAccessToken to access the friends of the user.

Controller and Model Editing

Now we need to edit our Controller and Model to access the information of User Facebook Account. Use the following procedure to do that.

Step 1: At first open your Login file to create an Action.

Shared Files in MVC

Step 2: Modify your code with the following code:

  1. <ul class="nav navbar-nav navbar-right">
  2. <li>
  3. @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
  4. </li>
  5. <li>
  6. @Html.ActionLink("Facebook Friends", "FacebookFriends", "Account")
  7. </li>
  8. <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
  9. </ul>

Step 3: Now open the AccountController.cs file from the Solution Explorer and proceed with the following sections to edit the controller:

Adding View

The final step is to add the View to access Facebook. So, proceed with the following procedure.

Step 1: Add a View to your Account Folder:

Adding View in MVC

Step 2: In the wizard, enter the View name:

Add View Wizard

Step 3: Replace the boilerplate code with the following code:

  1. @model IList<FbFriendsApp.Models.FacebookViewModel>
  2. @{
  3. ViewBag.Title = "Facebook Friends";
  4. }
  5. <h2>My Facebook Friends</h2>
  6. @if (Model.Count > 0)
  7. {
  8. <div class="row">
  9. @foreach (var MyFriend in Model)
  10. {
  11. <div class="col-md-3">
  12. <a href="#" class="thumbnail">
  13. <img [email protected] [email protected] />
  14. </a>
  15. </div>
  16. }
  17. </div>
  18. }

Access Application

Use the following procedure to run the application.

Step 1: Click on the LogIn link on your Home Page:

Mvc Application Login

Step 2: Click on Facebook to proceed.

Application LogIn With Facebook

Step 3: Enter the Facebook Credentials.

Facebook Credentials

Step 4: Authenticate the Facebook App.

Facebook App Authentication

Step 5: Register yourself and click on the Facebook Friends to proceed.

Facebook Provider

Step 6: Now you can view your Facebook Friends in your Web Application.

Facebook Friends

Step 7: Please log off.

That's it.

Summary

This article will help you to access social providers such as Facebook in your ASP.NET Web Application. You can also create views to display them in a sleek way. Thanks for reading.