通过创建派生自 DotNetOpenAuth.OAuth2.WebServerClient 的 FacebookAuthClient,我已经能够让 DNOA 版本 4.1.0.12182、.Net 3.5 和 Facebook 相互协作。我发现的一个小问题是,如果您使用基于 cookie 的会话,那么您必须在使用 OAuth 功能之前访问该会话。据我所知,这是因为 DNOA 使用会话 ID 作为状态参数,如果会话从未被访问过,它可以在请求之间更改。当响应从 Facebook 返回时,这将导致状态参数不匹配错误。
FacebookAuthClient:
public class FacebookAuthClient : DotNetOpenAuth.OAuth2.WebServerClient
{
private static readonly DotNetOpenAuth.OAuth2.AuthorizationServerDescription Description = new DotNetOpenAuth.OAuth2.AuthorizationServerDescription
{
TokenEndpoint = new Uri("https://graph.facebook.com/oauth/access_token"),
AuthorzationEndpoint = new Uri("https://graph.facebook.com/oauth/authorize")
};
public static readonly string [] ScopeNeeded = { "publish_stream" };
public FacebookAuthClient()
: base(Description)
{
}
}
Facebook.aspx.cs:
public partial class FacebookPage : System.Web.UI.Page
{
private FacebookAuthClient _client = new FacebookAuthClient
{
ClientIdentifier = ConfigurationManager.AppSettings["FBClientId"], //The FB app's Id
ClientCredentialApplicator = DotNetOpenAuth.OAuth2.ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["FBClientSecret"]) // The FB app's secret
}
protected void Page_Load(object sender, EventArgs e)
{
DotNetOpenAuth.OAuth2.IAuthorizationState auth = _client.ProcessUserAuthorization();
if (_auth == null)
{
// Kick off authorization request with the required scope info
client.RequestUserAuthorization(FacebookAuthClient.ScopeNeeded);
}
}
}
这只是一个测试应用程序,因此没有错误处理,但它似乎可以工作。
编辑
我对所有这些都使用了 DotNetOpenAuth(unified) NuGet 包。
编辑
在创建 ClientCredentialApplicator 时添加了缺少的 .PostParameter 调用。