我的目標是使用FB登錄按鈕,以便FB用戶可以登錄到我的ASP.NET MVC 3網站。看來Facebook的C#SDK最近已經發生了變化,所有的old examples都不能用於新版本。我嘗試了一天,讓他們工作...我正在關閉本教程Getting Started with the Facebook C# SDK for ASP.NET(無法)開始使用Facebook C#SDK
當前當我瀏覽http://localhost:8033/
它似乎自動登錄(即使重新啓動Chrome後)因爲它顯示「我的名字使用我的應用程序名稱」並顯示我的照片。我預計它會顯示一個FB登錄按鈕。當我去http://localhost:8033/Home/About
時,出現Session["AccessToken"]
爲空的錯誤(這很有意義,因爲它顯然沒有設置)。
這是我有:
HomeController.cs
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");
}
}
}
Index.cshtml
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
//appId: 'YOUR_APP_ID', // App ID
appId: '<MY-NUMBER-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') {
// 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));
</script>
<div class="fb-login-button" data-show-faces="true" data-width="400" data-max-rows="1"></div>
About.cshtml
@{
ViewBag.Title = "About Us";
}
<h2>About</h2>
<p>
@ViewBag.Message
</p>
您能否告訴我如何解決這個問題,以便顯示一個FB登錄按鈕,並且單擊它時會要求用戶執行FB身份驗證,然後將它們發回,然後我的應用程序將它們識別爲登錄用戶?
刪除步驟1-3,因爲這不再是真實的,一定是固定的:)。添加了一個觸發服務器端登錄流的鏈接。 – 2012-07-16 08:28:30