如何在 Azure 移动应用程序中实现自定义身份验证
How to implement custom authentication in an Azure Mobile App
在 Azure Mobile Apps 的最新更新中,终于添加了对自定义身份验证的支持,参考:https://azure.microsoft.com/en-us/blog/azure-mobile-apps-november-2015-update。
他们包含了一个用于发布 JWT 令牌的片段,但我的问题是我将如何在我的应用程序中使用它来验证请求?
我想我需要在我的 WebApiConfig 中添加自定义令牌处理程序,但我找不到关于该主题的任何文档。
我最终自己弄明白了。
如果有人想知道,这实际上是 "just works"。我查看了源代码,唯一完成的验证是基于 JWT 令牌加密密钥、"Audience" 设置和 "Issuer" 设置。您只需将 [Authorize] 属性添加到控制器或方法,管道会处理其余部分。
如果需要自定义声明,可以将它们添加到 MobileAppLoginHandler.CreateToken 调用并从用户对象中提取。我在 IPrincipal 上制作了自己的扩展方法,以获取具有我需要的属性的自定义对象,方式与内置提供程序相同。
- 开启应用服务身份验证
- 添加Microsoft.Azure.Mobile.Server.Login NuGet 包
- 创建自定义身份验证端点
- 将服务配置为需要身份验证
- 在客户端使用令牌
请查看此处了解更多详情。本文将逐步解释它。
http://www.newventuresoftware.com/blog/custom-authentication-with-azure-mobile-apps/
生成一个 Azure 令牌并将其return 发送到应用程序。您需要 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;
在 Azure Mobile Apps 的最新更新中,终于添加了对自定义身份验证的支持,参考:https://azure.microsoft.com/en-us/blog/azure-mobile-apps-november-2015-update。
他们包含了一个用于发布 JWT 令牌的片段,但我的问题是我将如何在我的应用程序中使用它来验证请求?
我想我需要在我的 WebApiConfig 中添加自定义令牌处理程序,但我找不到关于该主题的任何文档。
我最终自己弄明白了。
如果有人想知道,这实际上是 "just works"。我查看了源代码,唯一完成的验证是基于 JWT 令牌加密密钥、"Audience" 设置和 "Issuer" 设置。您只需将 [Authorize] 属性添加到控制器或方法,管道会处理其余部分。
如果需要自定义声明,可以将它们添加到 MobileAppLoginHandler.CreateToken 调用并从用户对象中提取。我在 IPrincipal 上制作了自己的扩展方法,以获取具有我需要的属性的自定义对象,方式与内置提供程序相同。
- 开启应用服务身份验证
- 添加Microsoft.Azure.Mobile.Server.Login NuGet 包
- 创建自定义身份验证端点
- 将服务配置为需要身份验证
- 在客户端使用令牌
请查看此处了解更多详情。本文将逐步解释它。
http://www.newventuresoftware.com/blog/custom-authentication-with-azure-mobile-apps/
生成一个 Azure 令牌并将其return 发送到应用程序。您需要 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;