msgraph-beta-sdk-dotnet icon indicating copy to clipboard operation
msgraph-beta-sdk-dotnet copied to clipboard

Unable to create TrustFrameworkPolicy

Open arievansomeren opened this issue 2 years ago • 1 comments

I'm trying to manage my Azure B2C policies with the graph API using the beta sdk but i'm unable to create a TrustFrameworkPolicy from scratch. Using SDK v4 4.74.0-preview

var policyResult = await graphClient.TrustFramework.Policies.Request().AddAsync(policy);

The above line errors with "The policy being uploaded is not XML or is not correctly formatted: Data at the root level is invalid. Line 1, position 1."

This is weird because there is no XML involved. There's no way to specifiy the TrustFrameworkPolicyContent at this stage. According to the docs the Content-Type should be set to "application/xml" for a "create" request.

I tried the following :

var policiesRequestBuilder = graphClient.TrustFramework.Policies;                   
var policiesUrl = policiesRequestBuilder.RequestUrl;
var policiesBuilder = new TrustFrameworkPolicyRequestBuilder(policiesUrl, graphClient);
var policyRequest = policiesBuilder.Request();
policyRequest.ContentType = "application/xml";
var policyResult = await policyRequest.CreateAsync(policy);

var policyContentRequestBuilder = graphClient.TrustFramework.Policies[policyResult.Id];
var policyUrl = policyContentRequestBuilder.AppendSegmentToRequestUrl("$value");
var policyContentBuilder = new TrustFrameworkPolicyContentRequestBuilder(policyUrl, graphClient);
var policyContentResult = await policyContentBuilder.Request().PutAsync(stream);

Still the same error occurs.

Is this a bug or am I using the functionality the wrong way...

UPDATE:

I upgraded to 5.49.0 and i still get the same error.

TrustFrameworkPolicy policy = new()
{
    Id ="TEST"
};

var policyResult = await graphClient.TrustFramework.Policies.PostAsync(policy, requestConfiguration =>
{
    requestConfiguration.Headers.Add("content-type", "application/xml");
});

arievansomeren avatar Sep 27 '23 12:09 arievansomeren

Thanks for raising this @arievansomeren

It looks like the metadata used to generate the SDK for this endpoint is incorrect, causing the SDK to build json payloads rather than build an xml payload. Are you able to confirm if you are able to make a successful request on the Graph Explorer?

Using V5 of the SDK, you may have to do something like close to this to convert the xml to json and send it out

TrustFrameworkPolicy policy = new()
{
    Id ="TEST"
};
var requestInformation = graphClient.TrustFramework.Policies.ToPostRequestInformation(policy);

//convert the json to xml
var jsonString = await new StreamReader(requestInformation.Content).ReadToEndAsync();
var xmlDocument = JsonConvert.DeserializeXmlNode(jsonString, "TrustFrameworkPolicy");
var outputStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlDocument.OuterXml));

//set the xml stream content
requestInformation.SetStreamContent(outputStream);
requestInformation.Headers.Add("Content-Type", "application/xml");
var errorMapping = new Dictionary<string, ParsableFactory<IParsable>> {
    {"4XX", ODataError.CreateFromDiscriminatorValue},
    {"5XX", ODataError.CreateFromDiscriminatorValue},
};
await graphClient.RequestAdapter.SendNoContentAsync(requestInformation,errorMapping);

andrueastman avatar Oct 12 '23 12:10 andrueastman