DotNet issue with PatchUser
-
Hi All, (pinging @Dan )
Could someone let me know what I am doing wrong here.
I'm trying to set user data using PatchUser. I'm getting a 200 back but the data is never changed.
When I use UpdateUser it works but of course strips out all non-specified data.
Here are two code samples.
This works, but as expected strips data as it is a replace.
FusionAuthSyncClient client = new FusionAuthSyncClient(Configuration["FusionAuthApiKey"], Configuration["FusionAuthAuthority"], Configuration["FusionAuthTenantId"]); Guid guid = new Guid(userID); recoveryCode = "abc"; Dictionary<string, object> data = new Dictionary<string, object>(); data.Add("recoveryCode", recoveryCode); UserRequest userRequest = new UserRequest(); userRequest.user.data = data; var response = client.UpdateUser(guid, userRequest);
This does not work, and I suspect I'm doing something wrong.
FusionAuthSyncClient client = new FusionAuthSyncClient(Configuration["FusionAuthApiKey"], Configuration["FusionAuthAuthority"], Configuration["FusionAuthTenantId"]); Guid guid = new Guid(userID); recoveryCode = "abc"; Dictionary<string, object> data = new Dictionary<string, object>(); data.Add("recoveryCode", recoveryCode); var response = client.PatchUser(guid, data);
I'm thinking it has something to do with recoveryCode being under data in the JSON hierarchy. I've tried nesting the dictionary within another dictionary, but PatchUser doesn't like that because it's expecting (Guid? userid, Dictionary<string, object> request), and not a nested dict. I've also tried nesting a UserRequest within the Dictionary, but it expectedly doesn't like that either.
However I've also tried updating a field like firstName too and that won't update either, so I'm not sure what I'm doing wrong.
Any help would be appreciated.
Thanks
Craig -
Hi @craig-hind
I was successfully able to patch a user's data field using this code:
static ClientResponse<UserResponse> patch(FusionAuthSyncClient client, String newfavColor, User userToPatch) { Dictionary<string, object> request = new Dictionary<string, object>(); Dictionary<string, object> user = new Dictionary<string, object>(); Dictionary<string, object> data = new Dictionary<string, object>(); user.Add("data", data); data.Add("favoriteColor", newfavColor); data.Add("recoveryCode", "recoveryCode"); user.Add("firstName", "D"); request.Add("user", user); // for debugging string json = JsonConvert.SerializeObject(request); Console.WriteLine(json); return client.PatchUser(userToPatch.id, request); }
Essentially, for the patch user method, we aren't using anything strongly typed, just building up a JSON array. I think the issue is that you needed to have the top level
user
key in your dictionary.I also added sample code here: https://github.com/FusionAuth/fusionauth-example-netcore
Please give that a go and let me know if it works.
-
Hi Dan,
I followed this format almost exactly and have had great success patching user information. However, trying to change one value is giving me problems. I am trying to patch user.verified using this code:
Dictionary<string, object> verified = new Dictionary<string, object> { {"verified", false} };
It comes back with a status code of 200 but it never updates the value. Any other key updates without problems. Is this value updatable or is my format wrong? I've tried using the false boolean and even "false" with no luck. Any help would be appreciated.
-
This may be a current limitation. My understanding is that there is no current way to programmatically mark the user as verified via API (and by extension, client library).
It looks like an issue is being considered.
https://github.com/FusionAuth/fusionauth-issues/issues/1319Thanks,
Josh -
@joshua Thanks for the reply. Actually I was trying to do the opposite. If a person changed their email I was going to mark "verified" as false until they clicked on the email link I send out to verify it. Being able to programmatically toggle it either way would be preferable so I could customize the flow as I see fit. Hopefully it is wanted enough that it makes it to a future version. I have already given my to the github issue. Thanks!
-
Great! There is also some of this functionality built into FusionAuth that you could explore as well under
Tenants > Registration > Email verification settings
Thanks,
Josh