public static string Content(this UrlHelper helper, string contentPath, bool absolute) { var url = string.Empty; if (!absolute) { url = helper.Content(contentPath); } else { url = helper.ToPublicUrl(contentPath); } return(url); }
public ActionResult FbAuth(string returnUrl) { var client = new FacebookClient(); try { 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"); //Get the Public Uri due to apphabor getting all "cloudy" with ports var urlHelper = new UrlHelper(Request.RequestContext); var publicUrl = urlHelper.ToPublicUrl(redirectUri.Uri); // Exchange the code for an access token dynamic result = client.Get("/oauth/access_token", new { client_id = ConfigurationManager.AppSettings["FacebookAppId"], redirect_uri = publicUrl, 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 }); //Instantiate FbModel var model = new FbModel(); // Read the Facebook user values model.FacebookId = Convert.ToInt64(me.id); model.FirstName = me.first_name; model.LastName = me.last_name; model.Email = me.email; // Add the user to our persistent store var user = AccountService.AddOrUpdateFacebookUser(model); //Check if the account requires the password to be set if (string.IsNullOrEmpty(user.Email)) { return RedirectToAction("RegisterFacebook", "Account", new { @code = user.AccountHash }); } else { AuthenticateUser(user.Id, user.FirstName, user.LastName, user.Email, user.FacebookId, user.AccessToken); return RedirectToAction("Index", "Home"); } } catch (Exception ex) { Elmah.ErrorSignal.FromCurrentContext().Raise(ex); } return RedirectToAction("Content", "Error"); }
public ActionResult SignUpFacebook() { // Build the Return URI form the Request Url var redirectUri = new UriBuilder(Request.Url); redirectUri.Path = Url.Action("FbAuth", "Account"); //Get the Public Uri due to apphabor getting all "cloudy" with ports var urlHelper = new UrlHelper(Request.RequestContext); var publicUrl = urlHelper.ToPublicUrl(redirectUri.Uri); var client = new FacebookClient(); #region Facebook OAuth URL example // 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 #endregion //Create the Facebook Oauth URL var uri = client.GetLoginUrl(new { client_id = ConfigurationManager.AppSettings["FacebookAppId"], redirect_uri = publicUrl, scope = "email", }); return Redirect(uri.ToString()); }