Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matthieu Nelmes 102 posts 384 karma points
    Nov 27, 2015 @ 11:51
    Matthieu Nelmes
    0

    Authenticating Member via Facebook login

    Trying to determine the best way to allow website members to register/login with Facebook login.

    Getting the members to login with Facebook and retrieving an accessToken I can create a link between the member and their facebook account by storing their Facebook Id in the member.

    I'm just struggling with how to authenticate the member when they go to login.

    So, when a user goes to login via Facebook, I can retrieve the member via:

    var client = new FacebookClient(accessToken);
    dynamic user = client.Get("me");
    var memberService = Services.MemberService;
    IEnumerable<IMember> existingMembers = memberService.GetMembersByPropertyValue("facebookUserId", user.id);
    

    But I can't see how I can authenticate the user as the Members.Login method requires both username and password as per what would normally be supplied with a typical login form.

    I've tried:

    Members.Login(existingMember.Username, existingMember.RawPasswordValue)
    

    With no success

  • Matthieu Nelmes 102 posts 384 karma points
    Nov 30, 2015 @ 10:49
    Matthieu Nelmes
    103

    Anyone else who gets stuck on this, you can authenticate a user by simply setting the AutCookie session with:

    System.Web.Security.FormsAuthentication.SetAuthCookie('Username', true);
    

    Just to expand on my code, The user is Authenticated via the Facebook JS SDK.

    So on my Login View page I have:

    <script>
        window.fbAsyncInit = function () {
            FB.init({
                appId: 'MY_APP_ID',
                xfbml: true,
                version: 'v2.5'
            });
            // Additional initialization code here
            FB.Event.subscribe('auth.authResponseChange', function (response) {
                if (response.status === 'connected') {
                    // the user is logged in and has authenticated your
                    // app, and response.authResponse supplies
                    // the user's ID, a valid access token, a signed
                    // request, and the time the access token
                    // and signed request each expire
                    var uid = response.authResponse.userID;
                    var userAccessToken = response.authResponse.accessToken;
    
    
                    $.post("/umbraco/Surface/BookingAgentSurface/FacebookLogin", { accessToken: userAccessToken });
    
                } else if (response.status === 'not_authorized') {
                    // the user is logged in to Facebook,
                    // but has not authenticated your app
                } else {
                    // the user isn't logged in to Facebook.
                }
            });
        };
    
    
        (function (d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) { return; }
            js = d.createElement(s); js.id = id;
            js.src = "//connect.facebook.net/en_US/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
    </script>
    

    And to render the Login Button:

    <div class="fb-login-button text-center" data-max-rows="1" data-size="large" data-show-faces="false" data-auto-logout-link="true"></div>
    

    So once the user is logged in, the Facebook subscribe event posts back to my SurfaceController with the userAccessToken, so my SurfaceController looks something like this:

        public ActionResult FacebookLogin()
                {
                    string accessToken = HttpContext.Request["accessToken"];
                    try
                    {
                        var client = new FacebookClient(accessToken);
                        dynamic user = client.Get("me");
    
                        var memberService = Services.MemberService;
                        IEnumerable<IMember> ExistingMember = memberService.GetMembersByPropertyValue("facebookUserId", user.id);
    
                        if (ExistingMember.Any())
                        {
                            IMember existingMember = ExistingMember.FirstOrDefault();
                            System.Web.Security.FormsAuthentication.SetAuthCookie(existingMember.Username, true);
    
                             //USER IS LOGGED IN, REDIRECT TO ADMIN SECTION ETC
                            return Redirect(Constants.AppPaths.BOOKING_AGENT_DASHBOARD);
                        }
                        else
                        {
                           //USER IS NOT LOGGED IN
                            return "User not found ";
                        }
                    }
                    catch (Exception e)
                    {
                        return e.Message + " " + e.StackTrace;
                    }
                }
    
  • Biagio Paruolo 1456 posts 1595 karma points c-trib
    Jun 24, 2016 @ 14:47
    Biagio Paruolo
    0

    Which libraries have you used?

    Skybrud plus Facebook JS SDK?

  • Azaz ul haq 4 posts 72 karma points
    Sep 25, 2017 @ 08:34
    Azaz ul haq
    0

    My $post is generating error

  • Biagio Paruolo 1456 posts 1595 karma points c-trib
    Apr 01, 2019 @ 14:53
    Biagio Paruolo
    0

    +1

  • Biagio Paruolo 1456 posts 1595 karma points c-trib
    Nov 30, 2015 @ 20:41
    Biagio Paruolo
    0

    Good!

  • Sahar 10 posts 79 karma points
    Dec 20, 2017 @ 07:48
    Sahar
    0

    Which libraries have you used in above code. where is this "FacebookClient" coming from?

    I am also trying to implement external login for "Members" on my site.

    Thanks

  • Biagio Paruolo 1456 posts 1595 karma points c-trib
    Dec 20, 2017 @ 09:47
    Biagio Paruolo
    0

    You can use Facebook .NET SDK or Skybrud social

    https://github.com/abjerner/Skybrud.Social

  • Azaz ul haq 4 posts 72 karma points
    Dec 20, 2017 @ 09:53
    Azaz ul haq
    0

    Install UmbracoIdentityMember

  • Sahar 10 posts 79 karma points
    Dec 20, 2017 @ 09:55
    Sahar
    0

    Thanks, appreciate quick response.

Please Sign in or register to post replies

Write your reply to:

Draft