April 30, 2012

Get Facebook user Emailid in ASP.Net

Using JavaScript SDK we can access user’s emailed in Facebook application.
Here is working example for ASP.Net.
 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FBLogin.aspx.cs" Inherits="FBLogin" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns
="http://www.w3.org/1999/xhtml">
<
head runat="server">
    <title></title>
    <style type="text/css">
        #login
        {
            width: 95px;
        }
    </style
>
</
head
>
<
body>
    <button id="login" class="fb-button">
        Login</button>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
    <div id="fb-root">
    </div>
    <script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script>
    <script type="text/javascript">
        FB.init({
            appId:'APP ID',
            xfbml:true,
            status:true, //check login status
            cookie:true, //enable cookies to allow server to access session,
            xfbml:true, //enable XFBML and social plugins
            oauth: true, //enable OAuth 2.0
            channelUrl: 'http://localhost/channel.htm' //custom channel
        });
        $('#login').bind('click', function () {
            FB.login(function (response) {
                if (response.authResponse) {
                    FB.api('/me', function (response) {
                        $("#txtUser").val(response.id);
                        $("#txtEmail").val(response.email);
                        FB.logout(function (response) {
                        });
                    });
                }
            }, { scope: 'email' });
        });
    </script>
    <form id="form1" runat="server">
    <div>
        <b>User Details</b><br />
        <br />
        <table align="left">
            <tr>
                <td>
                    UserID:
                </td>
                <td>
                    <asp:TextBox ID="txtUser" runat="server" CssClass="input1" Width="100px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    EmailID:
                </td>
                <td>
                    <asp:TextBox ID="txtEmail" runat="server" CssClass="input1" Width="200px"></asp:TextBox>
                </td>
            </tr>
        </table>
    </div>
    </form
>
</
body
>
</
html
>

That’s It.
Enjoy Learning.

No comments:

Post a Comment