Cordova 调用invokeApi时azure移动应用程序获取401错误

Cordova 调用invokeApi时azure移动应用程序获取401错误,cordova,ionic2,google-oauth,azure-mobile-services,asp.net-core-webapi,Cordova,Ionic2,Google Oauth,Azure Mobile Services,Asp.net Core Webapi,我正在为我的Api ASP.NET核心使用Azure应用程序服务,我有一个这样的Api控制器 [Route("api")] public class PingController : Controller { [HttpGet] [Route("ping")] public string Ping() { return "Pong"; } [Authorize] [HttpGet("claims")] public

我正在为我的Api ASP.NET核心使用Azure应用程序服务,我有一个这样的Api控制器

[Route("api")]
public class PingController : Controller
{
    [HttpGet]
    [Route("ping")]
    public string Ping()
    {
        return "Pong";
    }

    [Authorize]
    [HttpGet("claims")]
    public object Claims()
    {
        return User.Claims.Select(c =>
        new
        {
            Type = c.Type,
            Value = c.Value
        });
    }
}
this.client = new WindowsAzure.MobileServiceClient('https://mysite.azurewebsites.net/');
 this.client.login('google').then(result=>{
    this.client.invokeApi("claims", {
        body: null,
        method: "get"
    }).done(function (results) {
        alert(JSON.stringify(results));
    }, function (error) {
        let msg = error.message;
        let request = error.request;
        alert(msg+';'+request.status);
    });
});
然后我尝试从Ionic2 TypeScript和Cordova实现中访问它,就像这样

[Route("api")]
public class PingController : Controller
{
    [HttpGet]
    [Route("ping")]
    public string Ping()
    {
        return "Pong";
    }

    [Authorize]
    [HttpGet("claims")]
    public object Claims()
    {
        return User.Claims.Select(c =>
        new
        {
            Type = c.Type,
            Value = c.Value
        });
    }
}
this.client = new WindowsAzure.MobileServiceClient('https://mysite.azurewebsites.net/');
 this.client.login('google').then(result=>{
    this.client.invokeApi("claims", {
        body: null,
        method: "get"
    }).done(function (results) {
        alert(JSON.stringify(results));
    }, function (error) {
        let msg = error.message;
        let request = error.request;
        alert(msg+';'+request.status);
    });
});
登录屏幕正确显示,服务被调用,但失败并出现401错误。如果我调用“ping”服务,它就会工作。 HTTP调用具有X-ZUMO头,我认为这对于身份验证应该是很好的:

GET /api/claims HTTP/1.1
Host: mysite.azurewebsites.net
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
ZUMO-API-VERSION: 2.0.0
X-ZUMO-INSTALLATION-ID: 10badf87-...
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
X-ZUMO-AUTH: eyJ0eXAiOiJKV...
accept: application/json
X-ZUMO-VERSION: ZUMO/2.0 (lang=Cordova; os=--; os_version=--; arch=--; version=2.0.0-41128.193844)
Referer: http://localhost:8000/index.html
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
Cookie: ARRAffinity=4df5cc005....
Azure应用程序调试日志只显示IIS的默认401屏幕。表示资源已作为“匿名”访问。
我以前使用过Auth0,您必须在应用程序配置中注册JWT承载选项。但是对于Azure,我没有看到任何教程执行类似的步骤。那么,为了让它正常工作,我遗漏了什么呢?

我在ASP.NET Core中写到了应用程序服务:


基本上-您需要在.NETCore中处理令牌。它不是本机的。

花了几个小时后,我意识到应用程序服务没有填充ASP.NET核心应用程序的声明主体。这意味着它没有处理X-ZUMO头并验证它们

看看这个

建议的解决方案是使用X-ZUMO头调用/.auth/me服务,该服务返回所有声明,并使用响应在ASP.NET核心应用程序内设置身份验证上下文

这个存储库现在使这项工作变得非常容易。它定义了一个扩展方法,这样您就可以通过调用

app.UseAzureAppServiceAuthentication


我们将授权头添加到您的索赔请求中。您将从登录中获得一个结果,请使用it@misha130这就是X-ZUMO-AUTH头出现的原因。谢谢我的问题实际上是重复的:@adrian hall您的存储库已关闭。其他地方的代码?我不再与微软金融公司合作了-该样本是我离开时转移到微软金融公司的一份回购协议的一部分。很抱歉我将更新博客以删除链接