Question

I am trying to integrate my asp.net 4.0(non MVC) website with facebook.

I want to retrive the authenticated user info.

The samples listed in facebooksdk is based on MVC. i tried to implement it in my website, but getting errors. i am struck.

If anyone implemented the facebook connect logic and retrieved the user information using facebook-c#-sdk, please help me how to do this.

I tried the same using facebooktoolkit, and i am able to retrive the user info. but the toolkit is having issues with .net 4.0.

Greatly appreciated your help.

Was it helpful?

Solution

Here is some example code on how to protect a page called ProtectedPage.aspx and have a login page called LogOn.aspx:

Login.aspx:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Log In
    </h2>
    <p>
        <fb:login-button></fb:login-button>
    </p>
    <div id="fb-root">
    </div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
        FB.init({ appId: 'your app id', status: true, cookie: true, xfbml: true });
        FB.Event.subscribe('auth.sessionChange', function (response) {
            if (response.session) {
                // A user has logged in, and a new cookie has been saved
                window.location.reload();
            } else {
                // The user has logged out, and the cookie has been cleared
            }
        });
    </script>
</asp:Content>

Login.aspx.cs

public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FacebookApp app = new FacebookApp();
            Authorizer authorizer = new Authorizer(app);
            if (authorizer.IsAuthorized())
            {
                Response.Redirect(HttpUtility.UrlDecode(Request.QueryString["returnUrl"] ?? "/"));
            }
        }
    }

ProtectedPage.aspx.cs

public partial class ProtectedPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FacebookApp app = new FacebookApp();
        Authorizer authorizer = new Authorizer(app);
        if (!authorizer.IsAuthorized())
        {
            Response.Redirect("~/Account/Login.aspx?returnUrl=" + HttpUtility.UrlEncode(Request.Url.PathAndQuery));
        }
    }
}

There are more samples and istructions on our Codeplex wiki.

OTHER TIPS

Just Follow these simple steps:

  • Use the official sdk: github.com/facebook/csharp-sdk .
  • Implement the authentication process of your own.
  • Read facebookk api documentation on web application authentication for better understanding the authentication process: developers.facebook.com/docs/authentication/#authenticating-users-in-a-web-application
  • I have made an custom facebook classs for encapsulate this authentication process as much as possible. I have shared it on my blog on facebook api authentication using c#. You can use it as reference also.
  • After getting the access token, all are straight forward. Just set the access token and call 'get' method for provate data.
  • If you are still having problem,please let us know details.

Here is a good example of Facebook Auth 2.0 using facebook sdk from codeplex

Facebook Auth 2.0 using facebook sdk

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top