2

現在、Facebook Developerに記録されているバグを解決しようとしています: https ://developers.facebook.com/bugs/325086340912814/

Facebook C#SDKおよびFacebookJavascriptSDKでASP.NETMVC3を使用しています。

javascriptコードは次のとおりです。

window.fbAsyncInit = function () {
// init the FB JS SDK
FB.init({
    appId: '298365513556623', // App ID from the App Dashboard
    channelUrl: 'http://www.christianinfoportal.com/channel.html', // Channel File for x-domain communication
    status: true, // check the login status upon init?
    cookie: true, // set sessions cookies to allow your server to access the session?
    xfbml: true  // parse XFBML tags on this page?
});

// Additional initialization code such as adding Event Listeners goes here
FB.Event.subscribe('auth.authResponseChange', function (response) {
    if ((response.status == 'connected')) {
        if (fbIgnoreFirstEventFire)
            fbIgnoreFirstEventFire = false;
        else {
            // 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", '/FBLogin');

            var field = document.createElement("input");
            field.setAttribute("type", "hidden");
            field.setAttribute("name", 'accessToken');
            field.setAttribute("value", accessToken);
            form.appendChild(field);

            var field = document.createElement("input");
            field.setAttribute("type", "hidden");
            field.setAttribute("name", 'returnUrl');
            field.setAttribute("value", window.location.href);
            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's source Asynchronously
(function (d, debug) {
    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" + (debug ? "/debug" : "") + ".js";
    ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));

ビューコードは非常に単純です。

<div id="fbLoginButton" class="fb-login-button" data-show-faces="false" data-width="50" data-max-rows="1" data-scope="email"></div>

基本的に、アクセストークンをサーバー側のコードページに転送します。

public ActionResult FBLogin(String returnUrl)
    {
        var accessToken = Request["accessToken"];

        //FB fires even without the user clicking the Log In button
        Session["FBLoginFirstClick"] = true;

        //Session["AccessToken"] = accessToken;

        //this.lblAccessToken.Text = accessToken;
        //Response.Redirect("/MyUrlHere");

        var client = new Facebook.FacebookClient(accessToken);
        dynamic me = client.Get("me");

        String email, fullName;
        fullName = me.first_name + " " + me.last_name;
        email = me.email;

        Models.User.ThirdPartyLogin(email, fullName, Session);

        if (!string.IsNullOrEmpty(returnUrl))
        {
            return Redirect(returnUrl);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }
4

1 に答える 1

1

Facebook認証リダイレクトを使用して解決しました。

https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/

于 2014-02-04T08:33:32.070 に答える