Skip to content
WJunction - Webmaster Forum

HELP: Facebook Permission Page Request (C#) ASP.NET MVC

Status
Not open for further replies.

b1gj4v

Established Member
182
2010
25
i have created a page on facebook and using MVC to create the content etc

when the user clicks like it will notify the user that you have liked the page, what i want to do is now to display their information on the page. EG= First Name, Last Name, Birthday, Photos etc... Im new to MVC and all this facebook intergration, any help will be much appreciated.

If in the wrong section please move to appropriate area, thanks in advance

Here is what i have done so far;

Index.cshtml

Code:
@model MVCFacebookTestApp.Models.Home

@{
    ViewBag.Title = "Index";
}

<h1>Index</h1>

@Model.TestString
<br /><br />
@Model.TestString2

@if (Model.IsLiked)
{
 <div id="fb-root"></div>
 <script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
 <script type="text/javascript">
     FB.init({
         appId: '123233914444256',
         status: true, 
         cookie: true, 
         xfbml: false, 
         oauth: true 
     });

        FB.ui({
            method: 'permissions.request',
            'perms': 'user_birthday,user_relationship_details,read_stream',
            'display': 'iframe'
        },

//             FB.ui({
//          method: "stream.publish",
//          display: "iframe",
//          user_message_prompt: "Publish This!",
//          message: "I love Redstar Creative!",
//          attachment: {
//             name: "Darren has a gift!",
//             caption: "Darren has tested his skills and did extremely well",
//             description: "Here is a list of Darren's skills:",
//             href: " ",
//             media:[{"type":"image","src":" ","href":" "}],
//             properties:{
//               "1)":{"text":"Reading","href":" "},
//               "2)":{"text":"Math","href":" "},
//               "3)":{"text":"Farmville","href":" "}
//             }
//          },
//          action_links: [{ text: 'Test yourself', href: '' }]
//         },
//         function(response) {
//           if (response && response.post_id) {
//             //alert('Post was published.');
//           } else {
//             //alert('Post was not published.');
//           }
//         }
//         );

//     FB.ui(
//       {
//         'method': 'fbml.dialog',
//         'display': 'dialog',
//         'fbml': "<h1>this is some fbml markup</h1>",
//         'width': 575
//       },
//       function() {
//         alert(true, 'callback was invoked');
//       }
//     );

   function (response) {
      // check response.perms that it has all the required permissions...  if so, continue to next page.
      // TODO should be a better way to do this since UI checks perms

//            if (response.perms != null && isSetProperSubset(permissions.split(","), response.perms.split(","))) {
//                top.location.href = "http://apps.facebook.com/fbdevwiki/" + nextUrl;
//            }

      alert("hit");
  }
);

 </script>
}
HomeController.cs

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCFacebookTestApp.Models;
using Facebook;
using Facebook.Web;

namespace MVCFacebookTestApp.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {            
            string request = Request.Form["signed_request"];

            FacebookApplication app = new FacebookApplication();

            FacebookSignedRequest result = FacebookSignedRequest.Parse(app.InnerCurrent, request);

            dynamic data = result.Data;

            bool liked = data.page.liked;

            if (!liked)
            {
                Home h = Home.NotLiked();
                return View(h);
            }
            else
            {
                Home h = Home.Liked();
                
                var fbRequest = FacebookWebContext.Current;
                if (fbRequest.IsAuthorized())
                {
                    var fb = new FacebookWebClient(fbRequest);
                    // dynamic result = fb.Get("/me");

                }
                else
                {
                    //FacebookWebAuthorizer a = new FacebookWebAuthorizer(fbRequest);
                    //a.ReturnUrlPath = "http://localhost:49269/";
                    //a.Authorize();
                }

                h.TestString2 += " Ha! We captured this data about you!";

                h.TestString2 += " Name: ...";
                
                h.TestString2 += " Age: ...";

                h.TestString2 += " Location: ...";

                return View(h);
            }

        }     
    }
}
Home.cs

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCFacebookTestApp.Models
{
    public class Home
    {
        public string TestString { get; set; }
        public string TestString2 { get; set; }
        public bool IsLiked { get; set; }

        private Home() { }

        public static Home NotLiked()
        {
            Home h = new Home();
            h.TestString = "Hello World";
            h.TestString2 = "Welcome to RedStar Creative";
            h.IsLiked = false;
            return h;
        }

        public static Home Liked()
        {
            Home h = new Home();
            h.TestString = "Hello World";
            h.TestString2 = "You liked RedStar";
            h.IsLiked = true;
            return h;
        }

    }
}]
AccountController.cs

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Facebook;
using System.Web.Security;
using MVCFacebookTestApp.Models;

namespace MVCFacebookTestApp.Controllers
{
    public class AccountController : Controller
    {
        private const string redirectUrl = "http://localhost:49269/Account/OAuth";


        //
        // GET: /Account/OAuth/

        public ActionResult OAuth(string code, string state)
        {
            FacebookOAuthResult oauthResult;
            if (FacebookOAuthResult.TryParse(Request.Url, out oauthResult))
            {
                if (oauthResult.IsSuccess)
                {
                    var oAuthClient = new FacebookOAuthClient(FacebookApplication.Current);
                    oAuthClient.RedirectUri = new Uri(redirectUrl);
                    dynamic tokenResult = oAuthClient.ExchangeCodeForAccessToken(code);
                    string accessToken = tokenResult.access_token;

                    //DateTime expiresOn = DateTime.MaxValue;

                    //if (tokenResult.ContainsKey("expires"))
                    //{
                    //    DateTimeConvertor.FromUnixTime(tokenResult.expires);
                    //}

                    //FacebookClient fbClient = new FacebookClient(accessToken);
                    //dynamic me = fbClient.Get("me?fields=id,name");
                    //long facebookId = Convert.ToInt64(me.id);

                    //InMemoryUserStore.Add(new FacebookUser
                    //{
                    //    AccessToken = accessToken,
                    //    Expires = expiresOn,
                    //    FacebookId = facebookId,
                    //    Name = (string)me.name,
                    //});

                    //FormsAuthentication.SetAuthCookie(facebookId.ToString(), false);

                    //// prevent open redirection attack by checking if the url is local.
                    //if (Url.IsLocalUrl(state))
                    //{
                    //    return Redirect(state);
                    //}
                    //else
                    //{
                    //    return RedirectToAction("Index", "Home");
                    //}
                }
            }

            return RedirectToAction("Index", "Home");
        }

        //
        // GET: /Account/LogOff/

        //public ActionResult LogOff()
        //{
        //    FormsAuthentication.SignOut();
        //    var oAuthClient = new FacebookOAuthClient();
        //    oAuthClient.RedirectUri = new Uri(logoffUrl);
        //    var logoutUrl = oAuthClient.GetLogoutUrl();
        //    return Redirect(logoutUrl.AbsoluteUri);
        //}
    }
}
 
Last edited:
Status
Not open for further replies.

About the author

B
Established Member · Joined
Web/UI Developer C#/ASP.NET/jQuery
182
Messages
25
Reactions
28
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom