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

    Unable to create a registration using the .NET core client

    Scheduled Pinned Locked Moved
    Q&A
    dotnet client-library user-api
    2
    4
    11.0k
    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.
    • danD
      dan
      last edited by

      Hiya,

      I'm unable to create a user registration using the .NET client libraries: https://fusionauth.io/docs/v1/tech/client-libraries/netcore

      I have verified that the API key is basically a super user. I've verified that I'm sending the registration object. I've tried twiddling different properties (verified, insertInstant) and made sure that the application exists. I've added the a user registration to the application manually and it works. Creating a user and setting the userdata works just fine. It just seems like the registration isn't working.

      I looked in https://github.com/FusionAuth/fusionauth-netcore-client/issues and https://github.com/FusionAuth/fusionauth-issues/issues but didn't see any relevant issues.

      Here's my code so far (you can run it with fusionauth_api_key=<key> dotnet.exe run -- foo@foo5.com bluepass123 blue)

      $ cat usermanager.csproj
      <Project Sdk="Microsoft.NET.Sdk">
      
        <PropertyGroup>
          <OutputType>Exe</OutputType>
          <TargetFramework>netcoreapp3.1</TargetFramework>
        </PropertyGroup>
      
        <ItemGroup>
          <PackageReference Include="FusionAuth.Client" Version="1.15.7" />
          <PackageReference Include="JSON.Net" Version="1.0.18" />
        </ItemGroup>
      
      </Project>
      
      $ cat Program.cs
      using System;
      using io.fusionauth;
      using io.fusionauth.domain;
      using io.fusionauth.domain.api;
      using System.Collections.Generic;
      using Newtonsoft.Json;
      
      namespace usermanager
      {
          class Program
          {
              private static readonly string apiKey = Environment.GetEnvironmentVariable("fusionauth_api_key");
              private static readonly string fusionauthURL = "http://localhost:9011";
      
              private static readonly string tenantId = "66636432-3932-3836-6630-656464383862";
              static void Main(string[] args)
              {
                  if (args.Length != 3) {
                      Console.WriteLine("Please provide email, password and favorite color.");
                      Environment.Exit(1);
                  }
                  string email= args[0];
                  string password = args[1];
                  string favoriteColor = args[2];
      
                  FusionAuthSyncClient client = new FusionAuthSyncClient(apiKey, fusionauthURL, tenantId);
                  User userToCreate = new User();
                  userToCreate.email = email;
                  userToCreate.password = password;
                  Dictionary<string, object> data = new Dictionary<string, object>();
                  data.Add("favoriteColor", favoriteColor);
                  userToCreate.data = data;
                  UserRegistration registration = new UserRegistration();
                  registration.applicationId = Guid.Parse("4243b56f-0b45-4882-aa23-ac75eea22d22");
                  registration.verified = true;
      
                  registration.insertInstant = DateTimeOffset.UtcNow;
                  var registrations = new List<UserRegistration>();
                  registrations.Add(registration);
      
                  userToCreate.registrations = registrations;
      
                  UserRequest userRequest = new UserRequest();
                  userRequest.sendSetPasswordEmail = false;
                  userRequest.user = userToCreate;
                  string u = JsonConvert.SerializeObject(userRequest);
                  Console.WriteLine(u);
                  var response = client.CreateUser(null, userRequest);
                  string json = JsonConvert.SerializeObject(response);
                  Console.WriteLine(json);
      
                  if (response.WasSuccessful())
                  {
                      var user = response.successResponse.user;
                      Console.WriteLine("retrieved user with email: "+user.email);
                  }
                  else if (response.statusCode != 200)
                  {
                      var statusCode = response.statusCode;
                      Console.WriteLine("failed with status "+statusCode);
                  }
              }
          }
      }
      

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

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

        Here's the JSON output of the response:

        {"statusCode":200,"successResponse":{"user":{"active":true,"data":{"favoriteColor":"blue"},"email":"foo@foo6.com","insertInstant":1587579258792,"tenantId":"66636432-3932-3836-6630-656464383862","twoFactorDelivery":"None","twoFactorEnabled":false,"usernameStatus":"ACTIVE","breachedPasswordStatus":"None","id":"ca0954d0-28e5-4b76-9260-0cca0893cba6","passwordChangeReason":"Administrative","passwordChangeRequired":false,"passwordLastUpdateInstant":1587579258834,"verified":true}}}
        

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

        1 Reply Last reply Reply Quote 0
        • robotdanR
          robotdan
          last edited by

          The User API will not create a registration, the registration portion of your request object is being ignored.

          If you review the User API, you will notice that the registration is not part of the request body.
          https://fusionauth.io/docs/v1/tech/apis/users#create-a-user

          You can optionally create the user and then create a registration separately.
          https://fusionauth.io/docs/v1/tech/apis/users#create-a-user
          https://fusionauth.io/docs/v1/tech/apis/registrations#create-a-user-registration-for-an-existing-user

          We do offer a combo API that allows you to create a User and a User Registration in one go. To use this strategy, use the Combo version of the User Registration API.

          https://fusionauth.io/docs/v1/tech/apis/registrations#create-a-user-and-registration-combined

          Thanks!

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

            @robotdan Ah, I missed that. I was looking at the response section, which had the registration. Thanks!

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

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