Integrate Facebook login Authentication to Website Using in Asp.Net C#

Integrate Facebook login Authentication to Website

Website Integrate Facebook login Id Using asp.net C#.

                       Download Coding

                                           Download

                                 DEMO

                         HTML CODING


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
      
    <div align="center">
        <br />
         <asp:Button ID="Button1" runat="server" Text="Login  facebook" OnClick="Button1_Click" />
        <br />
        <br />
        <asp:Label ID="lblEmail" runat="server" Text="Email"></asp:Label>
        &nbsp;<asp:Label ID="lblEmailId" runat="server" ForeColor="Red"></asp:Label>
        <br />
        <asp:Label ID="lblName" runat="server" Text="Name:" Visible="False"></asp:Label>
           <asp:Label ID="lblUName" runat="server" ForeColor="Red" Visible="False"></asp:Label>
        <br />
        <asp:Label ID="lblUserId" runat="server" Text="User Id:" Visible="False"></asp:Label>
       
           <asp:Label ID="lblUid" runat="server" ForeColor="Red" Visible="False"></asp:Label>
      
    </div>
    </form>
</body>

</html>

                                                 C# CODING


using Facebook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AccessToken"] != null)
        {
            GetUserData(Session["AccessToken"].ToString());
        }
        else if (Request.QueryString["code"] != null)
        {
            string accessCode = Request.QueryString["code"].ToString();
            var fb = new FacebookClient();
            // throws OAuthException
            dynamic result = fb.Post("oauth/access_token", new
            {
                client_id = "1112433425589974",
                client_secret = "d1d20bf5c5e4ba39b714937a6525f8768",
                redirect_uri = "http://localhost:7137/Default.aspx",
                code = accessCode
            });
            var accessToken = result.access_token;
            var expires = result.expires;
            // Store the access token in the session
            Session["AccessToken"] = accessToken;
            GetUserData(Session["AccessToken"].ToString());
        }
    }


    private void GetUserData(string accessToken)
    {
        var fb = new FacebookClient(accessToken);

        dynamic me = fb.Get("me?fields=friends,name,email");

        string id = me.id;
        string email = me.email;
        string FBName = me.name;           
        lblEmailId.Text =  email;
        lblUid.Text =  id;      
        lblUName.Text = FBName;

        ViewState["FBName"] = FBName; // Storing User's Name in ViewState

       
        Button1.Text = "Log Out";
    }
    private void dologin()
    {
        FacebookClient fb = new FacebookClient();
        var loginUrl = fb.GetLoginUrl(new
        {
            client_id = "1112433425589974",
            redirect_uri = "http://localhost:7137/Default.aspx",
            response_type = "code",
            scope = "email" // Add other permissions as needed
        });
        Response.Redirect(loginUrl.AbsoluteUri);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Button1.Text == "Log Out")
            logout();
        else
        {
            dologin();
        }
    }
    private void logout()
    {
        var fb = new FacebookClient();
        var logoutUrl = fb.GetLogoutUrl(new
        {
            access_token = Session["AccessToken"],
            next = "http://localhost:7137/logout.aspx"

        });
        // User Logged out, remove access token from session
        Session.Remove("AccessToken");
        Button1.Text = "Log In with Fb";
        Response.Redirect(logoutUrl.AbsoluteUri);
    }

}


First - Create - Facebook AppID - https://www.facebook.com/developer - MyApp - Select - WWW





Give Your App Name - Create New id






Choose - Category - Create App Id






Next - Show Appid - Current Working Website Url Give - Site URL






Next - Add Tools - NuGet Package Manager - Manager NuGet Packager for Solution 






Next - Search - Facebook C# sdk  - Show SDKs - Install Suitable SDK 






Next - Add Button & Required Tolls From ToolBox





Next - Go To - Facebook Developer Dashboard - AppId & App Secrete 






Next - Add - Your App Id & Secrete Key in Coding






Settings - Valid EmailId Enter  - Save






Next - Add Public - YES Button Click For Permission






1 comments:

Post a Comment