1

On SC 9.0.2 we want to map an existing user in sitecore automatically to an external identity provider, but get the following error:

[InvalidOperationException: Unable to create a user. Reason: DuplicateUserName]
   Sitecore.Owin.Authentication.Identity.MembershipUserStore`1.CreateAsync(TUser user) +475
   Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +961
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
   Sitecore.Owin.Authentication.Services.<ResolveApplicationUserAsync>d__13.MoveNext() +949
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
   Sitecore.Owin.Authentication.Identity.<HandleFailureAsync>d__16.MoveNext() +548
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
   Sitecore.Owin.Authentication.Pipelines.Initialize.<<HandleExternalLoginCallbackUrl>b__10_1>d.MoveNext() +1341
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31

We override the ExternalUserBuilder:

public class CustomExternalUserBuilder : Sitecore.Owin.Authentication.Services.ExternalUserBuilder
{
    public bool IsPersistentUser { get; private set; }

    public override ApplicationUser BuildUser(UserManager<ApplicationUser> userManager, ExternalLoginInfo externalLoginInfo)
    {
        var userName = $"custom\\{externalLoginInfo.ExternalIdentity.FindFirstValue("preferred_username")}";

        return new ApplicationUser(userName)
        {
            IsVirtual = !this.IsPersistentUser
        };
    }
}

and the UserAttachResolver:

public class CustomUserAttachResolver : UserAttachResolver
{
    public override UserAttachResolverResult Resolve(UserAttachContext context)
    {
        UserManager<ApplicationUser> userManager = context.OwinContext.GetUserManager();
        var user = userManager.FindByName(context.OwinContext.Request.User.Identity.Name);

        if(user != null)
        {
            return new UserAttachResolverResult(UserAttachResolverResultStatus.Attach);
        }

        return new UserAttachResolverResult(UserAttachResolverResultStatus.New);
    }
}

But allthough the CustomUserAttachResolver is added to the serviceConfigurator, it is never called. I tried to remove the existing UserAttachResolver, but I guess it's too late and that the DefaultApplicationUserResolver is already instantiated with the existing UserAttachResolver.

3
  • Have you registered your CustomUserAttachResolver via a ServiceConfigurator class?
    – josedbaez
    Commented Apr 12, 2019 at 15:55
  • @Luuk- I can't tell from your answer if "added to the serviceConfigurator" means the class or if you registered your service configurator under the <sitecore><services> node. Have you given this a read? doc.sitecore.com/developers/90/sitecore-experience-management/… Commented Apr 13, 2019 at 3:56
  • Yes, i've added serviceCollection.AddSingleton<UserAttachResolver, CustomUserAttachResolver>(); and it's being hit (after the owin one). I've also tried to remove the UserAttachResolver from the serviceCollection before adding, but didn't work either.
    – Luuk
    Commented Apr 14, 2019 at 12:56

0

Your Answer

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

Browse other questions tagged or ask your own question.