2

I have social log in set up with federated authentication on a Sitecore 9 instance. I am able to login with facebook from my front end site: enter image description here

Where can a capture this 'hVEQItzjCrLQI1Fuc0v3pQ' on login? I looked up this blog post: http://blog.nikkipunjabi.com/2018/03/sitecore-federated-authentication-part-2-google-and-facebook-app-and-the-custom-processor.html

And the ProcessCore method does provide me with OnAuthenticated handler, but I dont see this sitecore generated user id in any object. I only see the claims data returned from facebook - I also checked in the handler for OnReturnEndpoint - and dont see it there too.

I am pretty much using the same code as in the example. Just added some code to show the login buttons on the front end site.

public class FacebookAuthentication : IdentityProvidersProcessor
{
    public FacebookAuthentication(FederatedAuthenticationConfiguration federatedAuthenticationConfiguration) : base(federatedAuthenticationConfiguration)
    {
    }

    protected override string IdentityProviderName => "Facebook";

    protected override void ProcessCore(IdentityProvidersArgs args)
    {
        Assert.ArgumentNotNull(args, "args");
        IdentityProvider identityProvider = GetIdentityProvider();
        GetAuthenticationType();

        FacebookAuthenticationProvider facebookProvider = new FacebookAuthenticationProvider
        {
            OnAuthenticated = context =>
            {
                // transform all claims
                ClaimsIdentity identity = context.Identity;
                foreach (Transformation current in identityProvider.Transformations)
                {
                    current.Transform(identity, new TransformationContext(FederatedAuthenticationConfiguration, identityProvider));
                }
                return System.Threading.Tasks.Task.FromResult(0);
            },

            OnReturnEndpoint = (context) =>
            {
                if (context.Request.Query["state"] != null)
                {
                    var state = HttpUtility.ParseQueryString(context.Request.Query["state"]);
                    //Tring stuff here, trying to resolve the sitecore user name hash from facebook info??
                    IOwinContext context1 = HttpContext.Current.GetOwinContext();
                    UserManager<ApplicationUser> userManager = context1.GetUserManager();
                }

                return System.Threading.Tasks.Task.FromResult(0);
            }
        };

        IdentifyProviderCredentials credentials = IdentityProviderConfiguration.Current.GetCredentials(GetType().Name);

        FacebookAuthenticationOptions options = new FacebookAuthenticationOptions
        {
            AppId = credentials.AppId,
            AppSecret = credentials.AppSecret,
            Provider = facebookProvider
        };
        options.Fields.Add("name");
        options.Fields.Add("email");
        options.Scope.Add("email");
        args.App.UseFacebookAuthentication(options);
    }
}

Any ideas?

2
  • Can you share also your code? Remember that the article referenced in your question is doing federated authentication on backend. In your case you are trying to do the same on frontend. In article it is describe how username is generated automatically by Sitecore and that it is hashed. I would search user by email field as you receive this information also from Facebook in claims and it should be unique on Facebook side as you cannot register two account with same email address. Jun 8, 2018 at 17:44
  • @PeterProchazka I've updated my code, but its pretty much the same as the blog. I just added more code to show the buttons on the front end. I thought of searching by email - but I have left the username generation as default, so technically I can have multiple users with same email in the Email column, since the Fully Qualified name will be different from different social networks. The only differentiator would be the identity provider name in the comment field. I am wondering if that is a reliable / recommended way to pull user?
    – Durga Bose
    Jun 8, 2018 at 18:18

1 Answer 1

1

The username is generated by Sitecore via Sitecore.Owin.Authentication.Services.DefaultExternalUserBuilder.

User names must be unique across a Sitecore instance. You cannot use user names from different external providers as Sitecore user names because this does not guarantee that the user names are unique. The DefaultExternalUserBuilder class creates a sequence of user names for a given external user name. It then uses the first of these names that does not already exist in Sitecore. The values in the sequence depend only on the external username and the Sitecore domain configured for the given identity provider.

You can change this functionality by patching the config to use your own username builder like:

<identityProvidersPerSites hint="list:AddIdentityProvidersPerSites">
<mapEntry name="visitors" type="Sitecore.Owin.Authentication.Collections.IdentityProvidersPerSitesMapEntry, Sitecore.Owin.Authentication">
 <externalUserBuilder type="MyNamespace.ExternalUserBuilder, MyLibrary">
    <param desc="isPersistentUser">false</param>
  </externalUserBuilder>
</mapEntry>

And the implementation:

public class ExternalUserBuilder : Sitecore.Owin.Authentication.Services.DefaultExternalUserBuilder
{        
    protected override string CreateUniqueUserName(UserManager<ApplicationUser> userManager, ExternalLoginInfo externalLoginInfo)
    {        
        return externalLoginInfo.ExternalIdentity.FindFirstValue("FullName");
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.