Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get current windows user from express?

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!
like image 599
sqr Avatar asked Dec 24 '14 03:12

sqr


1 Answers

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',
}));
like image 54
Fabio Poloni Avatar answered Nov 13 '22 23:11

Fabio Poloni