Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 获取在Azure Web App中经过身份验证的用户的电子邮件_Node.js_Azure_Authentication - Fatal编程技术网

Node.js 获取在Azure Web App中经过身份验证的用户的电子邮件

Node.js 获取在Azure Web App中经过身份验证的用户的电子邮件,node.js,azure,authentication,Node.js,Azure,Authentication,我在Node.js中编写的Azure Web应用程序通过Azure Active Directory验证,并使用用户的Microsoft帐户登录。我想知道登录用户的电子邮件,并尝试向客户端和服务器端的/.auth/me端点发出http get请求。然而,在服务器端的最后一次尝试中,我得到了一个401代码:“{\'code\”:401,“message\:\”IDX12741:JWT:“[PII是隐藏的]”必须有三个段(JWS)或五个段(JWE)。\“} 我确保在对端点的请求中包含了AppServ

我在Node.js中编写的Azure Web应用程序通过Azure Active Directory验证,并使用用户的Microsoft帐户登录。我想知道登录用户的电子邮件,并尝试向客户端和服务器端的
/.auth/me
端点发出http get请求。然而,在服务器端的最后一次尝试中,我得到了一个401代码:
“{\'code\”:401,“message\:\”IDX12741:JWT:“[PII是隐藏的]”必须有三个段(JWS)或五个段(JWE)。\“}

我确保在对端点的请求中包含了
AppServiceAuthSession
cookie,但不确定我做错了什么。我没有启用Azure功能,所以这就是我依赖http请求的原因

router.get('/', function(req, res, next) {
  getstuff(req, res);
});

var getstuff =  function (request, response, next) {
  console.log(request);
  console.log("getting stuff");
    var token = request.cookies.AppServiceAuthSession;
    console.log("my token is ", token);
    var options = {
        hostname: 'webportal.com',
        port: 443,
        path: '/.auth/me',
        method: 'GET',
        headers: {
            'x-zumo-auth': token // <-- is this the right field name? 
        }
    };
    var req = https.request(options, (res) => {
        console.log("inside auth me request");
        var str = '';
        res.on('data', (d) => {
            str += d;
        });

        res.on('end', function () {
            console.log("ended ");
            console.log(str);
            response.status(200).type('application/json').json(str);
        });
    });

    req.on('error', (e) => {
        console.error(e);
    });
    req.end();
}
router.get('/',函数(req,res,next){
getstuff(请求、回复);
});
var getstuff=函数(请求、响应、下一步){
控制台日志(请求);
日志(“获取内容”);
var token=request.cookies.AppServiceAuthSession;
log(“我的令牌是”,令牌);
变量选项={
主机名:“webportal.com”,
港口:443,
路径:'/.auth/me',
方法:“GET”,
标题:{
“x-zumo-auth”:令牌//{
log(“内部身份验证请求”);
var-str='';
res.on('数据',(d)=>{
str+=d;
});
res.on('end',function(){
控制台日志(“结束”);
console.log(str);
response.status(200).type('application/json').json(str);
});
});
请求开启('错误',(e)=>{
控制台错误(e);
});
请求结束();
}

AppServiceAuthSession是不同于令牌的cookie。如果您要检索访问令牌,我们需要更新Azure app auth设置的配置,使其获取web API的访问令牌

关于如何配置,请参考以下步骤

  • 登录到

  • 在页面顶部,选择读/写


  • 在左侧浏览器中,导航到订阅资源组提供商Microsoft.Web站点您的应用程序是nodejs express Web app应用程序吗?此外,您能告诉我如何配置您的应用程序吗re Azure AD auth?是的,这是一个web应用程序。基本上,我转到了身份验证并切换到应用程序服务身份验证。您还有其他问题吗?如果您没有其他问题,请接受它作为答案吗?谢谢,我如何找到Azure Active Directory应用程序ID?我需要注册该应用程序吗?我按照您的说明进行了操作,但没有问题在我的中没有属性“x-ms-token-aad-access-token”headers@ILike是因为您已经更新了应用程序服务身份验证设置,但仍然无法看到标题?此外,您可以告诉我您使用的应用程序id吗?是用于配置应用程序服务身份验证的应用程序id吗?
    "allowedAudiences": [
          "<app id>"
        ],
    "additionalLoginParams": ["response_type=code id_token", "resource=<app_id>"]
    
    const express=require('express')
    const request = require('request');
    const router =express.Router()
    
    var app = express()
    router.get('/', async function(req, res) {
     
     var token = req.headers["x-ms-token-aad-access-token"]
      
         getstuff(token).then(response => {
           console.log(response)
          res.json(response)
          })
          .catch(error => {
              res.send(error)
          })
        
      });
      
      var getstuff =  async function (token) {
        
        console.log("getting stuff");
        
       
    var options = {
      'method': 'GET',
      'url': 'https://testnodeweb06.azurewebsites.net/.auth/me',
    
      'headers': {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer '+ token
      }
    };
    console.log(options)
    return new Promise(resolve => {
      request( options,  function (error, response, body) {
         if(!error){
           console.log(body)
          resolve(JSON.parse(body));
    
    
         }
            
      });
      
     
    });
    
      }