Language

ASP.NET MVC Facebook Birthday App

By Kirthi Krishnamraju, Rick Anderson, Yao Huang Lin, Troy Dai and Tom Dykstra|

The Facebook App template includes a new library to take care of all the plumbing involved in building a Facebook app, so you can focus on building the business logic in your application. The Facebook app -- called a Canvas app -- that you can build with this new template is hosted on the web and is displayed inside the Facebook chrome via an iframe. The functionality included in the template will help you with authentication, permissions, and accessing Facebook data. The Facebook functionality also has an extensibility model so that you can build your own storage providers and more.

A Visual Studio project with C# source code is available to accompany this tutorial. Download the project.

What you’ll build

You'll implement a simple but fun and useful Facebook application that lists a person's Facebook friends who have upcoming birthdays and makes suggestions for birthday presents. Here are screenshots, one that shows the list of friends with upcoming birthdays and one with recommended birthday presents which appears when you click a friend:


 

Creating the project and Installing ASP.NET Facebook Application template

  1. Start by installing and running Visual Studio Express 2013 for Web or Visual Studio 2013.   Install Visual Studio 2013 Update 3  or higher.
  2. The Facebook template included with  previous versions of Visual Studio 2013 has been removed. The Facebook template is now available as a VSIX extension here. Download and install the NNN.ASP.NET Facebook Application.vsix file.
  3. From the File menu, click New and then click Project.
  4. In the New Project dialog box, expand Visual C# and then click Web under Templates.
  5. Click ASP.NET Facebook Application.
  6. Enter SampleFacebookBirthdayApp for the project name, and then click OK.
    Note: You must use SampleFacebookBirthdayApp for the project name so you can copy files from the download project without having to change the namespace
  7. Install the latest Microsoft.AspNet.Facebook Nuget package.

Setting up SSL in the Project

To connect to Facebook, you will need to set up IIS-Express to use SSL.

  1. In Solution Explorer, click the SampleFacebookApp project.
  2. Enter the F4 key to show the project properties. Alternatively, from the View menu you can select Properties Window.
  3. Change SSL Enabled to True.


  4. Copy the SSL URL.
  5. In Solution Explorer, right click the SampleFacebookApp and select Properties.
  6. Select the Web tab.


  7. Paste the SSL URL into the Project Url box, then click Create Virtual Directory. You will need this URL to configure Facebook.

Creating the app in Facebook and connecting the app to the project

Right out of the box, the Facebook template provides boilerplate code to help you connect your application to Facebook and retrieve Facebook properties such as Likes, photos, and email. All you have to do to get a simple application up and running is copy to your project some settings from an application that you create in Facebook.

  1. In your browser, navigate to https://developers.facebook.com/apps and log in by entering your Facebook credentials.
  2. Click on Add a New App



  3. Select Facebook Canvas


  4. Enter a name for the app and click on Create New Facebook App ID


  5. Choose a category for the app and click Confirm


  6.  In Visual Studio, open the application. Go to Solution Explorer, right-click the project and click Properties, and then click the Web tab.
  7.  Copy the Project URL from the Web tab and paste it into the Secure Host URL field and click Next.


  8. Click Next.


  9. Click on Skip to Developer Dashboard


  10. Click on Settings and enter a value in Namespace. Copy Secure Canvas URL value to Canvas URL and click on Save Changes.


  11.  In Visual Studio, open the app Web.config file that is located in the root folder of your project.
  12.  Copy and paste the AppId, App Secret, and Namespace values into the corresponding key elements in the appSettings collection in the Web.config file:
    <configuration>
      <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
        <add key="Facebook:AppId" value="" />
        <add key="Facebook:AppSecret" value="" />
        <add key="Facebook:AppNamespace" value="" />
        <add key="Facebook:AuthorizationRedirectPath" value="~/Home/Permissions" />
        <add key="Facebook:VerifyToken:User" value="" />
      </appSettings>
      <system.web>
    Security Note: Never store sensitive data in your source code. The account and credentials are added to the code above to keep the sample simple. See Jon Atten's ASP.NET MVC: Keep Private Settings Out of Source Control.

  13. Press CTRL+F5 to run the application. If you haven't installed the self-signed certificate for IIS Express, follow the instructions to install it. For more information see the Enable SSL for the Project section of my Deploy a Secure ASP.NET MVC 5 app with Membership, OAuth, and SQL Database to an Azure Website tutorial.
  14. After you log in, Facebook will display the following dialog boxes which asks you for permission to receive your public profile, email address and photos. Click Okay on both dialogs.




  15. You now have a simple working facebook app.


Examining the template code

This section of the tutorial walks you through the code that was created by the Facebook template. If you prefer to just get started building the birthday application, you can skip to the next section.
You saw the main page that was displayed by the template application. It was displayed by the Home controller’s Index action method.

