跳到主要内容

新增用户

以下 C# 代码创建经验证的连接,然后将新用户添加至 Qlik NPrinting Server 储存库数据库。请记住将 server.name.com 替换为您的实际l Qlik NPrinting Server 名称。

备注: 该示例中的 JSON 序列化方法使用来自 Newtonsoft 的第三方库来将对象转换为 JSON。

static void Main(string[] args) { //创建 HTTP 请求(验证)并添加所需标头 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://server.name.com:4993/api/v1/login/ntlm"); //如果证书可信则分配自定义 SSL 证书验证方法 //request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; CookieContainer cookies = new CookieContainer(); request.CookieContainer = cookies; request.Method = "GET"; request.UserAgent = "Windows"; request.Accept = "application/json"; //进行指定以作为当前 Microsoft Windows 用户运行 request.UseDefaultCredentials = true; try { // 创建 Web 请求并返回当前 HttpWebResponse 响应 = (HttpWebResponse)request.GetResponse(); StreamReader responseReader = new StreamReader(response.GetResponseStream()); string sResponseHTML = responseReader.ReadToEnd(); Console.WriteLine(sResponseHTML); } catch (Exception ex) { Console.WriteLine(ex.Message); } //创建第二个 HTTP 请求以新增用户和所需的标头 HttpWebRequest secondRequest = (HttpWebRequest)WebRequest.Create(@"https://server.name.com:4993/api/v1/login/ntlm"); //如果证书可信则分配自定义 SSL 证书验证方法 //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"; //进行指定以作为当前 Microsoft Windows 用户运行 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; } }