0

当我使用Azure的应用服务,为我的API(ASP.NET核心),我有这样的Azure的移动应用程序调用invokeApi

[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 
     }); 
    } 
} 

的API控制器然后我试图访问获得401错误这从这样

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(打字稿&科尔多瓦)实施正确显示了与服务被调用,但失败,401错误。它可以工作,如果我打电话给'平'服务。 HTTP调用具有X-谟头,我认为应该是很好的验证(?):

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 Bearer选项。但对于Azure,我没有看到任何教程做类似的步骤。那么,我错过了什么让这个工作?

+0

那么将授权标头添加到您的声明请求中。您从登录中获得结果,使用它 – misha130

+0

@ misha130这就是X-ZUMO-AUTH标题存在的原因。 https://docs.microsoft.com/en-us/azure/app-service/app-service-authentication-overview – n00b

回答

1

我写的应用程序服务ASP.NET中的核心位置:https://shellmonger.com/2017/03/02/azure-app-service-authentication-in-an-asp-net-core-application/

基本上 - 你需要处理在.NET核心令牌。这不是本地的。

+0

谢谢。我的问题实际上是重复的:http://stackoverflow.com/questions/41501612/trouble-getting-claimsprincipal-populated-when-using-easyauth-to-authenticate-ag – n00b

+0

@ adrian-hall你的仓库https:// github.com/adrianhall/netcore-server/tree/p5已关闭。在其他地方代码? – Ben

+0

我不再使用MSFT - 该示例是我离开时转移到MSFT的某个回购协议的一部分。抱歉!我会更新博客以删除链接 –

0

花了几个小时后,我意识到App Services没有为ASP.NET Core Application填充Claim Principal。这意味着它不处理X-ZUMO标题并验证它们。

看看这SO Question

所提出的解决方案是使用X-的Zumo头,使所述/.auth/me服务的调用,这将返回所有权利要求,并使用该响应来设定认证上下文在您的ASP.NET Core应用程序中。

GitHub存储库现在使这很容易做到。它定义了一个扩展方法,以便您可以通过调用

app.UseAzureAppServiceAuthentication();

相关问题