这几篇文章是一个系列的:
- 用issnode+IIS来托管NodeJs Server之一:安装篇
- 用issnode+IIS来托管NodeJs Server之二:移植
- 用issnode+IIS来托管NodeJs Server之三:加入Windows集成验证功能
- 用issnode+IIS来托管NodeJs Server之四:支持Debug
1. 首先要启用网站的Windows认证,方法是:在IIS里面,选择你的站点iisCER,然后点击Authetication进行配置,你应该能看到Windows Authentication的组件,先Enable,然后再Advanced Settings里面勾选Enable Kernel-mode authentication。不然的话,无法正常认证,这个也不知道是为什么。接着,再禁用Anonymous authentication。假如你没有看到Windows Authentication组件,就需要重启下电脑。
这样一来,再访问nodeJs站点就会提示认证了(我测试下来,如果你在域里面,IE可以自动完成认证,而不必输入用户名密码,Firefox和Chrome貌似也行)。
对于配置IIS,可以参考这篇博文,作者写的很好,对我配置IIS和后面的LDAP代码帮助蛮大的:http://www.cnblogs.com/fish-li/archive/2012/05/07/2486840.html
2. IIS默认不会把身份认证的信息传给Nodejs的,所以需要手动配置一下,具体参见:https://github.com/tjanczuk/iisnode/issues/87。其实最终修改的是web.config文件(你直接添加就行了):
<iisnode promoteServerVars="AUTH_USER,AUTH_TYPE" />
3. 经过以上2步,nodeJs端就可以获得用户名了:
exports.sendReport = function (req, res) {
var productId = req.query.productId;
var productName = req.query.productName;
var title = 'Send CER report';
var page = 'page_send_report';
var page_params = {
title: title, header: productName,
productId: productId, productName: productName,
domain_account: req.headers["x-iisnode-auth_user"]
};
用户的每一个Web请求,都会带有一个x-iisnode-auth_user头信息,里面就是用户的域账号,例如ads\fengx。用"\"分隔一下就能拿到域和用户名了。using System;
using System.Diagnostics;
using System.DirectoryServices;
namespace GetAdEmailAddress
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(getEmailAddress(@"ads\someone"));
}
static string getEmailAddress(string domainAccount)
{
String[] domain_username = domainAccount.Split('\\');
Debug.Assert(domain_username.Length == 2);
String domain = domain_username[0];
String user = domain_username[1];
DirectorySearcher search = null;
if (true)
{
search = new DirectorySearcher("(&(objectClass=user)(anr=" + user + "))");
}
else
{
// If we are not in a domain, we need authenticate outselves before access LDAP
//
DirectoryEntry entry = new DirectoryEntry(@"LDAP://ads.autodesk.com", "fengx", "test1234", AuthenticationTypes.Secure);
search = new DirectorySearcher(entry, "(&(objectClass=user)(anr=" + user + "))");
}
search.PropertiesToLoad.Add("mail");
SearchResult result = search.FindOne();
return (result.Properties["mail"][0].ToString());
}
}
}
app.get("/test", function (req, res) {
var exec = require('child_process').exec;
var getEmailAddress = exec('GetAdEmailAddress.exe',
{
cwd: "./app",
env: process.env,
},
function (error, stdout, stderr) {
if (error)
res.send(400, "failed to launch");
else
res.send(200,stdout);
});
});
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="app.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="mysite">
<match url="/*" />
<action type="Rewrite" url="app.js" />
</rule>
</rules>
</rewrite>
<defaultDocument>
<files>
<add value="app.js" />
</files>
</defaultDocument>
<iisnode promoteServerVars="AUTH_USER,AUTH_TYPE" />
</system.webServer>
<system.web>
<identity impersonate="false" />
</system.web>
</configuration>
7. 我开发过程中,还碰到一个严重的问题就是,我代码改了,IIS重启了,但是返回的竟然是我没有改代码前的页面,没办法,只有杀node.exe进程,让iisnode重启node.exe,这个具体是什么原因不知道,应该是iisnode的一个bug。
-------------------------------------------------
1. 假如开启windows认证有问题的话,可以看看这3篇博文:
http://support.microsoft.com/kb/215383/zh-cn
http://statsoft.blog.163.com/blog/static/276531322010414593971
http://blogs.iis.net/nitashav/archive/2010/03/12/iis6-0-ui-vs-iis7-x-ui-series-integrated-windows-authentication.aspx
C:\Windows\System32\inetsrv>appcmd list config /section:windowsAuthentication
C:\Windows\System32\inetsrv>appcmd set config /section:windowsAuthentication /enabled:true
-------------------------------------------------
附博文:http://www.cnblogs.com/fish-li/archive/2012/04/15/2450571.html,免得被博主删了。。。