public class HomeController : Controller
{
   [FacebookAuthorize("email", "user_photos")]
   public async Task<ActionResult> Index(FacebookContext context)
   {
      if (ModelState.IsValid)
      {
         var user = await context.Client.GetCurrentUserAsync<MyAppUser>();
         return View(user);
      }

      return View("Error");
   }

   // This action will handle the redirects from FacebookAuthorizeFilter when
   // the app doesn't have all the required permissions specified in the FacebookAuthorizeAttribute.
   // The path to this action is defined under appSettings (in Web.config) with the key 
   // 'Facebook:AuthorizationRedirectPath'.
   public ActionResult Permissions(FacebookRedirectContext context)
   {
      if (ModelState.IsValid)
      {
         return View(context);
      }

      return View("Error");
   }
}

Notice that the Index action method is an asynchronous method. Because this method calls a web service to get Facebook data, there will be some latency. Making the method asynchronous enables the server to process high traffic loads more efficiently. For more information about asynchronous methods in ASP.NET MVC, see my Using Asynchronous Methods in ASP.NET MVC tutorial.

The FacebookAuthorize attribute on the Index method is what causes the Permissions page to be displayed first when your application runs and the user hasn't given it permission yet. See Facebook best practices for requesting permissions. You use this attribute to specify the Facebook data that your application needs permission to retrieve. The Web.config file has a setting that specifies the URL to use when the application doesn't have the required permissions:

<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
     <!-- Other settings removed for clarity -->    
    <add key="Facebook:AuthorizationRedirectPath" value="~/Home/Permissions"/>
    <add key="Facebook:VerifyToken:User" value=""/>
  </appSettings>

The MVC model binder provides the Permissions method with a FacebookRedirectContext object that encapsulates information about the request, including the requested permissions:

public class FacebookRedirectContext
{
   public FacebookRedirectContext();

   public FacebookConfiguration Configuration { get; set; }
   public string OriginUrl { get; set; }
   public string RedirectUrl { get; set; }
   public string[] RequiredPermissions { get; set; }
}

The Views\Home\Permissions.cshtml view displays the requested permissions:

@using Microsoft.AspNet.Facebook
@model FacebookRedirectContext
@{
    ViewBag.Title = "Required Permissions";
}

