简体   繁体   中英

NodeJS + Express + Passport + IIS

I am deploying my node+express application which uses windows authentication. I followed the PassportJS windows-auth documentation for this. But I am facing error =>

iisnode encountered an error when processing the request.

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'.

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.

The last 64k of the output generated by the node.exe process to stderr is shown below:

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. (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:\\Program Files (x86)\\iisnode\\interceptor.js:210:1)

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

            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.

It seems like you import WindowsStrategy but never use it. You could try the following (as suggested in this GitHub issue )

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('MyAuthStrategy', new WindowsStrategy(
  // ...
));

And then specify this name in your authenticated routes

passport.authenticate('MyAuthStrategy')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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