Vai al contenuto principale

Aggiunta di un nuovo utente

Il codice C# seguente crea una connessione autenticata e quindi aggiunge un nuovo utente al database del repository di Qlik NPrinting Server. Ricordare di sostituire server.name.com con il nome effettivo di Qlik NPrinting Server.

Nota informaticaIl metodo di serializzazione JSON nell'esempio seguente utilizza una libreria di terze parti da Newtonsoft per convertire un oggetto in 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; } }