using Newtonsoft.Json.Linq;
Install-Package Newtonsoft.Json
"Copy Source | Copy HTML
- $ (document) .ready ( function () {
- if (document.getElementById ( 'fb-root' )! = undefined) {
- var e = document.createElement ( 'script' );
- e.type = 'text / javascript' ;
- e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js' ;
- e.async = true ;
- document.getElementById ( 'fb-root' ) .appendChild (e);
- }
- });
Copy Source | Copy HTML
- FB.init ({
- appId: 'YOUR_APP_ID' , cookie: true ,
- status: true , xfbml: true
- });
Copy Source | Copy HTML
- < fb: login-button perms = "email, user_checkins"
- onlogin = "onConnect ();" autologoutlink = "false">
- </ fb: login-button >
xmlns:fb="http://www.facebook.com/2008/fbml"
namespace.Copy Source | Copy HTML
- function onConnect () {
- FB.getLoginStatus ( function (response) {
- if (response.session) {
- window.location = "../Account/FbLogin?token=" + response.session.access_token;
- } else {
- // if user cancel
- }
- });
- };
Copy Source | Copy HTML
- public ActionResult FbLogin ( string token)
- {
- WebClient client = new WebClient ();
- string JsonResult = client.DownloadString ( string .Concat ( "https://graph.facebook.com/me?access_token=" , token));
- JObject jsonUserInfo = JObject.Parse (JsonResult);
- UInt64 facebook_userID = jsonUserInfo.Value < UInt64 > ( "id" );
- string username = jsonUserInfo.Value < string > ( "username" );
- string email = jsonUserInfo.Value < string > ( "email" );
- // can save this information
- FormsAuthentication.SetAuthCookie (username, true );
- return RedirectToAction ( "Index" , "Home" );
- }
Copy Source | Copy HTML
- @if (Request.IsAuthenticated)
- {
- < text > Welcome < strong > @ User.Identity.Name </ strong > ! [@ Html.ActionLink ("Log Off", "LogOff", "Account")] </ text >
- }
- else
- {
- < fb: login-button perms = "email, user_checkins" onlogin = "onConnect ();" autologoutlink = "false"> </ fb: login-button >
- < div id = "fb-script" st yle = "display: inline; margin-left: 20px;">
- </ div >
- @: [@ Html.ActionLink ("Log On", "LogOn", "Account")]
- }
- < script language = "javascript" type = "text / javascript" >
- window.fbAsyncInit = function () {
- FB.init ({appId: '177572352298948' , status: true , cookie: false , xfbml: true });
- };
- function afterConnect () {
- FB.getLoginStatus ( function (response) {
- if (response.session) {
- window.location = "../Account/FbLogin?token=" + response.session.access_token;
- } else {
- // if user cancel
- }
- });
- };
- $ (document) .ready ( function () {
- if (document.getElementById ( 'fb-root' )! = undefined) {
- var e = document.createElement ( 'script' );
- e.type = 'text / javascript' ;
- e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js' ;
- e.async = true ;
- document.getElementById ( 'fb-root' ) .appendChild (e);
- }
- });
- </ script >
Source: https://habr.com/ru/post/120837/
All Articles