Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

web - ASP.NET website authentication with facebook

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.

All I want is a "login with Facebook" example, and to get basic user information (such as name, email and photo).

Can you guys help me please?

Thanks a lot!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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à


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...