FusionAuth
    • Home
    • Categories
    • Recent
    • Popular
    • Pricing
    • Contact us
    • Docs
    • Login

    DotNet issue with PatchUser

    Scheduled Pinned Locked Moved
    Q&A
    1
    6
    959
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      craig.hind
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • danD
        dan
        last edited by dan

        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.

        --
        FusionAuth - Auth for devs, built by devs.
        https://fusionauth.io

        1 Reply Last reply Reply Quote 1
        • D
          dtokarz1
          last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • joshuaJ
            joshua
            last edited by

            @dtokarz1

            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/1319

            Thanks,
            Josh

            D 1 Reply Last reply Reply Quote 0
            • D
              dtokarz1 @joshua
              last edited by dtokarz1

              @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!

              1 Reply Last reply Reply Quote 0
              • joshuaJ
                joshua
                last edited by joshua

                @dtokarz1

                Great! There is also some of this functionality built into FusionAuth that you could explore as well under Tenants > Registration > Email verification settings

                Screen Shot 2021-08-03 at 1.42.38 PM.png

                Thanks,
                Josh

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post