Accéder au contenu principal

Ajout d'un nouvel utilisateur

Le code C# suivant permet de créer une connexion authentifiée puis d'ajouter un nouvel utilisateur à la base de données du référentiel Qlik NPrinting Server. Pensez à remplacer server.name.com par le nom réel de Qlik NPrinting Server.

Note InformationsLa méthode de sérialisation JSON employée dans cet exemple fait appel à une bibliothèque tierce de Newtonsoft pour convertir un objet au format 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://server.name.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://server.name.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; } }

Cette page vous a-t-elle aidé ?

Si vous rencontrez des problèmes sur cette page ou dans son contenu – une faute de frappe, une étape manquante ou une erreur technique – dites-nous comment nous améliorer !