在 Azure 移动应用程序中向经过身份验证的用户添加其他声明
Adding additional claims to authenticated users in Azure Mobile App
我正在学习 Azure 应用服务中 Azure 移动应用的可能性,并且有几个关于身份验证(和授权)的问题。我使用 Azure 移动应用 Quickstart 来了解可用的功能。
第一个问题:
我是否正确理解我无法使用 Microsoft.Azure.Mobile.Server.Login 包实现自定义身份验证,如文章 Work with the .NET backend server SDK for Azure Mobile Apps 的 "How to: Use custom authentication for your application" 部分所述,因为我想使用内置的 Azure 身份验证提供商(用于通过 Facebook 进行身份验证,Google,等等)?
就安全性而言,我更喜欢配置和使用内置功能而不是自定义开发 ;-)
第二个问题:
我目前正在尝试向在我的 Azure 移动应用程序中成功注册和验证的用户添加一些自定义声明(例如,根据用户的 SID 从 Azure SQL 数据库读取的角色声明)。
实现该目标的最佳方法是什么?
我的想法是像下面这样在 TableController 的 Initialize 方法中添加声明,然后使用 [Authorize(Roles = "Test")] 属性检查角色。
有什么理由不这样做吗?
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
myapptestContext context = new myapptestContext();
DomainManager = new EntityDomainManager<TodoItem>(context, Request);
var claimsPrincipal = this.User as ClaimsPrincipal;
ClaimsIdentity ci = new ClaimsIdentity();
string role = "Test"; // get role from Azure SQL database based on user's SID
ci.AddClaim(new Claim(ClaimTypes.Role, role));
claimsPrincipal.AddIdentity(ci);
}
如果您打算使用内置身份验证提供程序,则不需要使用 Microsoft.Azure.Mobile.Server.Login。内置提供程序(AAD、Facebook、Google、Twitter、MSA)将为您处理所有细节。
就自定义声明而言,我没有任何具体指导。你可以把它放在控制器初始化中,或者你可以添加一个注入它的自定义中间件,甚至是 Global.asax 中的回调。任何最适合您和您的应用的东西。
有一篇很好的文章描述了如何使用自定义令牌处理程序向身份添加自定义声明。在处理程序中,用户的角色作为声明添加。
我正在学习 Azure 应用服务中 Azure 移动应用的可能性,并且有几个关于身份验证(和授权)的问题。我使用 Azure 移动应用 Quickstart 来了解可用的功能。
第一个问题: 我是否正确理解我无法使用 Microsoft.Azure.Mobile.Server.Login 包实现自定义身份验证,如文章 Work with the .NET backend server SDK for Azure Mobile Apps 的 "How to: Use custom authentication for your application" 部分所述,因为我想使用内置的 Azure 身份验证提供商(用于通过 Facebook 进行身份验证,Google,等等)? 就安全性而言,我更喜欢配置和使用内置功能而不是自定义开发 ;-)
第二个问题: 我目前正在尝试向在我的 Azure 移动应用程序中成功注册和验证的用户添加一些自定义声明(例如,根据用户的 SID 从 Azure SQL 数据库读取的角色声明)。 实现该目标的最佳方法是什么? 我的想法是像下面这样在 TableController 的 Initialize 方法中添加声明,然后使用 [Authorize(Roles = "Test")] 属性检查角色。 有什么理由不这样做吗?
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
myapptestContext context = new myapptestContext();
DomainManager = new EntityDomainManager<TodoItem>(context, Request);
var claimsPrincipal = this.User as ClaimsPrincipal;
ClaimsIdentity ci = new ClaimsIdentity();
string role = "Test"; // get role from Azure SQL database based on user's SID
ci.AddClaim(new Claim(ClaimTypes.Role, role));
claimsPrincipal.AddIdentity(ci);
}
如果您打算使用内置身份验证提供程序,则不需要使用 Microsoft.Azure.Mobile.Server.Login。内置提供程序(AAD、Facebook、Google、Twitter、MSA)将为您处理所有细节。
就自定义声明而言,我没有任何具体指导。你可以把它放在控制器初始化中,或者你可以添加一个注入它的自定义中间件,甚至是 Global.asax 中的回调。任何最适合您和您的应用的东西。
有一篇很好的文章描述了如何使用自定义令牌处理程序向身份添加自定义声明。在处理程序中,用户的角色作为声明添加。