0 Replies Latest reply: Nov 1, 2012 8:03 AM by 968354 RSS

    NTLM Authentication Issue

    968354
      Hi,

      I am trying to get windows user name in we application using the below code which is running somewhere.
      I am stuck at one line which should return me msg[8] = 3 to move further. In below highlighted line, msg[8] need to be returned 3 in order to go in else statement which is not happening. Every time I am getting its value as 1 which makes the logic travel to if statement and them program terminates.

      Is there any IE setting that i am missing here. Please reply.

      public void doFilter(ServletRequest req, ServletResponse res,
      FilterChain chain) {
      HttpServletRequest request = (HttpServletRequest) req;
      HttpServletResponse response = (HttpServletResponse) res;

      // String username = null;
      //first, get the user agent

      String useragent = request.getHeader("user-agent");
      //if you're using IE, you can continue
      if ((useragent.indexOf("MSIE") > -1)) {
      //Always do the ntlm check (for IE POST back)
      try {
      String auth = request.getHeader("Authorization");
      if (auth == null) {
      response.setHeader("WWW-Authenticate", "NTLM");
      response.setStatus(response.SC_UNAUTHORIZED);
      response.setContentLength(0);
      response.flushBuffer();
      return;
      }
      if (auth.startsWith("NTLM ")) {
      byte[] msg = new BASE64Decoder().decodeBuffer(auth.substring(5));
      int off = 0, length, offset;
      if (msg[8] == 1) {
      byte z = 0;
      byte[] msg1 = {(byte) 'N', (byte) 'T', (byte) 'L', (byte) 'M', (byte) 'S', (byte) 'S', (byte) 'P', z, (byte) 2, z, z, z, z, z, z, z, (byte) 40, z, z, z, (byte) 1, (byte) 130, z, z, z, (byte) 2, (byte) 2, (byte) 2, z, z, z, z, z, z, z, z, z, z, z, z};
      response.setHeader("WWW-Authenticate", "NTLM " + new BASE64Encoder().encodeBuffer(msg1));
      response.setStatus(response.SC_UNAUTHORIZED);
      response.setContentLength(0);
      response.flushBuffer();
      return;
      } else if (msg[8] == 3) {
      off = 30;

      length = msg[off + 17] * 256 + msg[off + 16];
      offset = msg[off + 19] * 256 + msg[off + 18];
      String remoteHost = new String(msg, offset, length);

      length = msg[off + 1] * 256 + msg[off];
      offset = msg[off + 3] * 256 + msg[off + 2];
      String domain = new String(msg, offset, length);

      length = msg[off + 9] * 256 + msg[off + 8];
      offset = msg[off + 11] * 256 + msg[off + 10];
      String username = new String(msg, offset, length);

      PrintWriter out = res.getWriter();
      out.println("Username:" + username + "
      ");
      out.println("RemoteHost:" + remoteHost + "
      ");
      out.println("Domain:" + domain + "
      ");
      }
      }

      } catch (Exception e) {
      System.out.println(e);
      }
      //System.out.println("Suc);

      }

      try {
      chain.doFilter(req, res);
      } catch (IOException e) {
      System.out.println(e);
      } catch (ServletException e) {
      System.out.println(e);
      }
      }

      Edited by: 965351 on Nov 1, 2012 6:03 AM