Fixing NTLM authentication in Windows 2003

We had an application written in C# .Net, which used to communicate with Alfresco Enterprise Document/Content Management System. The application was using Alfresco’s NTLM component for authenticating users against their AD (Active Directory) user account.

The application worked perfectly while we were testing it on Windows XP system. However, on Windows 2003 64 Bit system, the application started failing as it could not authenticate users on alfresco server using NTLM.

So we had a situation where NTLM authentication used to fail only when our application was running on Windows 2003 Operating System. We tried disabling “Internet Explorer Enhanced Security Configuration”, but it didn’t help. Finally, after spending a lot of time on Google, we came across following article on Microsoft Knowledge Base:

You may experience authentication issues when you access a network-attached storage device after you upgrade to Windows Server 2008, to Windows Vista, to Windows Server 2003, or to Windows XP

This article talks about configuring the system to use appropriate NTLM version. In our Windows 2003 system, the value of “lmcompatibilitylevel” (Under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\LSA subkey) was set to 2. We just changed this value to 1, and the client application started working properly in Windows 2003 system as well.

If you are also facing a similar issue on Windows 2003, then you can try the solution mentioned here. If setting the value of “lmcompatibilitylevel” to 1 makes no difference, then change this value to 0, which is the default value of “lmcompatibilitylevel” in Windows XP.

Be careful while using ‘replaceAll’ and ‘replaceFirst’ methods of String class in Java

Most of the time, while trying to replace a substring in a given string, either all its occurrences or just the first one, java programmers tend to use ‘replaceAll’ and ‘replaceFirst’ methods provided by String class in Java. Java programmers like to use these methods to replace substrings as compared to ‘replace’ method of String class because of different reasons like:

  1. ‘replaceAll’ and ‘replaceFirst’ methods use regular expressions instead of character sequence. It is pre-assumed most of the time that using these methods will allow us to replace a given pattern in future, instead of just replacing a substring.
  2. ‘replaceAll’ and ‘replaceFirst’ methods are named such that they clarify the intent of their execution.

However, while using these methods, we need to be very careful while providing the string value which would replace a given substring or a pattern. Let’s understand why with the following example:

Run the following java code:

public class StringReplace {

public static void main(String[] args){
String stringValue = "test1 test2 test1 test3";
String replaceValue = "test";
stringValue = stringValue.replaceAll("test1", replaceValue);
System.out.print(stringValue);
}

}

This code works properly. But now try running the same code by changing the value of variable ‘replaceValue’ as:

String replaceValue = "$100";

This would result in an exception of type ‘IndexOutOfBoundException’. This is because the ‘$’ symbol is used to identify a group from the regular expression which is the first parameter of ‘replaceAll’ or ‘replaceFirst’ method. We can solve this problem by:

  1. Using ‘replace’ method: This would be the good choice if you want to replace a string literal and not a pattern.
  2. Escaping ‘$’ symbol: If you need to a use regular expression, and your pattern has no groups identified, then you can escape any group identification symbols from your replace string as shown below:

String replaceValue = java.util.regex.Matcher.quoteReplacement("$100");