简体   繁体   English

NodeJS + Express + Passport + IIS

[英]NodeJS + Express + Passport + IIS

I am deploying my node+express application which uses windows authentication. 我正在部署使用Windows身份验证的node + express应用程序。 I followed the PassportJS windows-auth documentation for this. 我为此遵循了PassportJS windows-auth文档。 But I am facing error => 但我面临错误=>

iisnode encountered an error when processing the request. 处理请求时,iisnode遇到错误。

HRESULT: 0x2 HTTP status: 500 HTTP subStatus: 1002 HTTP reason: Internal Server Error You are receiving this HTTP 200 response because system.webServer/iisnode/@devErrorsEnabled configuration setting is 'true'. HRESULT:0x2 HTTP状态:500 HTTP subStatus:1002 HTTP原因:内部服务器错误您收到此HTTP 200响应,因为system.webServer/iisnode/@devErrorsEnabled配置设置为“true”。

In addition to the log of stdout and stderr of the node.exe process, consider using debugging and ETW traces to further diagnose the problem. 除了node.exe进程的stdout和stderr的日志之外,请考虑使用调试和ETW跟踪来进一步诊断问题。

The last 64k of the output generated by the node.exe process to stderr is shown below: node.exe进程生成到stderr的最后64k输出如下所示:

Application has thrown an uncaught exception and is terminated: Error: authentication strategies must have a name at Passport.use (C:\\Workspace\\Trial\\node_modules\\passport\\lib\\passport\\index.js:51:20) at Object. 应用程序抛出未捕获的异常并终止:错误:身份验证策略必须在Object的Passport.use(C:\\ Workspace \\ Trial \\ node_modules \\ passport \\ lib \\ passport \\ index.js:51:20)中具有名称。 (C:\\Workspace\\Trial\\server.js:7:10) at Module._compile (internal/modules/cjs/loader.js:689:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3) at Module.require (internal/modules/cjs/loader.js:637:17) at require (internal/modules/cjs/helpers.js:22:18) at Object. (C:\\ Workspace \\ Trial \\ server.js:7:10)在Module._compile(internal / modules / cjs / loader.js:689:30)的Object.Module._extensions..js(internal / modules / cjs) /loader.js:700:10)在Function.Module的tryModuleLoad(internal / modules / cjs / loader.js:538:12)的Module.load(internal / modules / cjs / loader.js:599:32)。 _load(internal / modules / cjs / loader.js:530:3)在Module.require(internal / modules / cjs / loader.js:637:17)at require(internal / modules / cjs / helpers.js:22: 18)在对象。 (C:\\Program Files (x86)\\iisnode\\interceptor.js:210:1) (C:\\ Program Files(x86)\\ iisnode \\ interceptor.js:210:1)

server.js server.js

                <configuration>
            <system.webServer>
               <iisnode promoteServerVars="LOGON_USER" />
                <handlers>
                    <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
                </handlers>
                <rewrite>
                    <rules>
                        <rule name="sendToNode">
                            <match url="/*" />
                            <action type="Rewrite" url="server.js" />
                        </rule>
                    </rules>
                </rewrite>
            </system.webServer>
            <system.web>
                    <authentication mode="Windows" />
            </system.web>
            </configuration>

web.config web.config中

            var express = require('express');

            var app = express();
            var passport = require('passport');
            var WindowsStrategy = require('passport-windowsauth');

            passport.use(function(profile, done){
              User.findOrCreate({ waId: profile.id }, function (err, user) {
                done(err, user);
              });
            });

            app.get('/NodeTrialLogon/', function (req, res) {
                res.send('Express is workin on IISNode!');
            });

            app.get('/NodeTrialLogon/express-passport',
              passport.authenticate('WindowsAuthentication'),
              function (req, res){
                res.json(req.user);
              });
            app.listen(process.env.PORT);

Please help. 请帮忙。 I have enabled windows authentication in IIS and disabled all other forms of authentication. 我在IIS中启用了Windows身份验证,并禁用了所有其他形式的身份验证。

It seems like you import WindowsStrategy but never use it. 好像你导入了WindowsStrategy但从未使用它。 You could try the following (as suggested in this GitHub issue ) 你可以尝试以下(如本GitHub问题所示

passport.use(new WindowsStrategy({
    integrated: true
  },
  function(profile, done) {
    User.findOrCreate({ waId: profile.id }, function (err, user) {
      done(err, user);
    });
  }
));

You could also then give a name to your strategy as the first argument of passport.use() 然后你也可以给你的策略命名,作为passport.use()的第一个参数。

passport.use('MyAuthStrategy', new WindowsStrategy(
  // ...
));

And then specify this name in your authenticated routes 然后在经过身份验证的路由中指定此名称

passport.authenticate('MyAuthStrategy')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM