<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ITmeze &#187; facebook</title>
	<atom:link href="http://itmeze.com/tag/facebook/feed/" rel="self" type="application/rss+xml" />
	<link>http://itmeze.com</link>
	<description>IT world served like Cyprus meze</description>
	<lastBuildDate>Thu, 07 Apr 2011 09:55:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>How to implementing log in to site with Facebook account in ASP.NET MVC</title>
		<link>http://itmeze.com/2010/10/16/how-to-implementing-log-in-to-site-with-facebook-account-in-asp-net-mvc/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-implementing-log-in-to-site-with-facebook-account-in-asp-net-mvc</link>
		<comments>http://itmeze.com/2010/10/16/how-to-implementing-log-in-to-site-with-facebook-account-in-asp-net-mvc/#comments</comments>
		<pubDate>Sat, 16 Oct 2010 23:51:08 +0000</pubDate>
		<dc:creator>ITmeze</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[openid]]></category>

		<guid isPermaLink="false">http://itmeze.com/?p=286</guid>
		<description><![CDATA[I have decided to enable users that already have a Facebook account (who hasn&#8217;t ?!) to log on my site with their Facebook credentials. Process has shown to be pretty easy but It took me some time to figure out few things. I have decided to implement so called &#8216;Single Sign-On&#8217; and I pretty much [...]


Related posts:<ol><li><a href='http://itmeze.com/2009/10/19/short-history-of-facebook/' rel='bookmark' title='Permanent Link: Short history of Facebook'>Short history of Facebook</a></li>
<li><a href='http://itmeze.com/2010/10/10/browser-and-self-closed-script-tag/' rel='bookmark' title='Permanent Link: Browser and self closed script tag&#8230;'>Browser and self closed script tag&#8230;</a></li>
<li><a href='http://itmeze.com/2011/03/21/javascript-encode-on-server-side-medium-trust-environment/' rel='bookmark' title='Permanent Link: Javascript Encode on server side &#8211; Medium Trust Environment'>Javascript Encode on server side &#8211; Medium Trust Environment</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-7051816622200841";
google_ad_slot = "0915606426";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<br />
I have decided to enable users that already have a Facebook account (who hasn&#8217;t ?!) to log on my site with their Facebook credentials. Process has shown to be pretty easy but It took me some time to figure out few things.<br />
I have decided to implement so called &#8216;Single Sign-On&#8217; and I pretty much followed guildlines from <a href="http://developers.facebook.com/docs/guides/web">http://developers.facebook.com/docs/guides/web</a>. So, not to loose your precious time, let us start from the beginning.</p>
<h3>1. Register Your application/web site</h3>
<p>First You need to have a Facebook account. Then You visit<br />
<a href="http://developers.facebook.com/setup/">http://developers.facebook.com/setup/</a> to register Your web site. After filling out the form:</p>
<p><a href="http://itmeze.com/wp-content/uploads/2010/10/register-app.jpg"><img class="size-full wp-image-316 alignnone" title="register-app" src="http://itmeze.com/wp-content/uploads/2010/10/register-app.jpg" alt="" width="245" height="175" /></a></p>
<p>and pressing &#8216;Create application&#8217; button You will receive Application ID and Secret numbers. As You can see from filled form Your web site can stay on localhost &#8211; it would work great anyway. You will get Your info in similar form as here:</p>
<p><a href="http://itmeze.com/wp-content/uploads/2010/10/register-app-result.jpg"><img class="size-full wp-image-317 alignnone" title="register-app-result" src="http://itmeze.com/wp-content/uploads/2010/10/register-app-result.jpg" alt="" width="901" height="181" /></a></p>
<h3>2. Place login button</h3>
<p>Now, You are ready to place Facebook login button on Your web site. I have placed it next to standard login button in LoginUserControl.ascx (Partial View):</p>
<pre class="brush: csharp; title: ;">
&lt;%@ Control Language=&quot;C#&quot; Inherits=&quot;System.Web.Mvc.ViewUserControl&quot; %&gt;
&lt;%
    if (Request.IsAuthenticated) {
%&gt;
        Welcome &lt;b&gt;&lt;%: Page.User.Identity.Name %&gt;&lt;/b&gt;!
        [ &lt;%: Html.ActionLink(&quot;Log Off&quot;, &quot;LogOff&quot;, &quot;Account&quot;) %&gt; ]
&lt;%
    }
    else {
%&gt;
        &lt;div class=&quot;login-with-facebook&quot;&gt;
            &lt;fb:login-button &gt;&lt;/fb:login-button&gt;
        &lt;/div&gt;
        [ &lt;%: Html.ActionLink(&quot;Log On&quot;, &quot;LogOn&quot;, &quot;Account&quot;) %&gt; ]
&lt;%
    }

%&gt;
</pre>
<h3>3. Add Facebook namespace</h3>
<p>As You can see Facebook uses it&#8217;s own html namespace. Due to that I have actually lost some time &#8211; as I couldn&#8217;t figure out why my log in button does not show up in IE&#8230;. You need to add fb namespace to Your site (preferably in Site.master):</p>
<pre class="brush: xml; title: ;">
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:fb=&quot;http://www.facebook.com/2008/fbml&quot;&gt;
</pre>
<p>This solves the issue.</p>
<h3>4. Add JavaScript code</h3>
<p>Now some js code is needed, place it at the bottom of the web site:</p>
<pre class="brush: jscript; title: ;">
&lt;div id='fb-root'&gt;&lt;/div&gt;
&lt;script&gt;
window.fbAsyncInit = function () {
            FB.init({ appId: '&lt;%= Settings.FacebookApplicationId %&gt;', status: true, cookie: true,
                xfbml: true
            });
            FB.Event.subscribe('auth.sessionChange', function (response) {
                if (response.session) {
                    window.location = '&lt;%= Url.Action(&quot;LoginWithFacebook&quot;, &quot;account&quot;, new { returnUrl = Request.Url }) %&gt;';
                } else {
                    window.location = '&lt;%= Url.Action(&quot;logout&quot;, &quot;account&quot;, new { returnUrl = Request.Url }) %&gt;';
                }
            });
        };
        (function () {
            var e = document.createElement('script');
            e.type = 'text/javascript';
            e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
            e.async = true;
            document.getElementById('fb-root').appendChild(e);
        } ());
&lt;/script&gt;
</pre>
<p>Make sure You have placed &#8216;fb-root&#8217; element or You manually added script reference to &#8216;connect.facebook.net/en_US/all.js&#8217; script. Replace settings.facebookapplicationid with the one You received after registering app in facebook (Previous step)<br />
Previusly placed button () is initialized thanks to FB.init function. Event &#8216;auth.SessionChange&#8217; is handled in such a manner that when user registers it redirects to /account/loginwithfacebook and when user log out it goes to /account/logout. As simple as that.</p>
<h3>5.  Server side code</h3>
<p>Now we need to handle those actions. I will just describe log in, as log out is self explanatory.</p>
<pre class="brush: csharp; title: ;">
[AcceptVerbs(HttpVerbs.Get)]
        public ActionResult LoginWithFacebook(string returnUrl)
        {
            var cookie = FacebookCookie.GetCookie(Settings.FacebookApplicationId, Settings.FacebookSecretKey);

            if (cookie != null) {

                WebClient client = new WebClient();

                var result = client.DownloadString(&quot;https://graph.facebook.com/me?access_token=&quot; + cookie.AccessToken);

                JavaScriptSerializer js = new JavaScriptSerializer();

                var user = js.Deserialize&lt;FacebookUserHelper&gt;(result);

                var contextUser = UserService.CreateUserFromFacebook(user);

                FormsAuth.SetAuthCookie(contextUser.Name, false);

            }

            if (string.IsNullOrWhiteSpace(returnUrl))
                return RedirectToHomePage();
            else
                return Redirect(returnUrl);
        }
</pre>
<p>After a call to LoginWithFacebook is made code checks if there is a specific Facebook cookie and validates it (&#8216;Settings.FacebookApplicationId&#8217; and &#8216;Settings.FacebookSecretKey&#8217; are the one obtained during registration period).<br />
If everything is okay we call &#8216;https://graph.facebook.com/me&#8217; passing authentication token received within a cookie to  obtain user&#8217;s data, like name or email. Service returns Json data so we use JavaScriptSerializer to deserialize it.  Next we just set authentication cookie. Validation of a cookie may look like:</p>
<pre class="brush: csharp; title: ;">

         public bool ValidateFacebookCookie(string orgCookieValue, string facebookAppSecret) {
            var args = HttpUtility.ParseQueryString(orgCookieValue.Replace(&quot;\&quot;&quot;, string.Empty));
            var cookieCheck = new StringBuilder();
            foreach (var key in args.AllKeys.Where(k =&gt; k != &quot;sig&quot;)) {
                cookieCheck.AppendFormat(&quot;{0}={1}&quot;, key, args[key]);
            }
            cookieCheck.Append(facebookAppSecret);
            var md5Hash = cookieCheck.ToMD5Hash(Encoding.ASCII);
            StringBuilder signature = new StringBuilder();
            for (int i = 0; i &lt; md5Hash.Length; i++) {
                signature.Append(md5Hash[i].ToString(&quot;X2&quot;));
            }
            return string.Equals(args[&quot;sig&quot;], signature.ToString(), StringComparison.InvariantCultureIgnoreCase);
        }
</pre>
<p>You can obtain facebok cookie value with:</p>
<pre class="brush: csharp; title: ;">

       public static string GetFacebookCookieValue(string facebookAppId, string facebookAppSecret) {
            string name = string.Format(&quot;fbs_{0}&quot;, facebookAppId);
            if (!HttpContext.Current.Request.Cookies.AllKeys.Contains(name)) {
                return null;
            }
            return HttpContext.Current.Request.Cookies[name].Value;
        }
</pre>
<p>In case You need a complete code for Facebook cookie validation, You may find it using Googling or write me an email and I will send to anyone. You may check how it works by visiting <a href="http://cyprusqa.com">CyprusQA</a> web site. That is it, happy coding! </p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fitmeze.com%2F2010%2F10%2F16%2Fhow-to-implementing-log-in-to-site-with-facebook-account-in-asp-net-mvc%2F&amp;title=How%20to%20implementing%20log%20in%20to%20site%20with%20Facebook%20account%20in%20ASP.NET%20MVC"><img src="http://itmeze.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>

<p>Related posts:<ol><li><a href='http://itmeze.com/2009/10/19/short-history-of-facebook/' rel='bookmark' title='Permanent Link: Short history of Facebook'>Short history of Facebook</a></li>
<li><a href='http://itmeze.com/2010/10/10/browser-and-self-closed-script-tag/' rel='bookmark' title='Permanent Link: Browser and self closed script tag&#8230;'>Browser and self closed script tag&#8230;</a></li>
<li><a href='http://itmeze.com/2011/03/21/javascript-encode-on-server-side-medium-trust-environment/' rel='bookmark' title='Permanent Link: Javascript Encode on server side &#8211; Medium Trust Environment'>Javascript Encode on server side &#8211; Medium Trust Environment</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://itmeze.com/2010/10/16/how-to-implementing-log-in-to-site-with-facebook-account-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Short history of Facebook</title>
		<link>http://itmeze.com/2009/10/19/short-history-of-facebook/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=short-history-of-facebook</link>
		<comments>http://itmeze.com/2009/10/19/short-history-of-facebook/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 20:31:09 +0000</pubDate>
		<dc:creator>ITmeze</dc:creator>
				<category><![CDATA[internet startup]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[checkfacebook]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://itmeze.com/?p=174</guid>
		<description><![CDATA[If You haven&#8217;t seen or did not follow how world most popular social network changed over lat couple of years just take a break and spend next 4 minutes watching movie below. And wow, just look at those numbers! RESPECT to Mark Zuckerberg! After that I feel like an ant in today&#8217;s world. I also [...]


Related posts:<ol><li><a href='http://itmeze.com/2010/10/16/how-to-implementing-log-in-to-site-with-facebook-account-in-asp-net-mvc/' rel='bookmark' title='Permanent Link: How to implementing log in to site with Facebook account in ASP.NET MVC'>How to implementing log in to site with Facebook account in ASP.NET MVC</a></li>
<li><a href='http://itmeze.com/2009/06/22/making-internet-start-up-part-1-what-to-make/' rel='bookmark' title='Permanent Link: Making internet start-up&#8230; Part 1: What to make?'>Making internet start-up&#8230; Part 1: What to make?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-7051816622200841";
google_ad_slot = "0915606426";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<br />
If You haven&#8217;t seen or did not follow how world most popular social network changed over lat couple of years just take a break and spend next 4 minutes watching movie below.</p>
<p>And wow, just look at those numbers! RESPECT to Mark Zuckerberg!</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://www.youtube.com/v/6ju4LyQaZCE&amp;color1=0xb1b1b1&amp;color2=0xcfcfcf&amp;hl=pl&amp;feature=player_embedded&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/6ju4LyQaZCE&amp;color1=0xb1b1b1&amp;color2=0xcfcfcf&amp;hl=pl&amp;feature=player_embedded&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>After that I feel like an ant in today&#8217;s world.</p>
<p>I also recommend visiting web site: <a title="facebook stats" href="http://www.checkfacebook.com/">CheckFacebook</a>. Where you can get some up to date stats about popularity and growth of Facebook user&#8217;s all over the world. Here is a simple screen shoot:</p>
<p><img class="alignnone size-full wp-image-190" title="checkfacebook user's in cyprus" src="http://itmeze.com/wp-content/uploads/2009/10/checkfacebook.jpg" alt="checkfacebook user's in cyprus" width="500" height="351" /></p>
<p>It looks like it is pretty popular in Cyprus as well  &#8211; third of population&#8230;.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fitmeze.com%2F2009%2F10%2F19%2Fshort-history-of-facebook%2F&amp;title=Short%20history%20of%20Facebook"><img src="http://itmeze.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>

<p>Related posts:<ol><li><a href='http://itmeze.com/2010/10/16/how-to-implementing-log-in-to-site-with-facebook-account-in-asp-net-mvc/' rel='bookmark' title='Permanent Link: How to implementing log in to site with Facebook account in ASP.NET MVC'>How to implementing log in to site with Facebook account in ASP.NET MVC</a></li>
<li><a href='http://itmeze.com/2009/06/22/making-internet-start-up-part-1-what-to-make/' rel='bookmark' title='Permanent Link: Making internet start-up&#8230; Part 1: What to make?'>Making internet start-up&#8230; Part 1: What to make?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://itmeze.com/2009/10/19/short-history-of-facebook/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

