How to Pre-Fill Username from QueryString

Someone in the NetScaler forum asked if it is possible to pre-fill the username from the QueryString. They were being redirected to the AAA server authentication page (tmindex.html) with something like ?username=CitrixUser as part of the QueryString. They would like to use it to pre-fill the user name as follows:

Prefill Username.png

Luckily, this is easily accomplished with a bit of JavaScript. Using your favorite SFTP client (one such client is WinSCP - winscp.net), open up /netscaler/ns_gui/vpn/tmindex.html (after making a backup copy, of course!), and insert the highlighted lines right before the call to $(window).load(). Then add the call to fillName() when the page loads (see below).

parseQS.png

Let's break it down into two convenient functions.

The first function, parseQS(key), accepts a key value to look for in a querystring (QS). In our case, this would be "username". We retrieve the QS, and if there is none, we simply return an empty string. We then split the QS into key/value pairs. Cycling through each pair, we match each key against the search key passed to the function, changing each to lower case which makes the comparison case-insensitive. If we find the search key, we return the value portion of the pair. If not found, we return an empty value.

function parseQS( key ) {
var queries, pair, i, l;
var queryString = window.location.search+"";

// is there a QS?
if (queryString == "")
return "";

queryString = queryString.substring(1);

// Split into key/value pairs
queries = queryString.split('&');

// look for the key
for ( i = 0, l = queries.length; i < l; i++ ) {
pair = queries[i].split('=');
if (pair[0].toLowerCase() == key.toLowerCase())
return (pair[1]);
}

return "";
}

The second function, fillname(nameValue), takes the value returned by parseQS() as a parameter. It gets a link to the NetScaler username field, which happens to be called "login", and fills it with the passed value. If the value passed was not blank, then the focus is moved to the password field.

function fillName(nameValue) {
// parse the "username" querystring value
var name_field = document.forms["vpnForm"].elements["login"];
name_field.value = nameValue;
// position at the password field, if name was filled in
if (nameValue != "") {
document.forms["vpnForm"].elements["passwd"].focus();
}
}

Finally, add the call to fillName() in the function that's called when the page loads:

$(window).load(function(){resize(); fillName(parseQS("username")); });

That's it! A minor variation of this would be to implement a "Remember me" function. As you are about to login, store the username in a cookie on the user's browser. When the user next accesses the page, instead of the parseQS() function, create a function to read the cookie, and pass that value to the fillName function.

If you have a question, add a comment below, and then please drop me a line at sam.jacobs@ipm.com (authors are not notified when comments are added to posts).

Join me at Citrix Synergy 2016 for SYN317: NetScaler Troubleshooting and Debugging Best Practices, on Thursday, May 26th from 8:30-10:00AM PST in Bellini 2101A.

Please login to add your comments.

Recent Stories
My First Time: Citrix Machine Creation Services (MCS)

Solved: Citrix Mobile Productivity Apps, Business Contacts and Compliance with EU GDPR

Its Like Magic! The Citrix Workspace App