Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>As for the Login button</strong>, if you are logged in to Facebook prior to visiting your app you will see the faces instead of the login button, the only way to get the Login button back is to go to facebook.com and do a logout or possibly do a facebook logout using the C# SDK. Depending on your requirements this may or may not be what you wanted. There is a bit about <a href="http://developers.facebook.com/docs/authentication/reauthentication/" rel="nofollow">Re-Authentication</a> in the SDK documentation if that is what you really want.</p> <p>I've tweaked your app by removing the submit() and replaced it with an ajax post, The FacebookLogin action was changed and I added some error handling on the About action. Your original app will work but it will automatically redirect to About if you are logged in to Facebook. </p> <p><strong>Update</strong> Added a login link which does not use Javascript, insert appid and appsecret and adjust portnumber accordingly. This was adapted from the server side login sample found <a href="https://github.com/facebook-csharp-sdk/facebook-aspnet-sample" rel="nofollow">here</a> which is far prettier than this code :)</p> <p><strong>Note</strong> The state value being passed in the server side flow should be a unqiue value that you should validate in the ConnectResponse() method, i.e. generate a value in FacebookLoginNoJs and make sure it's the same in ConnectResponse to prevent cross site request <a href="http://developers.facebook.com/docs/reference/dialogs/oauth/" rel="nofollow">forgery</a></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() { ViewBag.Message = "Please log in first"; if (Session["AccessToken"] != null) { var accessToken = Session["AccessToken"].ToString(); var client = new FacebookClient(accessToken); try { dynamic result = client.Get("me", new { fields = "name,id" }); string name = result.name; string id = result.id; ViewBag.Message = "Hello id: " + id + " aka " + name; } catch (FacebookOAuthException x) { } } return View(); } public void FacebookLogin(string uid, string accessToken) { var context = this.HttpContext; context.Session["AccessToken"] = accessToken; } public ActionResult FacebookLoginNoJs() { return Redirect("https://www.facebook.com/dialog/oauth?client_id=MY-APPID-REMOVED&amp;redirect_uri=http://localhost:45400/Home/ConnectResponse&amp;state=secret"); } public ActionResult ConnectResponse(string state, string code, string error, string error_reason, string error_description, string access_token, string expires) { if (string.IsNullOrEmpty(error)) { try { var client = new FacebookClient(); dynamic result = client.Post("oauth/access_token", new { client_id = "MY-APPID-REMOVED", client_secret = "MY-APP-SECRET-REMOVED", redirect_uri = "http://localhost:45400/Home/ConnectResponse", code = code }); Session["AccessToken"] = result.access_token; if (result.ContainsKey("expires")) Session["ExpiresIn"] = DateTime.Now.AddSeconds(result.expires); } catch { // handle errors } } else { // Declined, check error } return RedirectToAction("Index"); } } } </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: 'MY-APPID-REMOVED', // 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') { var uid = response.authResponse.userID; var accessToken = response.authResponse.accessToken; var url = '/Home/FacebookLogin'; $.post(url, { uid: uid, accessToken: accessToken }, function (data) { }); } 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; @Html.ActionLink("The NoJs Login", "FacebookLoginNoJs", "Home") </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>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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