@if (Model.RequiredPermissions.Length > 0)
{
    <h3>You need to grant the following permission(s) on Facebook to view this page:</h3>
    <ul>
        @foreach (var permission in Model.RequiredPermissions)
        {
            <li>@permission</li>
        }
    </ul>
    <a class="buttonLink" href="@Html.Raw(Model.RedirectUrl)" target="_top">Authorize this application</a>

For the Index method that displays the main application page, the MVC model binder provides a FacebookContextobject that encapsulates information about the request:

public class FacebookContext
{
   public FacebookContext();

   public string AccessToken { get; set; }
   public FacebookClient Client { get; set; }
   public FacebookConfiguration Configuration { get; set; }
   [Dynamic]
   public dynamic SignedRequest { get; set; }
   public string UserId { get; set; }
}

The FacebookClient object that is included in the context object provides methods you can use to get Facebook data about the user. The template code in the Index method specifies that it wants a MyAppUser object when it calls FacebookClient.GetCurrentUserAsync.

public class HomeController : Controller
{
   [FacebookAuthorize("email", "user_photos")]
   public async Task<ActionResult> Index(FacebookContext context)
   {
      if (ModelState.IsValid)
      {
         var user = await context.Client.GetCurrentUserAsync<MyAppUser>();
         return View(user);
      }

      return View("Error");
   }

The MyAppUser class specifies the data to retrieve for the user of the application:

public class MyAppUser
{
   public string Id { get; set; }
   public string Name { get; set; }
   public string Email { get; set; }

   [JsonProperty("picture")] // This renames the property to picture.
   [FacebookFieldModifier("type(large)")] // This sets the picture size to large.
   public FacebookConnection<FacebookPicture> ProfilePicture { get; set; }

   // This sets the size of the friend list to 8, remove it to get all friends.
   [FacebookFieldModifier("limit(8)")]
   public FacebookGroupConnection<MyAppUserFriend> Friends { get; set; }

   // This sets the size of the photo list to 16, remove it to get all photos.
   [FacebookFieldModifier("limit(16)")]
   public FacebookGroupConnection<FacebookPhoto> Photos { get; set; }
}

The Index view displays the following:

@using WebApplication3.Models
@using Microsoft.AspNet.Facebook.Models
@model MyAppUser
@{
    ViewBag.Title = "Home Page";
}

<article class="intro">
    <span id="profilePicture">
        @if (Model.ProfilePicture != null && Model.ProfilePicture.Data != null)
        {
            <img src="@Model.ProfilePicture.Data.Url" />
        }
    </span>
    <h3>Welcome @Model.Name</h3>
    <label>Email: @Model.Email</label>
</article>

<article id="content">
    <div class="left">
        <h4>Friends</h4>
        @if (Model.Friends != null && Model.Friends.Data != null && Model.Friends.Data.Count > 0)
        {
            foreach (var myFriend in @Model.Friends.Data)
            {
            <a href="@myFriend.Link" target="_blank">
                <div class="photoTile">
                    <label>@myFriend.Name</label>
                    @if (myFriend.Picture != null && myFriend.Picture.Data != null)
                    {
                        <img src="@myFriend.Picture.Data.Url" />
                    }
                </div>
            </a>
            }
        }
        else
        {
            <p>No friends found.</p>
        }
    </div>
    <div class="right">
        <h4>Photos</h4>
        @if (Model.Photos != null && Model.Photos.Data != null && Model.Photos.Data.Count > 0)
        {
            foreach (var photo in @Model.Photos.Data)
            {
            <a href="@photo.Link" target="_blank">
                <div class="photoTile">
                    <img src="@photo.ThumbnailUrl" />
                </div>
            </a>
            }
        }
        else
        {
            <p>No photo available.</p>
        }
    </div>
</article>

The model passed to this view is a MyAppUser object. The <article class="intro"> element displays the user's name, email address, and picture. The <article id="content"> element displays the user's friends in the left div and the user's photos in the right div.

Creating the Facebook birthday app

This tutorial will show the steps for creating the birthday application by using files from the completed project that you can download by using the link at the top of the page. If you haven't already downloaded the project, download it now before continuing with the tutorial.

Note: If you just want to run the downloaded application without going through the following steps to build it in your own project, you have to configure the Facebook application settings as shown in the previous section, and configure the Shop Style API key as shown later in this tutorial. You may also need to enable NuGet package restore. To do this, click Library Package Manager from the Tools menu, and then click Manage NuGet Packages for Solution. In the Manage NuGet Packages dialog, click Restore. (If you have already enabled NuGet package restore, you won't see the yellow bar with the Restore button.)

Install Bootstrap

You'll begin by installing the Bootstrap NuGet package. Bootstrap is a popular and powerful front-end framework than enables faster and easier web development. We used it in this application because it allows you to easily create cool layouts that work great on desktops, tablets, and smartphones without having to learn or fight CSS. See the Bootstrap Getting Started guide for more information.

  1. From the Tools menu, click Library Package Manager, and then click Manage NuGet Packages for Solution.


  2. In the left tab of the Manage NuGet Packages dialog box, click Online.
  3. In the search box at the top right, enter bootstrap, and then press Enter.
  4. Install the Bootstrap package. The Bootstrap NuGet package installs the following files:
    bootstrap-responsive.css and bootstrap.css (and the minified versions).
    bootstrap.js (and the minified version).
  5. Add the Bootstrap CSS and JavaScript files to the App_Start\BundleConfig.cs file. The completed BundleConfig.cs file looks like this:
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));
    
        // Use the development version of Modernizr to develop with and learn   
        // from.Then, when you're ready for production, use the build tool at 
        // http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));
    
        bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css",
            "~/Content/bootstrap.css", "~/Content/bootstrap-responsive.css"));
    
            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                          "~/Scripts/bootstrap.js",
                          "~/Scripts/respond.js"));
    }
  6. Delete the Views\Shared\_Layout.cshtml file and add in its place the same file from the downloaded project. To add the file, right-click the Views\Shared folder and click Add Existing Item, and then navigate to the downloaded version of the Views\Shared\_Layout.cshtml file. The updated layout file is shown below, with the changes highlighted:
    @using Microsoft.AspNet.Facebook
    <!DOCTYPE html>
    <html lang="en">
    <head>
       <meta charset="utf-8" />
       <title>@ViewBag.Title - My ASP.NET MVC Application</title>
       @Styles.Render("~/Content/css")
       @Scripts.Render("~/bundles/modernizr")
       @Scripts.Render("~/bundles/jquery")
       @Scripts.Render("~/bundles/bootstrap")
       @Scripts.Render("~/bundles/app")
    </head>
    <body>
       <script>
          window.fbAsyncInit = function () {
             FB.init({
                appId: '@GlobalFacebookConfiguration.Configuration.AppId', // App ID
                status: true, // check login status
                cookie: true, // enable cookies to allow the server to access the session
                xfbml: true  // parse XFBML
             });
          };
    
          // Load the SDK Asynchronously
          (function (d) {
             var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
             if (d.getElementById(id)) { return; }
             js = d.createElement('script'); js.id = id; js.async = true;
             js.src = "//connect.facebook.net/en_US/all.js";
             ref.parentNode.insertBefore(js, ref);
          }(document));
       </script>
    
       <div id="wrapper">
          <nav class="navbar navbar-default" role="navigation">
             <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                   <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                      <span class="sr-only">Toggle navigation</span>
                      <span class="icon-bar"></span>
                      <span class="icon-bar"></span>
                      <span class="icon-bar"></span>
                   </button>
                   @*<a target="_top" href="@GlobalFacebookConfiguration.Configuration.AppUrl@Url.Action("Index", "Home")" class="navbar-brand">Birthday App</a>*@
                   <a target="_top" href="@ViewBag.AppUrl@Url.Action("Index", "Home")" class="navbar-brand">Birthday App</a>
                </div>
                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                   <ul class="nav navbar-nav">
                      <li class="active"><a target="_top" href="@ViewBag.AppUrl@Url.Action("Index", "Home")">Home</a></li>
                      <li><a href="@Url.Action("About", "Home")">About</a></li>
                   </ul>
                   <form class="navbar-form navbar-right" role="search" action="@Url.Action("Search", "Home")" method="get">
                      <div class="form-group">
                         <input type="text" class="form-control" name="friendName" placeholder="Friend's name">
                      </div>
                      <button type="submit" class="btn btn-default">Search</button>
                   </form>
                </div><!-- /.navbar-collapse -->
             </div><!-- /.container-fluid -->
          </nav>
          <header id="topHeader">
             <h1>Birthday App</h1>
             <h2>Time to buy your friend a birthday present.</h2>
          </header>
          @RenderBody()
          <footer>
             <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
          </footer>
       </div>
       @RenderSection("scripts", required: false)
    </body>
    </html>
    

    The markup inside the wrapper div uses Bootstrap to create a navigation bar at the top of the page that enables users to go to different application pages or search for specific friends. For more information about this use of Bootstrap, see Twitter Bootstrap 101: The Navbar and the Bootstrap navbar help pages.

  7. Open Content\Site.css and remove the margin line from the h1 definition.
        header h1 {
            font-size: 80px;
            color: #004C66;
            /*margin: 0;*/
            padding-top: 20px;
            font-weight: normal;
        }
    This change prevents the h1 and h2 headings from overlapping.
  8.  Run the app and you'll see the new navigation bar and the new title text.



