5

Hi I am new to Nodejs and express framework.

I am implementing a simple CRUD application, and users are expecting to visit the page from MS windows. I wish to log down the current windows user name.

I've tried logging the User-Agent string on the page, and it seems User-Agent does not contain the windows user name. Is this true? and what is the right way to implement this?

res.render('search', {user: req.get('User-Agent')});    

Then in jade,

body
    p welcome, #{user}!

Here is what i got:

Welcome, Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36!
Ravi
  • 1,320
  • 12
  • 19
sqr
  • 365
  • 2
  • 12
  • 29

1 Answers1

5

The User-Agent doesn't include the windows username. Have a look at Wikipedia for further information.

A possible solution to your problem may be a NTLM Authentication. To add this install and optionally save express-ntlm as a dependency:

npm install express-ntlm [--save]

Then require and add it as a middleware to express:

var ntlm = require('express-ntlm');
app.use(ntlm());

You will then be able to use the UserName in jade:

body
    p welcome, #{ntlm.UserName}!

In case you want to do a real NTLM Authentication and validate the credentials using an Active Directory you can do this as well:

app.use(ntlm({
    domain: 'MYDOMAIN',
    domaincontroller: 'ldap://myad.example',
}));
Fabio Poloni
  • 8,219
  • 5
  • 44
  • 74
  • ===================== app.js ===================== app.use('/', routes); app.use(ntlm()); app.use('/search', search); ===================== search.js ===================== res.render('search', { user:req.ntlm.UserName }); }); Hi @Fabio, I guess I am confused with the routing here. What should I write in this case if i want ntlm() to run for http://localhost:3000/search – sqr Jan 12 '15 at 08:01
  • Middlewares are executed by they order they are defined. So in your setup it should work. It's not needed to explicitely define `user` since `ntlm.UserName` is available to the rendering context (see my jade example). – Fabio Poloni Jan 12 '15 at 10:43
  • app.use('/', routes); var ntlm = require('express-ntlm'); app.use(ntlm()); app.use('/search', search); This gives me a 401 error. in chrome console: Failed to load resource: net::ERR_UNEXPECTED search:1 GET http://localhost:3000/search net::ERR_UNEXPECTED search:1 GET http://localhost:3000/search net::ERR_UNEXPECTED – sqr Jan 14 '15 at 07:56
  • hi, @Fabio Poloni, I am revisiting the problem. thanks for your previous suggestions. I now realized the problem i have is probably complicated. I re-post my question in http://stackoverflow.com/questions/28183444/how-to-use-express-ntlm-to-get-windows-user-name-without-authentication-using-no I just saw that you are actually a contributor to express-ntlm lib. wow! Do you have any idea why my request.connection.ntlm is null? thanks. – sqr Feb 25 '15 at 07:05