NT Authentication problem

Posted on 2003-03-08
Medium Priority
Last Modified: 2010-03-05
I have a problem when getting NT logged-in user.

I am using bellow code to get NT user id. When I access the web site using "http://PCNAME/..... " or "http://localhost/...", it's working fine.

But if I use IP address, browser prompts Usename/ password dialog box. I can enter any username/password there.(Not authenticate correctly).
(I am using a local server for testing)

Please help me on this problem.

private static final boolean authenticateUser(HttpServletRequest request, HttpServletResponse response)
throws Exception{

System.out.println("Inside authenticateUser()....................");
try {
String auth = request.getHeader("Authorization");

if (auth == null) {
response.setHeader("WWW-Authenticate", "NTLM");
return false;

if (!auth.startsWith("NTLM ")) {
throw new Exception("authenticateUser:Cannot authenticate user");

byte[] msg = new sun.misc.BASE64Decoder().decodeBuffer(auth.substring(5));

// Step 1: Negotiation message received
if (msg[8] == 1) {
// Send challenge message (Step 2)
response.setHeader("WWW-Authenticate", "NTLM " + new sun.misc.BASE64Encoder().encodeBuffer(CHALLENGE_MESSAGE));
return false;

// Step 3: Authentication message received
if (msg[8] == 3) {
int off = 30;
int length, offset;

length = (msg[off+1]<<8) + msg[off];
offset = (msg[off+3]<<8) + msg[off+2];
String domain = removeBlanks(new String(msg, offset, length));

length = (msg[off+9]<<8) + msg[off+8];
offset = (msg[off+11]<<8) + msg[off+10];
String user = removeBlanks(new String(msg, offset, length));

length = (msg[off+17]<<8) + msg[off+16];
offset = (msg[off+19]<<8) + msg[off+18];
String ws = removeBlanks(new String(msg, offset, length));

System.out.println("Domain: " + domain + "<br>");
System.out.println("Username: " + user + "<br>");
System.out.println("Workstation: " + ws + "<br>");

request.getSession().setAttribute("user", user);
return true;
else {
throw new Exception("authenticateUser:Cannot authenticate user");
catch (Throwable ex){
throw new Exception("authenticateUser:" + ex.getMessage());

Question by:sudheeral
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8117105
You might have better luck with posting this into the proper programming forum.

LVL 18

Expert Comment

ID: 8119630
Is there a PTR record for that IP (DNS reverse lookup) or a host file entry on the client machine?

Accepted Solution

DominicCronin earned 600 total points
ID: 8143171
I assume you are using Internet Explorer as your browser. If so, this accounts for the fact that using the IP address causes the browser to show a login prompt.

IE will suppress the login box if it can. The idea is that you are logged in to Windows on the client, and you want to be authenticated on the server as well. If the server can recognise the identity you have on the client, then there's no need for a login box. (If logging in with your client credentials fails, then you'll be offered a login prompt).

Generally, for this all to work, you are being authenticated by a domain controller that both the client and the server can "see". There are various other pieces to the equation, such as domain trusts etc. The point is that if you give IE an IP address, it has no way of "guessing" which domain the server is in, so it asks you for a username and password. If the server has indicated in the auth header that it supports NTLM authentication, then you'll see the three-box login allowing you to specify the domain as well.

I can't really follow what you are trying to do in the code, so this is only a partial answer, but I hope it's helpful to understand the rather quirky behaviour of IE with regard to suppressing the login box. (BTW - in recent versions of IE, you can change a setting so that it *never* does the automatic login, and you *always* get the login box)


Author Comment

ID: 8157430
Thnak you very much !!!

Expert Comment

ID: 8210676
One problem with this authentication solution is that the server really just trusts whatever the client is telling it. The client never proves it's identity. It is quite easy to create a client that lets the user impersonate any user on the system. This is not a secure solution, but might be good enough for your application.

More information: http://www.innovation.ch/java/ntlm.html 


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog we highlight approaches to managed security as a service.  We also look into ConnectWise’s value in aiding MSPs’ security management and indicate why critical alerting is a necessary integration.
The recent Petya-like ransomware attack served a big blow to hundreds of banks, corporations and government offices The Acronis blog takes a closer look at this damaging worm to see what’s behind it – and offers up tips on how you can safeguard your…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question