NewProgrammer1500
NewProgrammer1500

Reputation: 1

SCIM post of user cant be mapped properly

I am new to SCIM and trying to create an API where we can fetch users from our customers using azure ad.

Right now I am trying to test up against the SCIM validator provided by Microsoft https://scimvalidator.microsoft.com/ and I am getting an error regarding not being able to map my user object correctly.

This is my current user object:

{Core2UserBase} {Schemas: Count = 1}
 Schemas = {ReadOnlyCollection<string>} Count = 1
 Active = {bool} false
 Addresses = {IEnumerable<Address>} null
 CustomExtension = {ReadOnlyDictionary<string, IDictionary<string, object>>} Count = 0
 DisplayName = {string} "RPDPROADZNXI"
 ElectronicMailAddresses = {IEnumerable<ElectronicMailAddress>} null
 ExternalIdentifier = {string} "47e3a5a7-0925-431c-ad72-3e8bb87d9061"
 Identifier = {string} "0b2fd94a-87b7-4405-bb53-0aaf24a3fcc4"
 InstantMessagings = {IEnumerable<InstantMessaging>} null
 Locale = {string} null
 Metadata = {Core2Metadata} {Created: {06-05-2024 09:17:28}}
  Created = {DateTime} 06-05-2024 09:17:28
   Date = {DateTime} 06-05-2024 00:00:00
   Day = {int} 6
   DayOfWeek = {DayOfWeek} Monday
   DayOfYear = {int} 127
   Hour = {int} 9
   InternalKind = {ulong} 0
   Kind = {DateTimeKind} Unspecified
   Microsecond = {int} 159
   Millisecond = {int} 509
   Minute = {int} 17
   Month = {int} 5
   Nanosecond = {int} 700
   Second = {int} 28
   Ticks = {long} 638505838485091597
   TimeOfDay = {TimeSpan} 09:17:28.5091597
   UTicks = {ulong} 638505838485091597
   Year = {int} 2024
   _dateData = {ulong} 638505838485091597
  LastModified = {DateTime} 06-05-2024 09:17:28
  Location = {string} "DefaultLocation"
  ResourceType = {string} "User"
  Version = {string} "1"
 Name = Name
  FamilyName = {string} "Betty"
  Formatted = {string} "Fernando"
  GivenName = {string} "Crystel"
  HonorificPrefix = {string} "Adrien"
  HonorificSuffix = {string} "Cloyd"
 Nickname = {string} null
 PhoneNumbers = {IEnumerable<PhoneNumber>} null
 PreferredLanguage = {string} null
 Roles = {List<Role>} Count = 1
 TimeZone = {string} null
 Title = {string} null
 UserName = {string} "[email protected]"
 UserType = {string} null
 customExtension = {Dictionary<string, IDictionary<string, object>>} Count = 0
 schemas = {List<string>} Count = 1
 schemasWrapper = {ReadOnlyCollection<string>} Count = 1
 serializer = {IJsonSerializable} null
 thisLock = object

My User controller's post method:

´´´´ [HttpPost("")] public virtual async Task<ActionResult> Post([FromBody]Core2UserBase resource) { try { HttpRequestMessage request = ConvertRequest(); string correlationIdentifier = GetCorrelationIdentifier(request); IProviderAdapter provider = AdaptProvider();

        bool userExists = await provider.CheckUserExists(resource.Identifier, resource.ExternalIdentifier);
        if (userExists)
        {
            _logger.LogWarning($"Attempt to create duplicate user: {resource.UserName}");
            return Conflict("A user with the same identifier already exists.");
        }

        Resource result = await provider.Create(
            request,
            resource,
            correlationIdentifier
        ).ConfigureAwait(false);
        
        Uri baseResourceIdentifier = new Uri(Request.Scheme + "://" + Request.Host);
        Uri resourceIdentifier = new Uri(baseResourceIdentifier, $"/api/resource/{result.Identifier}"); 
        Response.Headers.Add("Location", resourceIdentifier.ToString());
        Response.StatusCode = (int)HttpStatusCode.Created;
        
        _logger.LogInformation($"Resourceidentifier: {resourceIdentifier}, Result: {result} identifier: {result.Identifier},user: {result}");
        
        _logger.LogInformation($"serialized result: {result.Serialize()}");
        return Created(resourceIdentifier.ToString(), result.Serialize());
    }
    catch (Exception ex)
    {
        _logger.LogError($"Error creating user: {ex.Message}", ex);
        return StatusCode(500, "Internal server error while creating user.");
    }
}

And my adapater class's Create method:

public Task<Resource> Create(HttpRequestMessage request, Resource resource, string correlationIdentifier)
    {
        if (resource == null)
            throw new ArgumentNullException(nameof(resource));

        var coreUser = resource as Core2UserBase;
        if (coreUser == null)
            throw new InvalidCastException("The provided resource cannot be cast to Core2UserBase.");
        
        if (string.IsNullOrWhiteSpace(coreUser.UserName))
            throw new ArgumentException("UserName is required.");
        
        string uniqueIdentifier = coreUser.Identifier ?? Guid.NewGuid().ToString();

        var user = new Core2UserBase
        {
            DisplayName = coreUser.DisplayName ?? "DefaultDisplayName",
            Identifier = uniqueIdentifier,  
            UserName = coreUser.UserName ?? "DefaultUserName",
            ExternalIdentifier = coreUser.ExternalIdentifier,
            InstantMessagings = coreUser.InstantMessagings,  
            Metadata = new Core2Metadata
            {
                ResourceType = "User",
                Created = DateTimeOffset.Now.DateTime,
                LastModified = DateTimeOffset.Now.DateTime,
                Location = coreUser.Metadata?.Location ?? "DefaultLocation",
                Version = "1"
            },
            Name = coreUser.Name ?? new Name(), 
            Roles = coreUser.Roles ?? new List<Role>()  
        };

        var response = new UserResponse
        {
            Resources = new List<Core2UserBase> { user },
            TotalResults = 1,
            ItemsPerPage = 1,
            StartIndex = 1
        };

        return Task.FromResult(response.Resources.First() as Resource);
    }
    ```

 

Upvotes: 0

Views: 61

Answers (0)

Related Questions