Change the way the app user and friends are displayed

The MyAppUser class determines what information is gathered from Facebook about the application user, and the MyAppUserFriend class determines what information is gathered about the user's friends. In this section of the tutorial you change those classes and then change the Index view that displays the user and friend info.

  1. Open the Models\MyAppUser.cs file.
  2. Comment out the FacebookFieldModifier attribute on the ProfilePicture field. The template uses this attribute to set the profile picture to large size, but the user of your app doesn't need to see a large picture of  herself.
  3. Comment out the FacebookFieldModifier attribute on the Friends field. The template uses this attribute to limit the number of friends displayed to 8, but you'll retrieve all of the friends. (After you sort them by birthday, you'll limit the display to the first 100 friends to prevent the list from getting too long to display).
  4.  Comment out the Photos property and its FacebookFieldModifier attribute. The birthday app doesn't need to display the user's photos.

    When you're done, the code will look like the following example :
    public class MyAppUser
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    
        [JsonProperty("picture")] // This renames the property to picture.
        public FacebookConnection<FacebookPicture> ProfilePicture { get; set; }
    
        // This sets the size of the friend list to 8, remove it to get all friends.
        [FacebookFieldModifier("limit(8)")] 
        public FacebookGroupConnection<MyAppUserFriend> Friends { get; set; }
    
        // This sets the size of the photo list to 16, remove it to get all photos.
        [FacebookFieldModifier("limit(16)")] 
        public FacebookGroupConnection<FacebookPhoto> Photos { get; set; }
    }
  5.  Delete the Models\MyAppUserFriend.cs file, and add the same file from the downloaded project. The MyAppUserFriend class encapsulates information about one of the application user's Facebook friends. It is used for the initial list of all of the user's friends.
    public class MyAppUserFriend
    {
        public string Id { get; set; }
    
        public string Name { get; set; }
    
        public string Gender { get; set; }
    
        public string Link { get; set; }
    
        public string Birthday { get; set; }
    
        [FacebookFieldModifier("height(100).width(100)")] // This sets the picture height and width to 100px.
        public FacebookConnection<FacebookPicture> Picture { get; set; }
    }
  6. Delete the Views\Home\Index.cshtml file and replace it with the same file from the downloaded project. The new Index view is shown here:
    @using SampleFacebookBirthdayApp.Models
    @using Microsoft.AspNet.Facebook.Models
    @using Microsoft.AspNet.Facebook
    @model MyAppUser
    @{
      ViewBag.Title = "Home Page";
    }
    
    <article class="intro">
      <span id="profilePicture">
        @if (Model.ProfilePicture != null && Model.ProfilePicture.Data != null)
        {
            <img class="img-responsive" src="@Model.ProfilePicture.Data.Url" />
        }
      </span>
      <h3>Welcome @Model.Name</h3>
    </article>
    <br/>
    <br/>
    <article>
      <label>Friends with upcoming birthdays</label>
      @Html.DisplayFor(m => m.Friends.Data, "Friends")
    </article>
    The view displays the user's picture (if available), then displays each of the user's friends by using a Friends.cshtml display template, which you will create next.

  7. Add a DisplayTemplates folder to the Views\Home folder.
  8. Copy the Views\Home\DisplayTemplates\Friends.cshtml file from the downloaded project to your project.

    The Friends template displays the MyAppUserFriend model and is used in the Index view and the Search view. The Friends.cshtml display template is shown here:
    @using SampleFacebookBirthdayApp.Models
    @using Microsoft.AspNet.Facebook.Models
    @using Microsoft.AspNet.Facebook
    
    @model IList<MyAppUserFriend>
    
    <table class="table">
      @foreach (var friend in Model)
      {
        <tr>
          <td>
            <a target="_blank" href="@friend.Link">
              <div class="photoTile">
                <label>@friend.Name</label>
                @if (friend.Picture != null && friend.Picture.Data != null)
                {
                  <img class="img-polaroid" src="@friend.Picture.Data.Url" />
                }
              </div>
            </a>
          </td>
          <td>
            <label>@friend.Birthday</label>
          </td>
          <td>
              <a target="_top" href="@GlobalFacebookConfiguration.Configuration.AppUrl@Url.Action("RecommendGifts", new { friendId = friend.Id })" class="btn btn-success" role="button">
                  @if (friend.Gender == "male")
                  {
                      <span>Buy him a present</span>
                  }
                  else
                  {
                      <span>Buy her a present</span>
                  }
              </a>
          </td>
        </tr>
      }
    </table>
    For each of the application user's friends, this template displays a picture of the friend (if one is available) and provides a link to suggested gifts that you can purchase.
  9. Note: Facebook changed the way their “user_friends” permission works. It used to return all of users friends and now only returns friends that also have your application. Unless you have a Facebook friend who has also installed this app, you will have an empty panel of friends to buy presents for. You can create test Facebook accounts to test this feature.

