Copied to clipboard

Flag this post as spam?

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


  • Alex J. 4 posts 114 karma points
    Jan 18, 2023 @ 14:10
    Alex J.
    0

    SSO Auto Linking / On External Login does not save Claims to Backend User

    Hey everyone

    I am currently having an issue with Umbraco not being able to save the claims to the Backend User after logging in.

    I have used the recommended approach and saved the claims exactly as described here: https://docs.umbraco.com/umbraco-cms/reference/security/auto-linking

    I am on version 10.2 and this is a backend user

    OnExternalLogin = (user, loginInfo) => {
        // You can customize the user before it's saved whenever they have
        // logged in with the external provider.
        // i.e. Sync the user's name based on the Claims returned
        // in the externalLogin info
    
        var extClaim = externalLogin
            .Principal
            .FindFirst("role");
    
        user.Claims.Add(new IdentityUserClaim<string>
        {
            ClaimType = extClaim.Type,
            ClaimValue = extClaim.Value,
            UserId = user.Id
        });
    return true;
    }
    

    Later I want to retrieve that claim in my Surface Controller that will return the User so I build the following:

    [HttpGet]
            public async Task<ClaimsIdentity> GetBackendUser()
            {
                var httpContext = _httpContextAccessor.HttpContext;
    
                var claims = httpContext.User.Claims;
    
                if (httpContext == null)
                    return new ClaimsIdentity();
    
                CookieAuthenticationOptions cookieOptions = _cookieAuthenticationOptionsSnapshot.Get(Umbraco.Cms.Core.Constants.Security.BackOfficeAuthenticationType);
    
                string backOfficeCookie = httpContext.Request.Cookies[cookieOptions.Cookie.Name!];
    
                if (string.IsNullOrEmpty(backOfficeCookie))
                    return new ClaimsIdentity();
    
                AuthenticationTicket unprotected = cookieOptions.TicketDataFormat.Unprotect(backOfficeCookie!);
                ClaimsIdentity backOfficeIdentity = unprotected!.Principal.GetUmbracoIdentity();
    
                var user = await _userManager.FindByEmailAsync(backOfficeIdentity.GetEmail());
    
                return backOfficeIdentity;
            }
    

    This also works successfully in terms of that I get the Backend User successfully from the database but the Claims are not part of the User.

    enter image description here

    So I debugged the code and when I get the user using the _userManager I get the user successfully but the claims which I added OnExternalLogin are not part of the User anymore.

    What would be the right way to save the claims I get from SSO so that I can access them later in the application as these are required for me?

    I would appreciate any type of help, thank you!!

  • Alex J. 4 posts 114 karma points
    Jan 19, 2023 @ 14:26
    Alex J.
    100

    I solved this issue by using

    StaticServiceProvider.Instance.GetService<IExternalLoginWithKeyService>()
    

    inside the OnExternalLogin callback after auto-linking. This allowed me to get the service inside the static method which does not allow injection.

    I then used the service and it's Save method to save the claims inside UserData field of the ExternalLogin table inside the database

    Hope this helps anyone if you ever run into the issue

Please Sign in or register to post replies

Write your reply to:

Draft