Skip to content

Instantly share code, notes, and snippets.

@ntotten
Created August 6, 2012 03:00
Show Gist options
  • Select an option

  • Save ntotten/3269385 to your computer and use it in GitHub Desktop.

Select an option

Save ntotten/3269385 to your computer and use it in GitHub Desktop.
Facebook Apps and Windows Azure Web Sites (Part 1 – Getting Started)
public class AccountController : Controller
{
//
// GET: /Account/Login
public ActionResult Login()
{
// Build the Return URI form the Request Url
var redirectUri = new UriBuilder(Request.Url);
redirectUri.Path = Url.Action("FbAuth", "Account");
var client = new FacebookClient();
// Generate the Facebook OAuth URL
// Example: https://www.facebook.com/dialog/oauth?
// client_id=YOUR_APP_ID
// &redirect_uri=YOUR_REDIRECT_URI
// &scope=COMMA_SEPARATED_LIST_OF_PERMISSION_NAMES
// &state=SOME_ARBITRARY_BUT_UNIQUE_STRING
var uri = client.GetLoginUrl(new
{
client_id = ConfigurationManager.AppSettings["FacebookAppId"],
redirect_uri = redirectUri.Uri.AbsoluteUri,
scope = "email",
});
return Redirect(uri.ToString());
}
}
[Authorize]
public class AppController : Controller
{
//
// GET: /App/
public ActionResult Index()
{
return View();
}
}
public ActionResult FbAuth(string returnUrl)
{
var client = new FacebookClient();
var oauthResult = client.ParseOAuthCallbackUrl(Request.Url);
// Build the Return URI form the Request Url
var redirectUri = new UriBuilder(Request.Url);
redirectUri.Path = Url.Action("FbAuth", "Account");
// Exchange the code for an access token
dynamic result = client.Get("/oauth/access_token", new
{
client_id = ConfigurationManager.AppSettings["FacebookAppId"],
redirect_uri = redirectUri.Uri.AbsoluteUri,
client_secret = ConfigurationManager.AppSettings["FacebookAppSecret"],
code = oauthResult.Code,
});
// Read the auth values
string accessToken = result.access_token;
DateTime expires = DateTime.UtcNow.AddSeconds(Convert.ToDouble(result.expires));
// Get the user's profile information
dynamic me = client.Get("/me",
new {
fields = "first_name,last_name,email",
access_token = accessToken
});
// Read the Facebook user values
long facebookId = Convert.ToInt64(me.id);
string firstName = me.first_name;
string lastName = me.last_name;
string email = me.email;
// Add the user to our persistent store
var userService = new UserService();
userService.AddOrUpdateUser(new User
{
Id = facebookId,
FirstName = firstName,
LastName = lastName,
Email = email,
AccessToken = accessToken,
Expires = expires
});
// Set the Auth Cookie
FormsAuthentication.SetAuthCookie(email, false);
// Redirect to the return url if availible
if (String.IsNullOrEmpty(returnUrl))
{
return Redirect("/App");
}
else
{
return Redirect(returnUrl);
}
}
Install-Package Facebook
<appSettings>
...
<add key="FacebookAppId" value="your_local_app_id"/>
<add key="FacebookAppSecret" value="your_local_app_secret" />
</appSettings>
<appSettings>
<add key="FacebookAppId" value="your_prod_app_id"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
<add key="FacebookAppSecret" value="your_prod_app_secret"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment