Adding a new user

The following C# code creates an authenticated connection and then adds a new user to the Qlik NPrinting Server Repository database. Remember to substitute with your actual Qlik NPrinting Server name.

Note: The JSON serialization method in this example uses a third-party library from Newtonsoft to convert an object to JSON.

static void Main(string[] args) { //Create the HTTP Request (authenticate) and add required headers ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@""); //Assign custom SSL certificate validation method if certificate is untrusted //request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; CookieContainer cookies = new CookieContainer(); request.CookieContainer = cookies; request.Method = "GET"; request.UserAgent = "Windows"; request.Accept = "application/json"; //Specify to run as the current Microsoft Windows user request.UseDefaultCredentials = true; try { // make the web request and return the content HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader responseReader = new StreamReader(response.GetResponseStream()); string sResponseHTML = responseReader.ReadToEnd(); Console.WriteLine(sResponseHTML); } catch (Exception ex) { Console.WriteLine(ex.Message); } //Create second HTTP request to add a new user and required headers HttpWebRequest secondRequest = (HttpWebRequest)WebRequest.Create(@""); //Assign custom SSL certificate validation method if certificate is untrusted //secondRequest.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; //Add the XSRF token secondRequest.Headers.Add("X-XSRF-TOKEN", cookies.GetCookies(request.RequestUri)["NPWEBCONSOLE_XSRF-TOKEN"].Value); secondRequest.CookieContainer = cookies; secondRequest.Method = "POST"; secondRequest.UserAgent = "Windows"; secondRequest.Accept = "application/json"; secondRequest.ContentType = "application/json"; //Specify to run as the current Microsoft Windows user secondRequest.UseDefaultCredentials = true; //Prepare JSON object to send to the remote server JsonUser user = new JsonUser(); user.Email = ""; user.Enabled = "true"; user.UserName = "NewUser"; user.DomainAccount = "domain\\user"; user.Timezone = "CET"; user.Locale = "En"; user.Folder = "NewUser"; user.SubFolder = "NewUser"; user.Password = "Password123"; string jUserString = JsonConvert.SerializeObject(user); using (var streamWriter = new StreamWriter(secondRequest.GetRequestStream())) { streamWriter.Write(jUserString); streamWriter.Flush(); streamWriter.Close(); } try { HttpWebResponse response2 = (HttpWebResponse)secondRequest.GetResponse(); StreamReader responseReader2 = new StreamReader(response2.GetResponseStream()); string sResponseHTML2 = responseReader2.ReadToEnd(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } public class JsonUser { public string Email { get; set; } public string Enabled { get; set; } public string UserName { get; set; } public string DomainAccount { get; set; } public string Timezone { get; set; } public string Locale { get; set; } public string Folder { get; set; } public string SubFolder { get; set; } public string Password { get; set; } }