Facebook身份验证重定向循环

时间:2012-02-16 10:02:24

标签: c# facebook facebook-c#-sdk

我正在尝试使用Facebook C# SDK添加将您刚刚购买的产品分享到我的网站的功能。为此,您需要使用Facebook对用户进行身份验证并获得所需的权限。

我有一些代码可以检查您是否经过身份验证,如果没有,请将您重定向到Facebook oAuth页面进行身份验证。

但是,当页面从Facebook返回时,它会进入重定向循环:

var auth = new FacebookWebAuthorizer();

bool authorized = auth.FacebookWebRequest.IsAuthorized(new string[] { "publish_stream" });

if (authorized)
{

    var client = new FacebookClient(auth.FacebookWebRequest.AccessToken);

    Dictionary<string, object> args = new Dictionary<string, object>();

    args["message"] = "This is a test.";
    args["name"] = "";
    args["link"] = "http://www.example.com/";
    args["description"] = "Test link.";

    client.Post("/me/feed", args);

}
else
{

    var facebookClient = new FacebookOAuthClient();

    var parameters = new Dictionary<string, object> {
        { "redirect_uri", "http://www.example.com/" },
        { "client_id", "xxxx" },
        { "scope", "publish_stream" }
    };

    Response.Redirect(facebookClient.GetLoginUrl(parameters).ToString(), true);

}

我的web.config看起来像这样:

<configuration>

    <configSections>
        <section name="facebookSettings" type="Facebook.FacebookConfigurationSection" />
    </configSections>

    <system.web>

        <customErrors mode="Off" />

        <compilation debug="true" targetFramework="4.0" />

    </system.web>

    <system.webServer>

        <modules runAllManagedModulesForAllRequests="true" />

        <handlers>
            <add name="facebookredirect.axd" verb="*" path="facebookredirect.axd" type="Facebook.Web.FacebookAppRedirectHttpHandler, Facebook.Web" />
        </handlers>

    </system.webServer>

    <facebookSettings appId="xxxx" appSecret="xxxx" />

</configuration>

看起来我的网站没有意识到我已经过身份验证,因此会重定向到Facebook登录信息。 Facebook然后知道我已经过身份验证,因此它会重定向回来,导致循环。

2 个答案:

答案 0 :(得分:2)

这样的循环通常是由不喜欢接受第三方cookie的浏览器引起的。尝试在网络服务器的响应中设置P3P标头。有关详细信息,请参阅:http://www.hanselman.com/blog/TheImportanceOfP3PAndACompactPrivacyPolicy.aspx。快乐的编码!

答案 1 :(得分:0)

我在http://aka.ms/need01开发了一些不使用cookie的示例代码。基本上,它使用我编写的自定义身份验证模块(会话状态存储在Windows Azure表中,但您可以编写一个使用另一个存储的模块)并将其余状态存储在URL中。 希望这有帮助