This article will then how to create a web-based application based on Facebook in Azure
Based on a Facebook marketing application, the application is mapped to the canvas page. Base URL on Azure (for example: http://azurefacebooksample.cloudapp.net/) This is a form that shows the initial contact information. Here's how to show your contact information:
Listing 1
[Canvasauthorize (Perms = "User_birthday")]
Public ActionResult Index ()
{
Facebookapp app = new Facebookapp ();
Dynamic response = App. Api ("Me");
Contact contacts = new Contact ();
Contact. FirstName = Response.first_name;
Contact. LastName = Response.last_name;
string query = "Select Birthday_date from user where uid = Me ()";
Response = App. FQL (query);
if (response. Count > 0)
Contact. dateOfBirth = response[0].birthday_date;
return View (Contact);
}
The program has been relatively easy to come by, thanks to the Facebook C # SDK. The SDK handles Facebook OAuth authentication and application authorization through the "Canvasauthorize" feature, and provides a "Facebookapp" object. You should remember that before a new user ran "Azuresample", they had to give the application special permission to access the user's birth date. All we need to do to achieve this is to tag any controller action that requires authorization by using the "canvasauthorize" feature, specifying that the extended permission required is optional (where we specify "User_birthday" permissions).
The "Facebookapp" object has access to signed request information, as well as to Facebook's graph APIs and rest APIs. In this controller, we use "Facebookapp" to invoke a graph API ("Me") to return information about the current user (including the first and last names). We can't use this graph API to get the date of birth for this user, but we can turn to Facebook FQL and create a query to find the current user's date of birth. We can use FQL (formal Query Language) through "Facebookapp". We add this user's information to a new "contact" object, which is a data model object from the domain object. Then we put this "contact" into a view.
This is the complete code for this indexed view:
Listing 2
<%@ Page title= "" Language= "C #" masterpagefile= "~/views/shared/site.master"
inherits= "System.web.mvc.viewpage<azurefacebooksample.domain.models.contact>"%>
<asp:content id= "Content1" contentplaceholderid= "titlecontent" runat= "Server" >
Home
</asp:Content>
<asp:content id= "Content2" contentplaceholderid= "maincontent" runat= "Server" >
<div>please provide the following information. All fields are required.</div>
<% using (Html.BeginForm ())
{%>
<%: Html.validationsummary (True)%>
<div class= "Editor-label" >
<%: html.labelfor (model => model. FirstName)%>
</div>
<div class= "Editor-field" >
<%: html.textboxfor (model => model. FirstName)%>
<%: html.validationmessagefor (model => model. FirstName)%>
</div>
<div class= "Editor-label" >
<%: html.labelfor (model => model. LastName)%>
</div>
<div class= "Editor-field" >
<%: html.textboxfor (model => model. LastName)%>
<%: html.validationmessagefor (model => model. LastName)%>
</div>
<div class= "Editor-label" >
<%: html.labelfor (model => model. Email)%>
</div>
<div class= "Editor-field" >
<%: html.textboxfor (model => model. Email)%>
<%: html.validationmessagefor (model => model. Email)%>
</div>
<div class= "Editor-label" >
<%: html.labelfor (model => model. dateOfBirth)%>
</div>
<div class= "Editor-field" >
<%: html.editorfor (model => model. dateOfBirth)%>
<%: html.validationmessagefor (model => model. dateOfBirth)%>
</div>
<div>you moment-in is or older to participate.</div>
<div class= "Editor-label" >
<%: html.labelfor (model => model. ZIP)%>
</div>
<div class= "Editor-field" >
<%: html.textboxfor (model => model. ZIP)%>
<%: html.validationmessagefor (model => model. ZIP)%>
</div>
<p>
<input type= "Submit" value= "Next"/>
</p>
<%}%>
</aspContent>
This view specifies the form fields that are used to collect contact information. Validation is done through specific annotations on the contact data model. When the user clicks the "Next" button, these form fields are sent to the controller's post operation:
Listing 3
[HTTP]
[Canvasauthorize]
Public ActionResult Index (contacts Contact)
{
Facebookapp app = new Facebookapp ();
Contact. Partitionkey = App. Userid.tostring ();
if (modelstate.isvalid)
{
Contactrepository.save (contact);
return this. Canvasredirecttoaction ("Selectstore");
}
return View (Contact);
}
In addition to the contact information returned from the form, we use the "Facebookapp" object to crawl the user's Facebook ID and put it in the "contact" so that we can track future access to the application (the customer can only register once and receive a prize).