Click here to Skip to main content
15,900,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
[AllowAnonymous]
public ActionResult Facebook()
{
    var fb = new FacebookClient();
    var loginUrl = fb.GetLoginUrl(new
    {
        client_id = "xxxxxx",
        client_secret = "xxxxx",
        redirect_uri = RedirectUri.AbsoluteUri,
        response_type = "code",
        scope = "email"
    });

    return Redirect(loginUrl.AbsoluteUri);
}
public ActionResult FacebookCallback(string code)
{
    var fb = new FacebookClient();
    dynamic result = fb.Post("oauth/access_token", new
    {
        client_id = "xxxxxx",
        client_secret = "xxxx",
        redirect_uri = RedirectUri.AbsoluteUri,
        code = code
    });

    var accessToken = result.access_token;

    // Store the access token in the session for farther use
    Session["AccessToken"] = accessToken;

    // update the facebook client with the access token so
    // we can make requests on behalf of the user
    fb.AccessToken = accessToken;

    // Get the user's information, like email, first name, middle name etc
    dynamic me = fb.Get("me?fields=first_name,middle_name,last_name,id,email");
    string email = me.email;
    string firstname = me.first_name;
    string middlename = me.middle_name;
    string lastname = me.last_name;
    if (ModelState.IsValid)
    {
        User _user = new User();
        _user.FullName = firstname;
        _user.Email = email;
        _user.PhoneNo = "";
        // _user.Password = model.Security.Password;
        //_user.RetypePassword = model.Security.RetypePassword;
        //_user.CityId = model.Security.CityId;
        //_user.IsUpdate = model.Security.IsUpdate;
        _user.StatusId = (byte)Utilities.Status.Active;
        _user.EmailConfirmed = true;
        _user.Image = "/images/business.png";
        _user.CreatedDate = DateTime.Now;
        _user.ModifiedDate = DateTime.Now;
        _user.CreatedBy = 1;
        var userObj = seuritybll.Insert(_user);
        Session["User"] = me;
        Session["UserName"] = firstname;
        return RedirectToAction("Index", "Home");
        //return RedirectToAction("RegistrationSuccessfull");
    }
    // Set the auth cookie
    FormsAuthentication.SetAuthCookie(email, false);
    return RedirectToAction("Index", "Home");
}


What I have tried:

I used above code for Login with facebook in asp.net mvc website
But using this code i can login just from my facebook account.When i login from other account it gave error Like that "your app is not created in developer option" i followed this link
can you help me please
https://www..com/Articles/577384/Introduction-to-OAuth-in-ASP-NET-MVC
Posted
v3

1 solution

 
Share this answer
 
Comments
Aitzaz Ahsan 30-May-17 6:31am    
i already read this topic its working fine in this case when i created APP ID and App Secrete of mine acoount when i login from another account it gave errors like this "pp Not Setup: This app is still in development mode, and you don't have access to it. Switch to a registered test user or ask an app admin for permissions."
but when i login from my account its working fine can you tell me
manu_dhobale 30-May-17 7:58am    
You have to go to app review settings, refer below link

http://fellowtuts.com/html-css/facebook-app-is-still-in-development-mode-dont-have-access/
Aitzaz Ahsan 30-May-17 8:08am    
Thank you so much its working fine now thank you... Great...
Aitzaz Ahsan 31-May-17 2:54am    
manu_dhobale when i login with Facebook this hashes "#_=_" are append at the end of Url
how i can solve this problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900