4

I am developing a basic web site in ASP.NET MVC 5 (using visual studio 2013). The site will use Facebook for user authentication and for retrieving initial profile data.like stack over flow login with facebook..access user name,profile photo etc..plz any one help me relevant sample

startup.auth.cs

 FacebookAuthenticationOptions fbao = new FacebookAuthenticationOptions();
        fbao.AppId = "****";
        fbao.AppSecret = "*****";
        fbao.Scope.Add("email");
        fbao.Scope.Add("user_birthday");
        fbao.Scope.Add("user_hometown");

        fbao.SignInAsAuthenticationType = Microsoft.Owin.Security.AppBuilderSecurityExtensions.GetDefaultSignInAsAuthenticationType(app);

        app.UseFacebookAuthentication(fbao);

Account controller>>Externallogincallback

   // GET: /Account/ExternalLoginCallback
    [AllowAnonymous]
    public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        ClaimsIdentity fboa = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
        //var email = ext.Claims.First(x => x.Type.Contains("emailaddress")).Value;
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            return RedirectToAction("Login");
        }

        // Sign in the user with this external login provider if the user already has a login
        var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);



        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
            case SignInStatus.Failure:
            default:
                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
        }
    }

Account view model

public class ExternalLoginConfirmationViewModel
{
    [Required]
    [Display(Name = "Email")]
    public string Email { get; set; }
    public string UserName { get; set; }
}

externalloginconfirmationview

<h4>Association Form</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<p class="text-info">
    You've successfully authenticated with <strong>@ViewBag.LoginProvider</strong>.
    Please enter a user name for this site below and click the Register button to finish
    logging in.
</p>
<div class="form-group">
    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Register" />
    </div>
</div>
  • Hi Surya, asking questions on here like this can sometimes attract people who would mark you down rather than try to help. It has no information on whether you have tried anything and a lot of people may assume you are asking someone to do it for you. A question like this may also get down voted because it would need a MASSIVE answer to explain. Try searching for something as simple as "how to login facebook using asp.net" on google. I just did and found quite a lot of results. – Keithin8a Jul 23 '15 at 10:20
  • but my question is not facebook login problem..profile details not accessable to databse..http://stackoverflow.com/questions/18942196/how-to-access-facebook-private-information-by-using-asp-net-identity-owin?rq=1 like this type samples are more cheked out..but no one yet correct solution – Aysha Parveen Pallikkara Jul 23 '15 at 10:37
  • Ideally you should put that all in your question. Put the code you already have and where is is not returning the data. Remember that when programming one error could come from many different places so if you can give us as much relevant information or code as you have then people will be more than willing to help. – Keithin8a Jul 23 '15 at 11:17
  • best solution for this [Facebook Graph API with access token only](http://stackoverflow.com/questions/31714500/access-email-address-in-the-oauth-externallogincallback-from-facebook-v2-4-api-i?lq=1) – Ravi Anand Feb 26 '17 at 22:24

1 Answers1

2

Following are some nice tutorials that may help solve your issue

http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

http://www.asp.net/mvc/overview/getting-started/aspnet-mvc-facebook-birthday-app

ItsZeus
  • 142
  • 12
  • my actual problem is assume my website is stackoverflow..and sign in in a new user ,then sign in with facebook button..click to login after acces our profile information like user name,profile photo,birthdate etc.. your refered link is asp.net facebook app creation.but my question actually web application alredy implemented facebook login..login success and but not retrived data into databse – Aysha Parveen Pallikkara Jul 23 '15 at 10:50
  • are you able to retrieve data from facebook and unable to save that data in local db. Or you are having issues fetching data from facebook ? – ItsZeus Jul 23 '15 at 12:08
  • ya..fetching data from facebook.but unable to save that data in local db..plz help me a solution or samples programs link any sourece.like github etc.. like stack over flow login with facebook..access user name,profile photo etc..plz any one help me relevant sample – Aysha Parveen Pallikkara Jul 23 '15 at 12:18
  • saving data in a database is fairly trivial task. I suppose you have created your necessary tables to save the data that is coming back from facebook ? If you have, can you share the c# statements where you have called facebook api and its returning data and subsequent statements where you are trying to save the data in the db. And it would be helpful what kind of issue/exception its raising if the data is not saved – ItsZeus Jul 23 '15 at 12:27
  • This link I shared earlier shows how to save data in database if you follow this tutorial it explains how to save data in db that is coming back from facebook http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on#1st – ItsZeus Jul 23 '15 at 12:35
  • 1
    Ok..but this link shown http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on#ap but Account controller>>externallogincallback function not retrived data..>>externalloginconfirmation is user added birthdate and hometown..but not facebook retrived information – Aysha Parveen Pallikkara Jul 23 '15 at 13:08
  • can you provide the logic u r using to save the data to db so a solution can be reached ? – ItsZeus Jul 23 '15 at 13:20
  • have you debugged your code ? where exactly are you receiving the exception or error ? – ItsZeus Jul 23 '15 at 14:23
  • [check here](http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on) .thsi above example how to access email . i have alredy email permessions.. – Aysha Parveen Pallikkara Sep 03 '15 at 06:57