Modify the app to support birthday gift suggestions

  1.  Replace the Controllers\HomeController.cs file in your project with the same file from the downloaded project.
    public class HomeController : Controller
    {
       [FacebookAuthorize("friends_birthday", "user_friends")]
       public async Task<ActionResult> Index(FacebookContext context)
       {
          ViewBag.AppUrl = GlobalFacebookConfiguration.Configuration.AppUrl;
          if (ModelState.IsValid)
          {
             var user = await context.Client.GetCurrentUserAsync<MyAppUser>();
             var friendsWithUpcomingBirthdays = user.Friends.Data.OrderBy(friend =>
             {
                try
                {
                   string friendBirthDayString = friend.Birthday;
                   if (String.IsNullOrEmpty(friendBirthDayString))
                   {
                      return int.MaxValue;
                   }
    
                   var birthDate = DateTime.Parse(friendBirthDayString);
                   friend.Birthday = birthDate.ToString("MMMM d"); // normalize birthday formats
                   return BirthdayCalculator.GetDaysBeforeBirthday(birthDate);
                }
                catch
                {
                   return int.MaxValue;
                }
             }).Take(100);
             user.Friends.Data = friendsWithUpcomingBirthdays.ToList();
             return View(user);
          }
    
          return View("Error");
       }
    
       [FacebookAuthorize("friends_birthday")]
       public async Task<ActionResult> Search(string friendName, FacebookContext context)
       {
          var userFriends = await context.Client.GetCurrentUserFriendsAsync<MyAppUserFriend>();
          var friendsFound = String.IsNullOrEmpty(friendName) ?
              userFriends.ToList() :
              userFriends.Where(f => f.Name.ToLowerInvariant().Contains(friendName.ToLowerInvariant())).ToList();
          friendsFound.ForEach(f => f.Birthday = !String.IsNullOrEmpty(f.Birthday) ? DateTime.Parse(f.Birthday).ToString("MMMM d") : "");
          return View(friendsFound);
       }
    
       [FacebookAuthorize]
       public async Task<ActionResult> RecommendGifts(string friendId, FacebookContext context)
       {
          if (!String.IsNullOrEmpty(friendId))
          {
             var friend = await context.Client.GetFacebookObjectAsync<MyAppUserFriend>(friendId);
             if (friend != null)
             {
                var products = await RecommendationEngine.RecommendProductAsync(friend);
                ViewBag.FriendName = friend.Name;
                return View(products);
             }
          }
    
          return View("Error");
       }
    
       [FacebookAuthorize]
       public ActionResult About()
       {
          return View();
       }
    
       // This action will handle the redirects from FacebookAuthorizeFilter when
       // the app doesn't have all the required permissions specified in the FacebookAuthorizeAttribute.
       // The path to this action is defined under appSettings (in Web.config) with the key 'Facebook:AuthorizationRedirectPath'.
       public ActionResult Permissions(FacebookRedirectContext context)
       {
          if (ModelState.IsValid)
          {
             return View(context);
          }
    
          return View("Error");
       }
    }

    The asynchronous Index action method gets the current Facebook logged on user and populates the MyAppUser model, which contains the user's ID, name, email, profile picture and list of friends. Then it sorts the list of friends by calling the OrderBy extension method. For each friend, it passes to the sort method the number of days until the person's birthday. To calculate the number of days it converts the birthdate to a string that has only the month and the day and then passes that value to a GetDaysBeforeBirthday helper method that you'll create later in the tutorial. If the birthday field doesn't have a valid date (typically because the birthday isn't public), it passes int.MaxValue to the sort method. The result is that all friends with known birthdays appear in birthday order starting with the birthday closest to the current date. Those that haven't made their birthdays public appear at the end of the list.

    The 
    Search action method is called when the user clicks the Search button to find a specific person without having to page through all friends. It gets the value of the string entered in the Search text box, calls the Facebook API to get all of the application user's friends, and then excludes from the list any friends whose names do not contain the search string. If no search string is entered, the entire list of all friends is returned. You'll create the view that displays this list of friends in the next step.

    The 
    RecommendGifts method is called when the user clicks Buy him a present or Buy her a present. It gets the ID of the selected friend, calls the Facebook API to get that friend's information, and calls a RecommendProductAsync helper method to get a list of products based on friend’s gender. You'll create the helper method later in the tutorial, and you'll create the view that displays the products in the next step. 

    Most of the action methods in the home controller are asynchronous. Asynchronous methods can make your web server more efficient when apps  make web service calls (like the Facebook API calls in this application). For more information about using asynchronous methods in ASP.NET MVC, see my  
    Using Asynchronous Methods in ASP.NET MVC tutorial.


  2.  Add the SearchResultModel.cs files from the Models folder of the downloaded project to your Models folder.
    The classes in the SearchResultModel.cs file contain the information that is returned by the Shop style API:
    namespace SampleFacebookBirthdayApp.Models
    {
        public class SearchResult
        {
            public Product[] Products { get; set; }
        }
    
        public class Product
        {
            public string Name { get; set; }
    
            public string Description { get; set; }
    
            public string ClickUrl { get; set; }
    
            public ProductImage Image { get; set; }
    
            public string PriceLabel { get; set; }
        }
    
        public class ProductImage
        {
            public Sizes Sizes { get; set; }
        }
    
        public class Sizes
        {
            public BestImage Large { get; set; }
        }
    
        public class BestImage
        {
            public string Height {get; set;}
    
            public string Url {get; set;}
    
            public string Width {get; set;}
        }
    }

    This hierarchy of classes corresponds to the response format of the Shop style API for Shopping. The API returns much more data than is specified here; only the properties that you are actually going to use are defined. The Product object from this data graph is provided to the RecommendGifts view.

  3. In Solution Explorer, right click the SampleFacebookApp project, click Add, and then click New Folder.Name the folder Helpers.
  4. Right click the Helpers folder and choose Add, and then click Existing Item.
  5. Navigate to the downloaded project and add the three files from the Helpers folder.
    • The BirthdayCalculator helper calculates the number of days before each friend's birthday:
      public static class BirthdayCalculator
      {
         public static int GetDaysBeforeBirthday(DateTime birthDate)
         {
            var today = DateTime.Now;
            var nextBirthday = new DateTime(today.Year, birthDate.Month, birthDate.Day);
            TimeSpan difference = nextBirthday - DateTime.Now;
            if (difference.Days < 0)
            {
               nextBirthday = new DateTime(today.Year + 1, birthDate.Month, birthDate.Day);
               difference = nextBirthday - DateTime.Now;
            }
            return difference.Days;
         }
      }
      The code subtracts the current date from the current year birthday to get the number of days until the birthday. If that number is negative, the birthday in the current year is before the current date, and the number of days is recalculated using the birthday in the next year.
    • The ShoppingSearchClient helper uses the Shop style API for Shopping to search for products that match a user’s gender:
      public static class ShoppingSearchClient
      {
         private const string SearchApiTemplate = "http://api.shopstyle.com/api/v2/products?pid={0}&cat={1}&offset=0&limit=10";
         private static HttpClient client = new HttpClient();
      
         public static string AppKey = ConfigurationManager.AppSettings["Search:AppKey"];
      
         public static Task<SearchResult> GetProductsAsync(string query)
         {
            if (String.IsNullOrEmpty(AppKey))
            {
               throw new InvalidOperationException("Search:AppKey cannot be empty. Make sure you set it in the configuration file.");
            }
      
            query = query.Replace(" ", "+");
            string searchQuery = String.Format(SearchApiTemplate, AppKey, query);
            var response = client.GetAsync(searchQuery).Result.EnsureSuccessStatusCode();
            return response.Content.ReadAsAsync<SearchResult>();
         }
      }
      The ReadAsAsync<SearchResult> method gets the JSON response from the Shop style API and uses it to populate a SearchResult object. You'll set up the key you need in order to call this API in the next section of the tutorial.

    • The RecommendationEngine helper uses the ShoppingSearchClient helper to get a list of suggested birthday gifts for a selected friend:
      public static class RecommendationEngine
      {
         private static List<string> MenCategoies = new List<string>()
              {
                  "mens-clothes",
                  "mens-bags",
                  "mens-shoes",
                  "mens-grooming"
              };
      
         private static List<string> WomenCategoies = new List<string>()
              {
                  "womens-clothes",
                  "handbags",
                  "womens-shoes",
                  "womens-beauty"
              };
      
         public static async Task<List<Product>> RecommendProductAsync(MyAppUserFriend friend)
         {
            List<Product> recommendedItems = new List<Product>();
            List<string> categoryBasedOnGender = WomenCategoies;
      
            if (friend.Gender == "male")
            {
               categoryBasedOnGender = MenCategoies;
            }
      
            foreach (var item in categoryBasedOnGender)
            {
               var result = await ShoppingSearchClient.GetProductsAsync(item);
               //Randomly pick an item from the retrieved items
               Random r = new Random();
               var product = result.Products[r.Next(result.Products.Count())];
               var des = product.Description;
               //Remove html elements from Product Description
               string noHTML = Regex.Replace(product.Description, @"<[^>]+>|&nbsp;", "").Trim();
               product.Description = Regex.Replace(noHTML, @"\s{2,}", " ");
               recommendedItems.Add(product);
            }
      
      
            return recommendedItems;
         }
      }
      The MenCategoies and WomenCategoies list specifies a list of popular product categories to filter based on selected friend's gender.

      The RecommendProductAsync method takes a MyAppUserFriend parameter. Based on gender, it picks MenCategoies or WomenCategoies. It searches for products in all the categories and randomly picks one each in each category. Then it returns the list.

      The RecommendationEngine class uses a simple selection and search algorithm in order to keep the code easy to follow and understand; you can plug in a more sophisticated selection engine if you prefer.

  6. Add the Search.cshtml, RecommendGifts.cshtml, and About.cshtml files from the Views\Home folder in the downloaded project to your project's Views\Home folder. The Search view displays the list of friends that is returned by the Search action method:
    @using System.Collections.Generic
    @using SampleFacebookBirthdayApp.Models
    @{
      ViewBag.Title = "Search";
    }
    @model IList<MyAppUserFriend>
    
    <article class="intro">
      <h3>Search results</h3>
    </article>
    @if(Model.Count == 0)
    {
        <h3>No friends match your search criteria</h3>
    }
    else
    { 
        @Html.DisplayFor(m => Model, "Friends")
    }
    The RecommendGifts view displays the list of products that is returned by the RecommendGifts action method:
    @model List<SampleFacebookBirthdayApp.Models.Product>
    
    @{
      ViewBag.Title = "Buy Presents";
      string friendName = ViewBag.FriendName;
    }
    
    <article class="intro">
      <label>Buy birthday presents for @friendName</label>
    </article>
      <div class="container-fluid">
        @foreach (var present in Model)
        {
          <div class="row-fluid">
            <div class="span3">
              @if (@present.Image != null)
              {
                <img src="@present.Image.Sizes.Large.Url" />
              }
              else
              {
                <p>Picture not available</p>
              }
            </div>
            <div class="span9">
              <dl>
                <dt>@present.Name</dt>
                <dd>@present.Description</dd>
                <dt>Price</dt>
                <dd class="price">@present.PriceLabel</dd>
              </dl>
              <a class="btn btn-success" target="_blank" href="@present.ClickUrl">
                <i class="icon-shopping-cart"></i>
                Buy it
              </a>
            </div>
          </div>
        }
      </div>

    The CSS classes in this code are defined in the Bootstrap CSS files.
    The About view credits the author of the application:
    @{
      ViewBag.Title = "About";
    }
    
    <article class="intro">
      <h3>About</h3>
    </article>
    <div>
        <b>Contributions: </b><br/>
        <a target="_blank" href="https://www.facebook.com/yaohl">Yao Huang Lin</a> <br/>
        <a target="_blank" href="https://www.facebook.com/kirthik">Kirthi Krishnamraju</a>
    </div>

Create Shop style API Key

The application is now complete except for one thing: a key for the Shopstyle API. In order to search for birthday presents the application uses the Shopstype API, and you need a key provided by Shop style to call that API.

  1. Browser to https://shopsense.shopstyle.com/api/overview and click on sign up here link:


  2. Sign up for an account here:


  3.  Once you are signed in, you can go to https://shopsense.shopstyle.com/account to get your UID/API key.
  4. In Visual studio, open web.config and add a new key element to the appSettings collection:
    <configuration>
      <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
         <!-- Other settings. -->
        
        <add key="Search:AppKey" value="" />
      </appSettings>
      <system.web>

    Run the application and you see that friends are now in birthday order. You can try the Search page by entering a search string and clicking Search, and you can see a list of recommended gifts by clicking a Buy him a present or Buy her a present link.

Deploying the app to Windows Azure

So far your application has been running locally in IIS Express on your development computer. To make it available for other people to use, you have to deploy it to a web hosting provider. In this section of the tutorial you'll deploy it to a Windows Azure Web Site.

Create a Windows Azure Web Site

To create a Windows Azure Web Site, follow the directions in the Create a web site section of the Deploying an ASP.NET Web Application to a Windows Azure Web Site tutorial.

Tell Facebook about your Windows Azure Web Site

Next, you have to tell Facebook to use your Windows Azure URL instead of the localhost URL when it runs your application.

  1.  In your browser, go to the Windows Azure Management Portal, click the Web Sites tab, and then click the web site that you created for your Facebook app.
  2. Click the Dashboard tab, and then copy the URL from Site URL. It's on the right under the Quick Glance section. The URL will look like this: http://yoursitename.azurewebsites.net


  3. Go to https://developers.facebook.com/apps, click your app, and go to the Settings page.
  4. On the Facebook Settings page for your app, paste the Windows Azure Web Site URL in the Canvas URL field. Add a slash (/) to the end of the URL.
  5. Do the same for the Secure Canvas URL field but change http to https.
  6. Click Save Changes at the bottom of the page.

    Note: These instructions explain how to set up the application to use the default azurewebsites.net domain. When you use SSL with a URL in this domain, you are using an SSL certificate that is shared by all URLs in the same domain. If you want to use your own SSL certificate and your own domain, see Configuring a custom domain name for a Windows Azure web site and Configuring an SSL certificate for a Windows Azure web site.

Deploy the Facebook Application project to the Windows Azure Web Site

Next, deploy your Facebook application to the Windows Azure Web Site by following the directions in the Deploy the application to Windows Azure section of the same tutorial.

When you finish publishing the project, Visual Studio automatically opens a browser to the URL of your Windows Azure Web Site, and your application automatically redirects to the Facebook site. Your application is now running in the Facebook page as before, but now it is being served from your Windows Azure Web Site instead of from the local computer.

Next steps

Yao Huang Lin wrote this Facebook Birthday application, and his blog post, The new Facebook application template and library for ASP.NET MVC, contains an excellent overview of the ASP.NET MVC Facebook library.

Kirthi Krishnamraju updated this tutorial based on the new Facebook API. This blog post written by Taylor Mullen contains an excellent overview of the ASP.NET Facebook library.

You can enable your Facebook application to get real-time updates from Facebook by using the UserRealtimeUpdateController controller. For more information, see Realtime Update in the ASP.NET MVC Facebook Template on Troy Dai's blog.

Acknowledgements

  • Yao Huang Lin: Yao is the principal developer for the ASP.NET MVC Facebook library and templates. Yao wrote the sample used in this tutorial.
  • Kirthi Krishnamraju: Kirthi updated the sample. She is a Software Design Engineer in Test at Microsoft.
  • Rick Anderson: (twitter @RickAndMSFT ) Rick co-authored this tutorial and is a senior programming writer for Microsoft focusing on Azure and MVC.
  • Tom Dykstra: Tom co-authored this tutorial and is a senior programming writer on the Microsoft Web Platform & Tools Content Team.
  • Troy Dai: (twitter: @troy_dai ) Troy is a Software Design Engineer in Test at Microsoft.

This article was originally created on September 30, 2014

Author Information

Kirthi Krishnamraju

Kirthi Krishnamraju – Kirthi Krishnamraju is a Software Design Engineer in Test at Microsoft.

Rick Anderson

Rick Anderson – Rick Anderson works as a programmer writer for Microsoft, focusing on ASP.NET MVC, Windows Azure and Entity Framework. You can follow him on twitter via @RickAndMSFT.

Yao Huang Lin

Yao Huang Lin – Yao Huang Lin is a software engineer at Microsoft.

Troy Dai

Troy Dai – Troy Dai is a software engineer at Microsoft.

Tom Dykstra

Tom Dykstra – Tom Dykstra is a Senior Programming Writer on Microsoft's Web Platform & Tools Content Team...