C
C#•2y ago
Davaaron

Map complex object (dictionary) from appsettings.json to model

Hi, i have a complex appsettings.json like this
"interceptor": {
"interactionType": "redirect",
"protectedResourceMap": [
["https://graph.microsoft.com/v1.0/me", ["user.read"]]
]
}
"interceptor": {
"interactionType": "redirect",
"protectedResourceMap": [
["https://graph.microsoft.com/v1.0/me", ["user.read"]]
]
}
How can I map this? I have a class definition
public class MSAL
{
public MSALInterceptorConfig Interceptor { get; set; }
}

public class MSALInterceptorConfig
{
public string? InteractionType { get; set; }
public List<Dictionary<string, List<string>>>? ProtectedResourceMap { get; set; }
}
public class MSAL
{
public MSALInterceptorConfig Interceptor { get; set; }
}

public class MSALInterceptorConfig
{
public string? InteractionType { get; set; }
public List<Dictionary<string, List<string>>>? ProtectedResourceMap { get; set; }
}
and read the config like
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
var clientConfig = config.Get<MSAL>();
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
var clientConfig = config.Get<MSAL>();
When I check the clientConfig object, I see that the ProtectedResourceMap has one entry: key="1" and value="user.read". How can I fix it so I have a list of dictionary entries?
4 Replies
Angius
Angius•2y ago
Well, it's not a dictionary in your JSON So kinda hard to parse it into something it is not
[
["https://graph.microsoft.com/v1.0/me", ["user.read"]]
]
}
[
["https://graph.microsoft.com/v1.0/me", ["user.read"]]
]
}
is an array of arrays of strings-or-arrays So List<List<object>> would be the closest
Davaaron
DavaaronOP•2y ago
Okay, got. That's way too complicated, I guess will leave Microsofts structure there 😄 Shouldn't this work?
"msal-interceptor": {
"interactionType": "popup",
"protectedResourceMap": {
"https://graph.microsoft.com/v1.0/me": [ "user.read" ]
}
}
"msal-interceptor": {
"interactionType": "popup",
"protectedResourceMap": {
"https://graph.microsoft.com/v1.0/me": [ "user.read" ]
}
}
public class MSALInterceptorConfig
{
public string? InteractionType { get; set; }
public Dictionary<string, List<string>>? ProtectedResourceMap { get; set; }
}
public class MSALInterceptorConfig
{
public string? InteractionType { get; set; }
public Dictionary<string, List<string>>? ProtectedResourceMap { get; set; }
}
The key is now "http" and value is empty
Angius
Angius•2y ago
This should work, yeah Not sure why it wouldn't
Davaaron
DavaaronOP•2y ago
Couldnt get this to work. Changed it completely now 😄
"msal-interceptor": {
"interactionType": "popup",
"protectedResourceMap": [
{
"resource": "https://graph.microsoft.com/v1.0/me",
"permissions": [ "user.read" ]
}
]
}
"msal-interceptor": {
"interactionType": "popup",
"protectedResourceMap": [
{
"resource": "https://graph.microsoft.com/v1.0/me",
"permissions": [ "user.read" ]
}
]
}
public class MSALInterceptorConfig
{
public string? InteractionType { get; set; }

public List<MSALProtectedResourceMap> ProtectedResourceMap { get; set; }
}

public class MSALProtectedResourceMap
{
public string? Resource { get; set; }
public List<string> Permissions { get; set; }
}
public class MSALInterceptorConfig
{
public string? InteractionType { get; set; }

public List<MSALProtectedResourceMap> ProtectedResourceMap { get; set; }
}

public class MSALProtectedResourceMap
{
public string? Resource { get; set; }
public List<string> Permissions { get; set; }
}
Want results from more Discord servers?
Add your server