Note that there are some explanatory texts on larger screens.

plurals
  1. PO(Unable to) Get started with the Facebook C# SDK
    primarykey
    data
    text
    <p>My goal is to use the FB login button so that FB users can log into my ASP.NET MVC 3 website. It seems that things have changed recently with the Facebook C# SDK and all the <a href="http://facebooksdk.codeplex.com/documentation" rel="nofollow">old examples</a> will not work with the new version. I've tried for a day to get them to work... I'm working off of the tutorial <a href="http://csharpsdk.org/docs/web/getting-started" rel="nofollow">Getting Started with the Facebook C# SDK for ASP.NET</a></p> <p>Currently when I browse to <code>http://localhost:8033/</code> it seems to automatically log me in (even after a fresh restart of Chrome) because it shows "<em>my-name</em> uses <em>my-app-name</em>" and shows my picture. I expected it to instead show a FB login button. And when I go to <code>http://localhost:8033/Home/About</code> I get an error that <code>Session["AccessToken"]</code> is null (which makes sense because it's clearly not getting set).</p> <p>Here's what I have:</p> <p><strong>HomeController.cs</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Facebook; namespace FacebookTest.Controllers { public class HomeController : Controller { public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!"; return View(); } public ActionResult About() { var accessToken = Session["AccessToken"].ToString(); var client = new FacebookClient(accessToken); dynamic result = client.Get("me", new { fields = "name,id" }); string name = result.name; string id = result.id; ViewBag.Message = "Hello id: " + id; return View(); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult FacebookLogin(HttpContext context) { var accessToken = context.Request["accessToken"]; context.Session["AccessToken"] = accessToken; return RedirectToAction("About"); } } } </code></pre> <p><strong>Index.cshtml</strong></p> <pre><code>@{ ViewBag.Title = "Home Page"; } &lt;h2&gt;@ViewBag.Message&lt;/h2&gt; &lt;p&gt; To learn more about ASP.NET MVC visit &lt;a href="http://asp.net/mvc" title="ASP.NET MVC Website"&gt;http://asp.net/mvc&lt;/a&gt;. &lt;/p&gt; &lt;div id="fb-root"&gt;&lt;/div&gt; &lt;script&gt; window.fbAsyncInit = function () { FB.init({ //appId: 'YOUR_APP_ID', // App ID appId: '&lt;MY-NUMBER-REMOVED&gt;', // App ID status: true, // check login status cookie: true, // enable cookies to allow the server to access the session xfbml: true // parse XFBML }); // Additional initialization code here FB.Event.subscribe('auth.authResponseChange', function (response) { if (response.status === 'connected') { // the user is logged in and has authenticated your // app, and response.authResponse supplies // the user's ID, a valid access token, a signed // request, and the time the access token // and signed request each expire var uid = response.authResponse.userID; var accessToken = response.authResponse.accessToken; // TODO: Handle the access token // Do a post to the server to finish the logon // This is a form post since we don't want to use AJAX var form = document.createElement("form"); form.setAttribute("method", 'post'); //form.setAttribute("action", '/FacebookLogin.ashx'); form.setAttribute("action", '/Home/FacebookLogin'); var field = document.createElement("input"); field.setAttribute("type", "hidden"); field.setAttribute("name", 'accessToken'); field.setAttribute("value", accessToken); form.appendChild(field); document.body.appendChild(form); form.submit(); } else if (response.status === 'not_authorized') { // the user is logged in to Facebook, // but has not authenticated your app } else { // the user isn't logged in to Facebook. } }); }; // Load the SDK Asynchronously (function (d) { var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) { return; } js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js"; ref.parentNode.insertBefore(js, ref); } (document)); &lt;/script&gt; &lt;div class="fb-login-button" data-show-faces="true" data-width="400" data-max-rows="1"&gt;&lt;/div&gt; </code></pre> <p><strong>About.cshtml</strong></p> <pre><code>@{ ViewBag.Title = "About Us"; } &lt;h2&gt;About&lt;/h2&gt; &lt;p&gt; @ViewBag.Message &lt;/p&gt; </code></pre> <p>Can you tell me how to fix this so that a FB login button is displayed, and when clicked it asks the users to do a FB authentication, sends them back, and then my app recognizes them as a logged in user?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload