Asp.Net MVC3 Razor with facebook Authentication

Today we have to see Asp.Net MVC3 Razor with facebook Authentication before we see details first u have to Register u r Application in facebook developer(https://developers.facebook.com/apps).and u got AppID.

Ok now lets see step by step

Step 1 :Open VS 2010 go to project and select Asp.Net MVC 3 Web Application

Step 2 :select empty and select Razor

Step 3 :Now go to Controllers and right click and give Some name like Home Controllers and add this code

          public ActionResult Index()
        {
            if (!string.IsNullOrEmpty(Request.Params["signed_request"]))
            {
                ViewBag.IsAuthenticated =true;
                Dictionary<string,string> info = Utility.DecodePayload(Request.Params["signed_request"]);
                Session["access_token"] = info;
                Facebook.FacebookAPI api = new Facebook.FacebookAPI(info["oauth_token"]);
                var json = api.Get("/me");
            }
           
else
                ViewBag.IsAuthenticated = false;
            return View();
         }


Step 4 :Now go to Model and

create utility class(Here u have to Add Facebook API throw reference in our App)

public class Utility
{
    public static Dictionary<string, string> DecodePayload(string payload)
    {
        var parameters = new Dictionary<string, string>();

       
try
        {
            string[] sB64String = payload.Split('.');
            payload = payload.Replace((sB64String[0] + "."), string.Empty);
           
//End
            var encoding = new UTF8Encoding();
            var decodedJson = payload.Replace("=", string.Empty).Replace('-', '+').Replace('_', '/');
            var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 -
decodedJson.Length % 4) % 4, '='))

            var json = encoding.GetString(base64JsonArray);

            var jObject = JSONObject.CreateFromString(json);
           
//parameters.Add("liked", jObject.Dictionary["page"].Dictionary["liked"].Boolean.ToString());
            {
                parameters.Add("isAuthorized", "true");
                parameters.Add("oauth_token", jObject.Dictionary["oauth_token"].String);
                parameters.Add("profile_id", jObject.Dictionary["user_id"].String);
            }
           
else
            {
                parameters.Add("isAuthorized", "false");
                parameters.Add("oauth_token", string.Empty);
            }

        }
        catch { }

        return parameters;
    }
}


Step 5 :now go to Home Controllers and right click on Index and add View after adding u have to write this code

index.chtml code is

<html xmlns:fb="http://www.facebook.com/2008/fbml">
<
body>
    <script src="http://connect.facebook.net/en_US/all.js"></script
>

    <div id="fb-root">

    </div>
   <script
>

// assume we are already logged in
FB.init({ appId: '161729093918865', xfbml: true, cookie: true, oauth: true });

if ([email protected]().ToLower())

{
window.top.location =
"http://www.facebook.com/connect/uiserver.php"

+ "?app_id=" +
'161729093918865'

+ "&next=" +
'http://apps.facebook.com/spatialidea/'

+
"&display=page"

+ "&cancel_url=" +
'http://www.facebook.com/'

+
"&locale=en_IN"

+
"&perms=publish_stream,email&method=permissions.request"

+ "&return_session=1&session_version=3&fbconnect=0&canvas=1&legacy_return=1";

}

    </script
>

</
body>
</
html>

Step 6 : Now run the App

Next Recommended Reading Autocomplete TextBox in MVC3 Razor