-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtickRequest.aspx
169 lines (146 loc) · 7.46 KB
/
tickRequest.aspx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<%@ Page Language="C#" AutoEventWireup="true" debug="true"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Threading.Tasks" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Security.Cryptography.X509Certificates" %>
<!--
====================================================================================
File: tickRequest.aspx
Developer: Jeff Goldberg
Created Date: 14-July-2015
Description:
tickRequest.aspx is an asp.net web page to demonstrate using the Qlik Sense Ticketing
API; a component of the Qlik Sense Proxy Service API (qps).
WARNING!:
This code is intended for testing and demonstration purposes only. It is not meant for
production environments. In addition, the code is not supported by Qlik.
This code uses bootstrap for ui enhancement. Bootstrap is delivered by urls.
If bootstrap CDN cannot be contacted, please download bootstrap (http://getbootstrap.com/)
and configure for this web page.
Change Log
Developer Change Description Modify Date
====================================================================================
Jeff Goldberg Initial Release 14-July-2015
====================================================================================
====================================================================================
-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<html>
<body>
<form runat="Server" id="MainForm" class="form-horizontal">
<div class="container">
<div class="jumbotron" style="width: 950px; margin-top: 10px;">
<H1 class="text-center" style="margin-top: -10px;">Qlik Sense Ticket Example</H1>
<div class="row">
<div style="padding-bottom: 5px;" class="form-group col-md-5">
<div>
<label for="txtServer" class="control-label">Sense Server</label>
<asp:TextBox id="txtServer" runat="server" width="250px" class="form-control">Enter Qlik Sense Server Name</asp:TextBox>
</div>
<div>
<label for="txtServer" class="control-label">Virtual Proxy</label>
<asp:TextBox id="txtVp" runat="server" width="250px" class="form-control">Enter Virtual Proxy Name</asp:TextBox>
</div>
</div>
<div style="padding-bottom: 5px;" class="form-group col-md-5">
<div>
<label for="txtUserDirectory" class="control-label">User Directory</label>
<asp:TextBox id="txtUserDirectory" runat="server" width="250px" class="form-control">Enter User Directory</asp:TextBox>
</div>
<div>
<label for="txtUser" class="control-label">User Id</label>
<asp:TextBox id="txtUser" runat="server" width="250px" class="form-control">Enter UserId</asp:TextBox>
</div>
</div>
</div>
<div class="row">
<label for="tickResponse" class="control-label">Response from Server</label>
</div>
<div class="row form-inline" style="padding-bottom: 10px;">
<asp:TextBox id="tickResponse" runat="server" class="form-control" style="width: 750px;"></asp:TextBox>
<asp:Button id="btnGo" runat="server" Text="Get Ticket" onclick="Go_Button_Click" class="btn btn-primary" style="width: 100px"></asp:Button>
</div>
<div class="row">
<label for="theTicket" class="control-label">Redirect Url</label>
</div>
<div class="row form-inline">
<asp:TextBox id="theTicket" runat="server" width="750px" class="form-control"></asp:TextBox>
<asp:Button id="btnLaunch" runat="server" Text="Use Ticket" onclick="Launch_Button_Click" class="btn btn-success" style="width: 100px"></asp:Button>
</div>
</div>
</div>
</form>
</body>
</html>
<script language="c#" runat="server">
private string TicketRequest(string method, string server, string virtualProxy, string user, string userdirectory)
{
X509Certificate2 certificateFoo =null;
// First locate the Qlik Sense certificate
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
certificateFoo = store.Certificates.Cast<X509Certificate2>().FirstOrDefault(c => c.FriendlyName == "QlikClient");
store.Close();
//The following line is required because the root certificate for the above server certificate is self-signed.
//Using a certificate from a trusted root certificate authority will allow this line to be removed.
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
//Create URL to REST endpoint for tickets
string url = "https://" + server + ":4243/qps/" + virtualProxy + "/ticket";
//Create the HTTP Request and add required headers and content in Xrfkey
string Xrfkey = "0123456789abcdef";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?Xrfkey=" + Xrfkey);
// Add the method to authentication the user
request.ClientCertificates.Add(certificateFoo);
request.Method = method;
request.Accept = "application/json";
request.Headers.Add("X-Qlik-Xrfkey", Xrfkey);
//The body message sent to the Qlik Sense Proxy api will add the session to Qlik Sense for authentication
string body = "{ 'UserId':'" + user + "','UserDirectory':'" + userdirectory +"',";
body+= "'Attributes': [],";
body+= "}";
byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
if (!string.IsNullOrEmpty(body))
{
request.ContentType = "application/json";
request.ContentLength = bodyBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bodyBytes, 0, bodyBytes.Length);
requestStream.Close();
}
// make the web request and return the content
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
return stream != null ? new StreamReader(stream).ReadToEnd() : string.Empty;
}
protected void Go_Button_Click(object sender, EventArgs e)
{
//Request a ticket
string ticketresponse= TicketRequest("POST", txtServer.Text, txtVp.Text, txtUser.Text, txtUserDirectory.Text);
tickResponse.Text = ticketresponse;
//Parse the response
string[] getTicket = ticketresponse.Split(new Char[] {','});
string[] getTicketCode = getTicket[3].Split(new Char[] {':'});
//Form the url to use the ticket
theTicket.Text = "https://" + txtServer.Text + "/" + txtVp.Text + "/Hub?qlikTicket=" + getTicketCode[1].Trim(new Char[] {'"'});
}
protected void Launch_Button_Click(object sender, EventArgs e)
{
//Redirect to the Qlik Sense server url desired
Response.Redirect(theTicket.Text);
}
</script>