How to compile sample API code written in C Sharp

Added by Gaurav Joshua Vaz , last edited by Vishal Ganeriwala on Feb 24, 2008  (view change)
Tags: 

Basic instructions for compiling applications written in C# which contain the Citrix NetScaler API.

Requirements

To compile C# programs that contain the Citrix NetScaler API, you need the .Net Framework 1.0 or later.

Instruction to compile samples

Instructions to create Client application using the Citrix NetScaler WSDL (NSConfig.wsdl)

1. Invoke the .Net Framework Command prompt. 
2. Create the client service class from wsdl by using wsdl utility .

C:\<c # sample path>>wsdl NSConfig.wsdl

		Microsoft (R) Web Services Description Language Utility

		[Microsoft (R) .NET Framework, Version 1.0.3705.0]

		Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

        Writing file '<c# sample path>\NSConfigService.cs'.

This will generate the client class to create the client class.

Compiliing and Executing the Client application

  1. Run the batch file 'build.bat'
  2. The Client application is compiled with stub sources and generate the executables, set.exe, get.exe, and rm.exe.
  3. Execute the NSConfig to configure the NetScaler box remotely.

Usage : setConfig <protocol>://<netscaler ip address> <username> <password>
Usage : getConfig <protocol>://<netscaler ip address> <username> <password>
Usage : rmConfig <protocol>://<netscaler ip address> <username> <password>

Cookie Handling

NetScaler uses the cookies  for client authentication purposes. C# client needs to handle the cookies.
Please add the following code in client code to handle the cookie.

/*
Inherit the generated class from wsdl NSConfigServer and override the
method GetWebRequest and GetWebResponse method.*/

        public class ClientService : NSConfigService {

        	private static string
        	cookie = null;

        	/*override the getWebRequest to send cookie */

        	protected override System.Net.WebRequest GetWebRequest(Uri uri) {

                System.Net.HttpWebRequest req = (System.Net.HttpWebRequest) base.GetWebRequest(uri);

                     if (cookie != null) {

                        req.Headers.Add("Set-Cookie", cookie);

                        }

        return req;
	}

/* override the getWebResponse to get the cookie */
        protected override System.Net.WebResponse GetWebResponse(System.Net.WebRequest req) {

        	System.Net.HttpWebResponse
        	rep = (System.Net.HttpWebResponse) base.GetWebResponse(req);

                 if (rep.Headers["Set-Cookie"] != null) {

                 	cookie = rep.Headers["Set-Cookie"];
		}

                 return rep;

          }

        }

 HTTPS Support

You should use the HTTPS protocol to send the secured API request to the Citrix NetScaler. 

The Citrix NetScaler, by default, uses a self signed CA certificate. Therefore, all C# clients need to handle the self signed certificate if an HTTPS request is sent to the Citrix NetScaler.  Please follow the given instruction to use the https protocol in API requests.

The .NET Framework uses the System.NET.ICertificatePolicy interface to provide custom security certificate validation for an application.  This can be overridden using the following code snippet

using System;
using System.Net;
using System.Security;
using System.Security.Cryptography.X509Certificates;

namespace NSConfig
{
  public class trustedCertificatePolicy : System.Net.ICertificatePolicy
  {
    public trustedCertificatePolicy() {}
    public bool CheckValidationResult
    (
      System.Net.ServicePoint sp,
      System.Security.Cryptography.X509Certificates.X509Certificate certificate,
      System.Net.WebRequest request, int problem)
    {
      return true;
    }
  }
}

In  application, the default Certificate Policy needs to be overridden with the new trustedCertificatePolicy class.

class NSConfig
{
  static void Main(string[] args)
  {
    System.Net.ServicePointManager.CertificatePolicy = new trustedCertificatePolicy();
    ...
  }
};

Now, all SSL based communications in this application will follow the new security policy of allowing all server certificates.

usage : setconfig  https:// <NS IP> <username> <password>
            g{_}etconfig  https:// <NS IP> <username> <password>_
_           rmconfig  https:// <NS IP> <username> <password>_

More Information

User Rating?

Can you also give a full example of how to use NetScaler API from C# for configuring vservers, etc.

  • Add to Bookmarks