Take the community feedback survey now.

Trouble Logging in to CMS12 with Token-Based Authentication

Vote:
 

Hi,

I'm trying to log in to CMS12 using a code-based approach, but I can't get it to work.

We have a token-based solution that successfully retrieves the username with valid access. My goal is to use that username to log in to CMS12—without showing the login page—if the user's roles are correct.

From the logs, it looks like I'm authenticated, but not actually signed in to CMS.

Any ideas on what might be missing?

 

Log; 

2025-09-19 11:39:29.297 +02:00 [INF] AuthenticationScheme: Identity.Application signed in.
2025-09-19 11:39:29.297 +02:00 [INF] After SignInAsync: IsAuthenticated=True, UserName=pers, Roles=[WebAdmins, CmsAdmins, CmsEditors]

 

In startup.cs

services.AddCmsAspNetIdentity<ApplicationUser>();

 

var user = await _userManager.FindByNameAsync("pers");

  if (user != null && user.IsApproved)
  {
      var claims = new List<Claim>
      {
          new Claim(ClaimTypes.Name, value)
      };

      var roles = await _userManager.GetRolesAsync(user);
      foreach (var role in roles)
      {
          claims.Add(new Claim(ClaimTypes.Role, role));
      }

      claims.Add(new Claim(ClaimTypes.Role, "CmsAdmins"));
      claims.Add(new Claim(ClaimTypes.Role, "CmsEditors"));

      var identity = new ClaimsIdentity(claims, IdentityConstants.ApplicationScheme);
      var principal = new ClaimsPrincipal(identity);

      await httpContext.SignInAsync(IdentityConstants.ApplicationScheme, principal);      var isAuthenticated = principal.Identity?.IsAuthenticated ?? false;
      var userName = principal.Identity?.Name;
      var userRoles = principal.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value).ToList();


      _logger.LogInformation($"After SignInAsync: IsAuthenticated={isAuthenticated}, UserName={userName}, Roles=[{string.Join(", ", userRoles)}]");

  }

 

#340463
Sep 19, 2025 11:35
Vote:
 

Try this one as mentioned in my blog post, CMS 12 needs you to synchronize the user:

options.Events.OnSignedIn = async ctx =>{if (ctx.Principal?.Identity is ClaimsIdentity claimsIdentity){if (claimsIdentity.Name != null){
// Syncs user and roles so they are available in the CMS
var synchronizingUserService = ctx
                .HttpContext
                .RequestServices
                .GetRequiredService<ISynchronizingUserService>();await synchronizingUserService.SynchronizeAsync(claimsIdentity);}}
};

#340464
Edited, Sep 19, 2025 12:25
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.