(内联网应用程序)我使用 ReactJS 和 NodeJS 作为服务器端 api 来访问各种资源(例如用户事件目录配置文件)。我需要与 Active Directory 集成,而不提示用户输入凭据。
使用 ActiveDirectory for Node (npm i activedirectory) 我可以使用 LDAP 查询 AD,以获取硬编码的 sAMAccountName,使用下面的代码。
ad.findUser(sAMAccountName, function(err, user) {
if (err) {
console.log('ERROR: ' + JSON.stringify(err));
}
if (!user) console.log('User: ' + sAMAccountName + ' not found.');
else {
thisUser = user;
}
});
但我不明白的是,当他们调用 API 时如何获取当前用户 ID。
这 2 个示例给出了“服务器”用户 ID,而不是客户端 ID:
const user = process.env;
const user = require('os').userInfo().username;
在 .net 中,我通常会使用
string NTID = HttpContext.Current.User.Identity.Name;
那么在服务器上运行的 NodeJS 中有没有一种方法可以访问客户端用户 ID?
---------更新:---------
我一直在尝试使用 Passport-windowsauth 实现下面的解决方案,但无法完全正常工作。我的 web.config 文件配置如下:
<iisnode watchedFiles="*.js;node_modules\*;routes\*.js;views\*.jade" promoteServerVars="LOGON_USER,AUTH_USER" />
<security>
<authentication>
<anonymousAuthentication enabled="true" />
<windowsAuthentication enabled="true" />
</authentication>
</security>
Azure 配置设置为:
这是我单击按钮开始身份验证时调用的函数:
activeDirectory = () => {
let url = '';
//Default to current user when in dev
if (
window.location.href.indexOf('localhost') > -1
) {
url = 'http://localhost:5000/express-passport';
} else {
url = '/express-passport';
}
axios({
method: "GET",
url: url,
withCredentials: true
})
.then(response => {
console.log("Done: ", response.data);
})
.catch(error => {
console.log("An error occurred - ", error);
});
};
这是 NodeJs 服务器路由代码:
const passport = require("passport");
const WindowsStrategy = require("passport-windowsauth");
module.exports = app => {
let thisUser = {};
passport.use(
new WindowsStrategy(
{
ldap: {
url: "ldap://somethingldap.somewhere.com/CN=,DC=,DC=,DC=,DC=",
base: "CN=,DC=,DC=,DC=,DC=",
bindDN: "serviceAccountDetailsHere",
bindCredentials: "masked"
}
},
function(profile, done) {
thisUser = profile;
console.log("Profile:", profile);
}
)
);
app.get(
"/express-passport",
function(req, res, next) {
passport.authenticate("WindowsAuthentication", function(
error,
user,
info
) {
// log everything to console
console.log(error);
console.log(user);
console.log(info);
if (error) {
res.status(401).send(error);
} else if (!user) {
res.status(401).send(info);
} else {
next();
}
res.status(401).send(info);
})(req, res);
},
// function to call once successfully authenticated
function(req, res) {
res.status(200).send("logged in!");
}
);
};
现在,当页面打开时,我单击一个按钮,希望它能够进行身份验证。出现一个弹出窗口,询问我的凭据,我输入了该凭据,但弹出窗口只是消失并不断重新出现。我显然做错了什么......
最佳答案
您最大的问题是让它在您的 MacOS 开发计算机上运行,并且可以无缝转移到 Windows 计算机上并以相同的方式工作。
node-sspi包可能是最简单的,但它也仅适用于 Windows。
我认为你最好的选择是使用 passport-windowsauth模块 Passport.js并将其与 LDAP 一起使用,因为您可以从任何操作系统对 AD 进行 LDAP 查询。
在服务器上,将其放在 IIS 后面并按照 Integrated Authentication 下的说明进行操作使用 Windows 身份验证设置 IIS,使其成为用户的无缝登录。
在您的 MacOS 开发机器上,您当然不能这样做。我认为您能为 MacOS 做的最好的事情就是它会要求您提供凭据。你必须对此进行测试。我不确定它是否会像通常那样在没有 IIS 的情况下进行身份验证质询(我认为不会)。如果没有,您可以按照他们的Non-integrated authentication创建一个单独的登录页面。说明,并且您只能在您的开发机器上使用它。
在对passport.use()
的实际调用中,两者之间的唯一区别是integrated: false
。因此,您可能会发现您必须为本地开发与服务器进行不同的设置(希望您可以让代码检测您是否在开发计算机上并将其自动设置为 false)。
所以,是的,您的开发计算机和服务器之间会有不同的登录行为,但至少您应该能够按原样升级您的代码并使其在两个地方都能工作。
关于node.js - 服务器端 NodeJS - 需要客户端 Windows ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53761259/