ESCAPING SPECIAL CHARS IN LDAP SEARCH FILTERS

When programming against any LDAP backend, it’s good to sanitize any user input that may go into a search filter. A typical case is authentication (login or single sing-on) applications, where an input username or email must be used to resolve a user’s distinct name (DN) in the LDAP directory. Continue reading “ESCAPING SPECIAL CHARS IN LDAP SEARCH FILTERS”

Create an Active Directory Parser based on Organisational Units (OUs)

LDAP queries look like this:

("CN=Dev-UK,OU=Distribution Groups,DC=gp,DC=gl,DC=google,DC=com");

What it means:

  • CN = Common Name
  • OU = Organizational Unit
  • DC = Domain Component

These are all parts of the X.500 Directory Specification, which defines nodes in a LDAP directory.

Each = pair is a search criteria.

With the query

("CN=Dev-UK,OU=Distribution Groups,DC=gp,DC=gl,DC=google,DC=com");

In effect the query is:

From the com Domain Component, find the google Domain Component, and then inside it the glDomain Component and then inside it the gp Domain Component.

In the gp Domain Component, find the Organizational Unit called Distribution Groups and then find the the object that has a common name of Dev-UK.

In order to list all users in this OU, you can write the following function:

public ArrayList EnumerateOU(string domainController, string OuDn, string username, string password)
 {
 ArrayList alObjects = new ArrayList();
 try
 {
 DirectoryEntry directoryObject = new DirectoryEntry("LDAP://" + domainController + "/" + OuDn);
 directoryObject.Username = username;
 directoryObject.Password = password;
 directoryObject.AuthenticationType = AuthenticationTypes.Secure;
 
 foreach (DirectoryEntry child in directoryObject.Children)
 {
 string childPath = child.Path.ToString();
 alObjects.Add(childPath.Remove(0, 7));
 //remove the LDAP prefix from the path
child.Close();
 child.Dispose();
 }
 directoryObject.Close();
 directoryObject.Dispose();
 }
 catch (DirectoryServicesCOMException e)
 {
 Console.WriteLine("An Error Occurred: " + e.Message.ToString());
 }
 return alObjects;
 }

Allow Swagger to return Content Type XML or JSON

At the moment only application/json is available. What is required to add application/xml as option to the selectbox?

Response

The most annoying thing is that two “Response Content Type” dropdowns appear in swagger-ui, one at the top of the operation (above the parameters) and one embedded within my Message Body parameter area (which is redundant and seems to be completely ignored).

For API calls with no Message Body parameter (i.e. a GET as opposed to PUT, POST) the UI works great and I have no complaints.

{
"swaggerVersion": "2.0",
"resourcePath": "/document_list",
"apis": [
{
"path": "/document_list",
"operations": [
{
"httpMethod": "GET",
"summary": "Search for documents based on a set of criteria",
"produces": [
"application/json",
"application/xml",
"application/atom+xml"
],

In App_Start>SwaggerConfig.cs

// Similar to Schema filters, Swashbuckle also supports Operation and Document filters:
//
// Post-modify Operation descriptions once they’ve been generated by wiring up one or more
// Operation filters.
//
c.OperationFilter();

Swagger XML and JSON options

 
public class CustomResponseType : IOperationFilter
        {
            public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
            {
                if (operation.produces.Contains("text/json"))
                {
                    operation.produces.Remove("text/json");
                }
                if (!operation.produces.Contains("application/json"))
                {
                    operation.produces.Add("application/json");
                }
                if (!operation.produces.Contains("application/xml"))
                {
                    operation.produces.Add("application/xml");
                }
              
            }
        }

Using ASMX webservices with HttpWebRequest in c# 4.0

If you want to call a .NET 4.0 C# web service, without using the WSDL or “Add Service Reference” in Microsoft Visual Studio 2015, you can use the following functions written in c#:

/// <summary>
/// Execute a Soap WebService call
/// </summary>
public override void Execute()
{
HttpWebRequest request = CreateWebRequest();
XmlDocument soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<soap:Body>
<HelloWorld3 xmlns=""http://tempuri.org/"">
<parameter1>test</parameter1>
<parameter2>23</parameter2>
<parameter3>test</parameter3>
</HelloWorld3>
</soap:Body>
</soap:Envelope>");
using (Stream stream = request.GetRequestStream()) 
{ 
soapEnvelopeXml.Save(stream); 
}
using (WebResponse response = request.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream())) 
{ 
string soapResult = rd.ReadToEnd();
Console.WriteLine(soapResult);
} 
}
}
/// <summary>
/// Create a soap webrequest to [Url]
/// </summary>
/// <returns></returns>
public HttpWebRequest CreateWebRequest()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://dev.nl/Rvl.Demo.TestWcfServiceApplication/SoapWebService.asmx"); 
webRequest.Headers.Add(@"SOAP:Action"); 
webRequest.ContentType = "text/xml;charset=\"utf-8\""; 
webRequest.Accept = "text/xml"; 
webRequest.Method = "POST"; 
return webRequest; 
}

Result

<?xml version=”1.0″ encoding=”utf-8″?><soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema”><soap:Body><HelloWorld3Response xmlns=”http://tempuri.org/”><HelloWorld3Result>test</HelloWorld3Result></HelloWorld3Response></soap:Body></soap:Envelope>

 

You can use complex types in you’re request. I use fiddler to get the contents of the soap envelope.

Windows Azure – How to migrate data from SQL Server to a Storage account

Starting from this great article https://azure.microsoft.com/en-gb/documentation/articles/storage-dotnet-how-to-use-tables/ we started by including the required imports:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
using System.Configuration;
using System.Data.SqlClient;

Continue reading “Windows Azure – How to migrate data from SQL Server to a Storage account”

Windows Azure storage tables – How to delete a table in C#

Namespace declarations

Add the following code namespace declarations to the top of any C# file in which you wish to programmatically access Azure Storage.

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;

 try
            {
                // Retrieve the storage account from the connection string.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                        ConfigurationManager.AppSettings["StorageConnectionString"]);

                // Create the table client.
                CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

                // Create the table if it doesn't exist.
                //Table names must be unique within an account.
                //Table names may contain only alphanumeric characters.
                //Table names cannot begin with a numeric character.
                //Table names are case-insensitive.
                //Table names must be from 3 to 63 characters long.
                //Some table names are reserved, including "tables". Attempting to create a table with a reserved table name returns error code 404 (Bad Request)..
                string strTableName = txtClientDB.Text.Trim().Replace("_", "") + "erp";
                strTableName = strTableName.ToLower();
                CloudTable table = tableClient.GetTableReference(strTableName);
                // Delete the table it if exists.
                table.DeleteIfExists();
                lblSuccess.Text = "Delete succeeded";
                lblSuccess.Visible = true;
            }
            catch (Exception ex)
            {
                lblError.Visible = true;
                lblError.Text = ex.Message;
            }

https://azure.microsoft.com/en-gb/documentation/articles/storage-dotnet-how-to-use-tables/