Beispiel #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["proxyRestUri"]))
            {
                //Create new instance, supply UserDirectory and UserId
                var req = new Ticket
                {
                    UserDirectory = "QLIK",
                    UserId        = "rikard",
                };

                //Add a list of groups (delimiter separated string or List<string>)
                req.AddGroups("Group1;Group2;Group3");

                //Add some custom attributes (delimiter separated string or List<string>)
                req.AddAttributes("Email", "*****@*****.**");
                req.AddAttributes("Country", "Sweden");
                req.AddAttributes("Phone", "+46-012-345678");

                //Perform ticket request
                req.TicketRequest();
            }
            else
            {
                Response.Write("Please don't access this Authentication Module directly. Use a virtual proxy instead!");
            }
        }
        public Stream TicketRequest(Ticket ticketRequest)
        {
            Uri url = CombineUri(ticketRequest.ProxyRestUri, EndPoint);

            //Get certificate
            LocateCertificate();

            if (certificate == null)
            {
                logger.Info("Current User Certificate not found! Verify AppPool credentials.", systemUserId);
                CertificateLocation = StoreLocation.LocalMachine;
                LocateCertificate();
            }

            if (certificate == null)
            {
                logger.Info("Local Machine Certificate not found! Verify AppPool credentials.", systemUserId);
                throw new Exception("Certificate not found! Verify AppPool credentials.");
            }

            //Create the HTTP Request and add required headers and content in Xrfkey
            string xrfkey = GenerateXrfKey();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?Xrfkey=" + xrfkey);

            // Add the method to authentication the user
            request.Method = "POST";
            request.Accept = "application/json";
            request.Headers.Add("X-Qlik-Xrfkey", xrfkey);

            request.ClientCertificates.Add(certificate);

            string body = string.Empty;

            byte[] bodyBytes;

            if (string.IsNullOrWhiteSpace(ticketRequest.TargetId))
            {
                body = "{ 'UserId':'" + ticketRequest.UserId + "','UserDirectory':'" + ticketRequest.UserDirectory + "','Attributes': [] }";
            }
            else
            {
                //  ticketRequest.Attributes = new List<Dictionary<string, string>>();
                ticketRequest.AddGroups("Users");
                body = JsonConvert.SerializeObject(ticketRequest);
            }

            if (!string.IsNullOrEmpty(body))
            {
                bodyBytes = Encoding.UTF8.GetBytes(body);

                request.ContentType   = "application/json";
                request.ContentLength = bodyBytes.Length;

                // Write Request object
                StringBuilder message = new StringBuilder();
                message.Append($"URL: {request.RequestUri}");
                message.Append($"Request Headers: {request.Headers}");
                message.Append($"Body: {body}");
                logger.Info(message.ToString(), systemUserId);

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bodyBytes, 0, bodyBytes.Length);
                requestStream.Close();
            }

            // make the web request and return the content
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return(response.GetResponseStream());
            }
            catch (WebException ex)
            {
                logger.Error($"WebException {ex}", systemUserId);
                using (var stream = ex.Response.GetResponseStream())
                    using (var reader = new StreamReader(stream))
                    {
                        string error = ex.ToString() + reader.ReadToEnd();
                        throw new Exception(error);
                    }
            }
        }