Saltar al contenido principal

Añadir un nuevo usuario

El siguiente código de C# crea una conexión autenticada y después agrega un nuevo usuario a la base de datos del repositorio de Qlik NPrinting Server. Recuerde sustituir server.name.com por su nombre real de Qlik NPrinting Server.

Nota informativaEl método de serialización JSON en este ejemplo usa una biblioteca de terceros de Newtonsoft para convertir un objeto a JSON.

static void Main(string[] args) { //Create the HTTP Request (authenticate) and add required headers ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://usott-bll.qliktech.com:4993/api/v1/login/ntlm"); //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(@"https://usott-bll.qliktech.com:4993/api/v1/users"); //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 = "name@domain.com"; 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; } }