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

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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

ITIL has an elaborate incident management framework. This article serves as a starter for those who'd like to know more or need to suss out the baseline elements in a typical incident response execution plan on the "need to have" and the "good to ha…
If you are like me and like multiple layers of protection, read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

616 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