简体   繁体   中英

Facebook c# sdk login in asp.net 4.0 website

I know that similar question was asked several times, but still I can't make it work. I want to login on my site with newest facebook C# SDK and read logged in user e-mail and name.

Here is my web.config facebook configuration:

  <configSections>
    <section type="Facebook.FacebookConfigurationSection, Facebook" 
    name="facebookSettings" allowLocation="true" allowDefinition="Everywhere" />
  </configSections>
  <facebookSettings appId = "{appId}" appSecret = "{appSecret}"/>

Here is my facebook sdk and client side code:

  Your facebook name is :<asp:Label ID="Label1" runat="server" Text="Not authenticated."></asp:Label>

<div id="fb-root">
</div>
<fb:login-button id="fblogin" onlogin="window.open('http://www.filmania.pl/Default.aspx?code=1', '_self')">
</fb:login-button>
<fb:prompt-permission perms="email"></fb:prompt-permission>
<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: 'applicationId',
            cookie: true,
            xfbml: true,
            oauth: true
        });
        function facebooklogin() {
            FB.login(function (response) {
                if (response.authResponse) {
                    // user authorized
                    //window.location.reload();
                    window.open("http://www.filmania.pl/Default.aspx?code=1", "_self")
                } else {
                    // user cancelled
                }
            }, { scope: 'email' });
        };
    };
    (function () {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    } ());
</script>

This is my server side code:

if (Request.QueryString["code"] == "1")
    {
        try
        {
            var fbWebContext = new FacebookWebContext();
            Label1.Text += fbWebContext.IsAuthenticated();

            if (fbWebContext.IsAuthenticated())
            {
                var fb = new FacebookWebClient();
                dynamic me = fb.Get("me");
                Label1.Text += me.email + " " + me.name;
            }

        }
        catch (Exception ex)
        {
            Label1.Text += ex;
        }
    }

After clicking Facebook Login Button facebook popup show up and is asking for permission. Seems to work fine, but after server side code magic Label1.Text is always == false. What Am I missing here? Please help me out, I'm lost.

Per the Javascript SDK, you'll want to call FB.getLoginStatus prior to calling FB.login. That might change up your workflow some.

See: https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/ and https://developers.facebook.com/docs/reference/javascript/FB.login/

I have worked with older versions of facebook sdk api but i now i made an example for the newest stable version 5.4.1.0 wich i found on codeplex...

the example is here: http://www.simpa.dk/Projects.aspx?CategoryId=1&IndexId=2

hope this can help others...:) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM