ASP.NET website authentication with facebook

a 夏天 提交于 2019-11-28 00:16:20

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à

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!