API端点可在浏览器URL中工作,但不能在客户端应用程序中工作

时间:2019-02-05 16:41:32

标签: node.js express iis windows-authentication iisnode

我是Web开发的新手,所以也许我已经走了。我在Intranet内,想对前端React应用程序使用Windows授权。我可以从浏览器URL访问端点,但是在我的React应用程序中出现未经授权的错误401。

  • Windows Server 2008 IIS
    • 启用Windows身份验证
    • 关闭匿名身份验证
  • iisnode
    • 已经公开了AUTH_USER,并且可以正常工作
  • 表达后端应用
    • cors中间件
      • 凭证:true
// front end test code
var axios = require('axios')

var config = {
    method:'get',
    url:'someURLendPointThatWorks',
    timeout: 1000,
    withCredentials: true

};
axios(config)
.then(response => {console.log(response)})
.catch(error => {console.log(error)})
// express backend app.js
const express = require('express')
// file path functions
const path = require('path')
// cross origin resource sharing
const cors = require('cors')

// route imports
const ntRouter = require('./routes/nt')
const apiRouter = require('./routes/api')

var app = express()

// cross origin resource sharing
// https://daveceddia.com/access-control-allow-origin-cors-errors-in-react-express/
app.use(cors({
    origin:'*',
    credentials: true
}))

// Routes
app.use(ntRouter)
app.use('/api', apiRouter)

const port = process.env.PORT || 3000
 app.listen(port, () => console.log(`Listening on port ${port}`))
// routes/nt.js
const express = require('express')
const router = express.Router()

router.use((req, res, next) => {
  var username = req.headers['x-iisnode-auth_user'];
  var authenticationType = req.headers['x-iisnode-auth_type'];
  req.NT = username
  next()
})


module.exports = router
// routes/api.js
const express = require('express')
const router = express.Router()

  // check to see if you're authorized, returns your NT
  router.get('/', (req, res) => {
    res.send(`Your NT is ${req.NT}`)
  })

module.exports = router

0 个答案:

没有答案