facebook sdk c#/ ASP.Net登录后保持清爽

时间:2015-01-28 17:53:43

标签: c# facebook sdk

我遇到了一些问题,当我使用facebook sdk进行登录时,它会不断刷新网站。

我试图围绕javascript制作一些if语句以检查cookie,但它不起作用。

我的javascript在ASP.Net中的MasterPage上。

`     

    window.fbAsyncInit = function () {
        FB.init({
            appId: '***********', // 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;


                if (sessvars.fbauthenticated == undefined) {
                    sessvars.fbauthenticated = "1";

                    // 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');


                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/da_DK/all.js";
        ref.parentNode.insertBefore(js, ref);
    }(document));
</script>

我的处理程序文件如下:

        var accessToken = context.Request["accessToken"];
        context.Session["AccessToken"] = accessToken;
        HttpCookie cookie = new HttpCookie("SuHelperAuth");
        cookie["userid"] = accessToken;
        cookie.Expires = DateTime.Now.AddDays(14);
        context.Response.Cookies.Add(cookie);

        context.Response.Redirect("/satser.aspx");

}
public bool IsReusable
{
    get { return false; }
}

`

1 个答案:

答案 0 :(得分:1)

我认为您的IF语句也需要封装以下内容。

                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();

我可能会在左侧的场地出路。