ASP.NET website authentication with facebook

前端 未结 1 1499
无人及你
无人及你 2020-12-06 13:58

I\'ve seen a lot of documentation about integration between ASP .NET web sites and Facebook, but I haven\'t found a simple working example, even using Facebook C# SDK.

相关标签:
1条回答
  • 2020-12-06 14:42

    as you say using Facebook C# SDK, then here is path and some code for canvas application:

    1- Create your web application from visual studio
    2- install nuget and get by nuget Facebook C# SDK
    3- from https://developers.facebook.com/apps/ create and configure your app.
    4- Your web config for facebook integration :

    <configuration>
      <configSections>
        <section name="facebookSettings" type="Facebook.FacebookConfigurationSection" />
      </configSections>
      <facebookSettings appId="123..." appSecret="abc...." siteUrl="http://apps.facebook.com/myapp/" canvasPage="http://app.facebook.com/myapp" secureCanvasUrl="https://myapp.com/" canvasUrl="http://myapp.com/" cancelUrlPath="http://www.facebook.com/" />
    ...
    

    By using sdk, you can parse signed request or cookie written by facebook js sdk

    FacebookWebContext fbWebContext = new FacebookWebContext();
    //Check if user auhtenticated
    bool IsAuthenticated = fbWebContext.IsAuthenticated(); 
    

    Here you can have friend count by:

    FacebookWebClient fbWebClient = new FacebookWebClient();
    dynamic result = fbWebClient.Get("me/friends");
    var friends = result["data"];
    int frienCount = friends.Count;
    

    For the client side:

    <body> 
    <div id="fb-root"></div>
    <script>
    
    window.fbAsyncInit = function () {
        FB.init({ 
        appId: '123...', 
        status: true, 
        cookie: true, 
        xfbml: true, 
        oauth:true  });
    };
    (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);            
    } ());
    
    <!-- rest of your html -->
    
    </body>
    

    For login & asking permission from javascript

        FB.getLoginStatus(function(response) {
            console.log( response );
            if ((response.status)&&(response.status=='connected')) {
                //successs
            } else {
                //user declined 
               }, {scope:'user_likes, offline_access'}
    
        });
    

    I prefer in my project to client side login thus not yet registered user have landing page, if for example submit form then I call code block above.

    Note: you have to set P3P header for Internet explorer to read/write cookie depending of your server. for IIS, global.asax:

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    
        HttpContext.Current.Response.AddHeader("p3p", "CP=\"CAO PSA OUR\"");
    
    }
    

    Volià

    0 讨论(0)
提交回复
热议问题