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

问题描述 投票:4回答:3

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

它们包含了一个发行JWT令牌的代码段,但是我的问题是我将如何在我的应用程序中使用它来验证请求?

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

azure azure-mobile-services
3个回答
2
投票
  1. 打开应用服务认证
  2. 添加Microsoft.Azure.Mobile.Server.Login NuGet程序包
  3. 创建自定义身份验证端点
  4. 配置服务以要求认证
  5. 在客户端上使用令牌

请查看更多信息。本文分步介绍了它。

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


0
投票

我终于自己弄清楚了。

[如果有人想知道,这实际上是“有效的”。我查看了源代码,唯一要做的验证是基于JWT令牌加密密钥,“ Audience”设置和“ Issuer”设置。您可以只将[Authorize]属性添加到控制器或方法中,然后管道将处理其余部分。

如果需要自定义声明,则可以将它们添加到MobileAppLoginHandler.CreateToken调用中,并从用户对象中提取。我在IPrincipal上创建了自己的扩展方法,以使用与内置提供程序相同的方式获取具有所需属性的自定义对象。


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;
© www.soinside.com 2019 - 2024. All rights reserved.