如何在Azure移动应用程序中实现自定义身份验证

时间:2015-11-20 12:41:27

标签: azure azure-mobile-services

在Azure移动应用程序的最新更新中,最终添加了对自定义身份验证的支持,参考:https://azure.microsoft.com/en-us/blog/azure-mobile-apps-november-2015-update

他们已经包含了一个用于发布JWT令牌的代码段,但我的问题是如何在我的应用中使用它来验证请求?

我想我需要在我的WebApiConfig中添加自定义令牌处理程序,但我找不到关于该主题的任何文档。

2 个答案:

答案 0 :(得分:2)

  1. 启用应用服务身份验证
  2. 添加Microsoft.Azure.Mobile.Server.Login NuGet包
  3. 创建自定义身份验证端点
  4. 配置要求身份验证的服务
  5. 在客户端上使用令牌
  6. 请查看此内容以获取更多详情。本文逐步解释它。

    http://www.newventuresoftware.com/blog/custom-authentication-with-azure-mobile-apps/

答案 1 :(得分:0)

生成Azure令牌并将其返回给应用程序。您需要 Microsoft.Azure.Mobile.Server.Login NuGet程序包。

 var claims = new Claim[]
    {
        new Claim(JwtRegisteredClaimNames.Sub, "YOUR_UNIQUE_EMAIL_OR_USERNAME_OR_PHONENUMBER")
    };

    var signingKey = Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY");
    var audience = "https://myservice.azurewebsites.net/"; // audience must match the url of the site
    var issuer = "https://myservice.azurewebsites.net/"; // audience must match the url of the site

    JwtSecurityToken token = AppServiceLoginHandler.CreateToken(
        claims,
        signingKey,
        audience,
        issuer,
        TimeSpan.FromDays(30)
        );

string tokenString = token.RawData;