Monday, 8 July 2013

How To Create Facebook Login in asp.net MVC

How to Create Facebook Login in asp.net MVC OR How to Create Facebook Login in MVC OR Facebook Login using asp.net MVC

Step-1:
Go to Facebook Developer .

Step-2:
Click on Create new App














Step-3:
Fill the AppName , AppCategory Field












Step-3:
Fill the Captcha Field











Step-4:
Fill the forms




















Step-5:

After Creating app of facebook go to tools->Library Package Manager->package Manger Console. After the console is open then write following command 
Install-Package Facebook 












Step-6:
Create a controller Called FBLogin and then create a action Called FaceBookLogin and FBInfo and Profile

public ActionResult FaceBookLogin()
       {
           if (Request.QueryString["code"] == null && Session["AccessToken"] == null)
           {
               var redirectUri = new UriBuilder(Request.Url);
               redirectUri.Path = Url.Action("FBInfo", "FBLogin");
               var client = new FacebookClient();
               var uri = client.GetLoginUrl(new
               {
                   client_id = fbclientid,
                   redirect_uri = redirectUri.Uri.AbsoluteUri,
                   response_type = "code",
                   scope = "email"
               });
               return Redirect(uri.ToString());
           }
           return RedirectToAction("FBInfo");

       }
public ActionResult FBInfo()
        {
             
            var fb = new FacebookClient();
            if (Request.QueryString["code"] != null && Session["AccessToken"] == null)
            {
                
                string accessCode = Request.QueryString["code"].ToString();
                dynamic result = fb.Post("oauth/access_token", new
                {

                    client_id = fbclientid,

                    client_secret = fbsecret,

                    redirect_uri = "http://localhost:4447/FBLogin/FBInfo",

                    code = accessCode

                });

                var accessToken = result.access_token;
                var expires = result.expires;

                // Store the access token in the session
                Session["AccessToken"] = accessToken;
                return RedirectToAction("FBInfo");
            }
            else if (Session["AccessToken"] != null)
            {
                fb.AccessToken = Session["AccessToken"].ToString();
                dynamic me = fb.Get("me?fields=friends,name,email");
                TempData["fbid"] = me.id;
                TempData["name"] = me.name;
                TempData["email"] = me.email;
                
            }
            return RedirectToAction("Profile");

        }
Public ActionResult Profile()
{
return View();
}

Step-7

Create a view Page named Profile.aspx and write the following code
<a href="/FBLogin/FaceBookLogin"><img src="../../Image/facebook.gif" /></a><br />
<% if( TempData["fbid"] != null ) { %>
<label>UserID:<%= TempData["fbid"]%></label><br/>
<label>Name:<%= TempData["name"]%></label><br/>
<label>Email:<%= TempData["email"]%></label><br/>
<% } %>

No comments:

Post a Comment