1

This is my custom provider class:

public class Facebook : IdentityProvidersProcessor
{
    public Facebook(FederatedAuthenticationConfiguration federatedAuthenticationConfiguration,
                    ICookieManager cookieManager,
                    BaseSettings settings)
                    : base(federatedAuthenticationConfiguration, cookieManager, settings)
    {
    }

    protected override string IdentityProviderName => "Facebook";

    protected override void ProcessCore(IdentityProvidersArgs args)
    {
        Assert.ArgumentNotNull(args, nameof(args));

        var identityProvider = this.GetIdentityProvider();
        var authenticationType = this.GetAuthenticationType();

        var options = new FacebookAuthenticationOptions
        {
            Caption = identityProvider.Caption,
            AuthenticationType = authenticationType,
            AuthenticationMode = AuthenticationMode.Passive,
            AppId = Settings.GetSetting("Feature.Accounts.Facebook.AppId"),
            AppSecret = Settings.GetSetting("Feature.Accounts.Facebook.AppSecret"),
            Provider = new FacebookAuthenticationProvider
            {
                OnAuthenticated = context =>
                {
                    context.Identity.ApplyClaimsTransformations(new TransformationContext(this.FederatedAuthenticationConfiguration, identityProvider));
                    AddClaim(context, "full_name", "name");
                    AddClaim(context, "email", "email");
                    return Task.CompletedTask;
                }
            },
            Scope =
            {
                "public_profile",
                "email"
            },
            Fields =
            {
                "name",
                "email",
            }
        };

        FacebookAuthenticationExtensions.UseFacebookAuthentication(args.App, options);
    }

    private void AddClaim(FacebookAuthenticatedContext context, string claimName, string propertyName)
    {
        var value = context.User[propertyName]?.ToString();
        if (propertyName == "picture")
        {
            value = context.User["picture"]?["data"]?["url"]?.ToString();
            if (value == null)
                return;
            context.Identity.AddClaim(new Claim("picture_url", value));
            context.Identity.AddClaim(new Claim("picture_mime", "image/jpg"));
            return;
        }

        if (value == null)
            return;
        context.Identity.AddClaim(new Claim(claimName, value));
    }
}

So i am able to login by using facebook successfully but whenever i am changing the page the cache whatever i added at the time of facebook login getting removed.

1
  • What kind of cache are you talking about here? There is no cache in your examples. Jan 24, 2020 at 20:49

1 Answer 1

0

You need to login the FB user into Sitecore as a Virtual User. By doing that,you will be able to check from Sitecore.Context.User.IsAuthenticated on other pages ,if user is logged in/not, then show/hide relevant content. Below code block will create and login the virtual user. This can be used on the OnAuthorizationCodeReceived() of your Facebook pipeline

       User virtualUser = Sitecore.Security.Authentication.AuthenticationManager.BuildVirtualUser(userName, false);

        virtualUser.RuntimeSettings.Load();
        virtualUser.RuntimeSettings.AddedRoles.Clear();
        virtualUser.Roles.RemoveAll();

        // Optional: Add the user to an existing extranet role
        virtualUser.Roles.Add(Role.FromName("Facebook\\"));

        // set profile properties
        virtualUser.Profile.Email = userName;
        virtualUser.Profile.Name = displayName;
        virtualUser.Profile.FullName = displayName;
        virtualUser.Profile.Comment = "User Created from Facebook";
        foreach (var prop in properties)
        {
            // virtualUser.Profile.SetCustomProperty(prop.Key, prop.Value);
        }
        virtualUser.Profile.Save();
        virtualUser.RuntimeSettings.IsVirtual = true;

        virtualUser.RuntimeSettings.Save();
        user = virtualUser;
        _res = true;
        virtualUser.Profile.Reload();

        // Login the virtual user
        Sitecore.Security.Authentication.AuthenticationManager.LoginVirtualUser(virtualUser);    
1

Your Answer

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

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