Skip to content

Instantly share code, notes, and snippets.

@tathagata
Created April 24, 2012 23:39
Show Gist options
  • Select an option

  • Save tathagata/2484627 to your computer and use it in GitHub Desktop.

Select an option

Save tathagata/2484627 to your computer and use it in GitHub Desktop.
Facebook login example
@if(Request.IsAuthenticated) {
<text><img src="http://graph.facebook.com/@User.Identity.Name/picture" alt=""/> <strong>@User.Identity.Name</strong>!
[ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
}
else
{
<text>Login using Facebook:@User.Identity.Name </text>
<fb:login-button scope="email,user_checkins" onlogin="afterFacebookConnect();"
autologoutlink="false" ></fb:login-button>
<div id="fb-root" style="display:inline; margin-left:20px;"></div>
<text>OR </text>
@:[ @Html.ActionLink("Log On", "LogOn", "Account") ]
}
<script language="javascript" type="text/javascript">
window.fbAsyncInit = function () {
FB.init({ appId: 144248845702538,
status: true, cookie: false, xfbml: true
});
};
function afterFacebookConnect() {
FB.getLoginStatus(function (response) {
if (response.authResponse) {
console.log("Calling controller");
window.location = "../Account/FacebookLogin?token=" +
response.authResponse.accessToken;
} else {
// user clicked Cancel
console.log("response.session did not come true");
}
});
};
$(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>
@if(Request.IsAuthenticated) {
<text>Welcome <strong>@User.Identity.Name</strong>!
[ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
}
else {
@:[ @Html.ActionLink("Log On", "LogOn", "Account") ]
}
@tathagata
Copy link
Copy Markdown
Author

    public ActionResult FacebookLogin(string token)
    {
        System.Diagnostics.Debug.WriteLine(token);
        WebClient client = new WebClient();
        string JsonResult = client
            .DownloadString(string.Concat("https://graph.facebook.com/me?access_token=", token));
        JObject jsonUserInfo = JObject.Parse(JsonResult);
        string username = jsonUserInfo.Value<string>("username");
        string locale = jsonUserInfo.Value<string>("locale");
        string email = jsonUserInfo.Value<string>("email");
        string picture = jsonUserInfo.Value<string>("picture");
        int facebook_userID = jsonUserInfo.Value<int>("id");

        FormsAuthentication.SetAuthCookie(username, true);

        return RedirectToAction("Index", "Home");

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment