开发者

ASP.Net and Facebook: Logging-in via ASP.Net

开发者 https://www.devze.com 2023-01-28 23:25 出处:网络
I want to enable Facebook authentication and the FB-Graph in my website, which already has forms authentication. Using http://multitiered.wordpress.com/2010/08/05/getting-started-with-the-facebook-c-s

I want to enable Facebook authentication and the FB-Graph in my website, which already has forms authentication. Using http://multitiered.wordpress.com/2010/08/05/getting-started-with-the-facebook-c-sharp-sdk/, I was able to figure out how to login server-side.

However, the problem with this approach is that a secure cookie will not be created, since the call returns the authentication code in the querystring via a callback. This means that the user will have to login every time.

I can see two ways around this:

  1. Store the access token in a secure cookie manually
  2. Instead of开发者_JAVA技巧 the above approach, use the FB JS API to login - this stores a secure cookie with the access token automatically

I would prefer not to use the second approach, as I would like the login code to be server-side.

Which would be the better approach? Am I missing something?


I use the JavaScript method to first authenticate the user, the JS SDK then writes an encrypted cookie (called "fbs_[YourAppID]") when a connected user hits your page; using one of the many Facebook c# SDKs, this cookie can be decoded using your application secret giving you the user ID, oAuth token, expiry date etc.

Then I hook into the AuthenticateRequest event of my .NET application, check the presence of the cookie, decode if it found, and then find a user who has been assigned this facebook ID (your user table must have a extra field for storing the ID of their facebook account).

If a match is found, I write a normal forms authentication cookie for this user, then .NET will recognise them for all future requests. If no user is found, then this is a brand new user who has just connected. Use the SDK again to query the graph API using their oAuth token, get things like their name/email etc and create a new account, then issue a authentication token as normal.

By writing a normal authetication cookie, the user will stay logged into to your site for all requests, just as if they were a normal user.

One side point, when using email address, check for duplicates, and check for the facebook cookie in all requests. For example, an existing registered logged in user may have just connected.

0

精彩评论

暂无评论...
验证码 换一张
取 消