我正在使用 Windows 身份框架在部署到 Azure 的 Web 应用程序中进行身份验证。当我在本地运行应用程序时,我可以使用 PrimaryContext 来查看用户是否在组中:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAIN");
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "GROUP-NAME");
if (user != null)
{
// check if user is member of that group
return user.IsMemberOf(group);
}
这在本地工作,但在部署到 Azure 的应用程序中不起作用。我现在不知道如何查看这些信息。我发现 Azure 不支持 LDAP 协议(protocol),所以这就是它不起作用的原因,但我不确定现在尝试完成此操作的正确方法。我读过一些有关组声明、应用程序角色和 Azure Graph API 的内容,但它们要么看起来有些过分,要么不是我应该采取的方向。有什么帮助吗?
最佳答案
正如您已经发现的,该方法在 Azure AD 中不起作用 - 您的应用程序未在 Intranet 中运行,因此无法进行这些调用。对此的标准解决方案是确保 Azure AD 在登录时发送的 token 中包含组,请参阅此 sample .
您需要牢记各种注意点 - 主要的一点是使用 WIF 会迫使您依赖 WSFederation,如果您有太多组,这就不方便了:Azure AD 通过以下方式避免发行大型 token :希望您回调 Azure AD Graph API 并在事后检索组,但该操作需要更现代的协议(protocol),例如 OpenID Connect(如我链接到的示例中所示)。
另一个挑战是,出于安全原因, token 中的组由其 SID 表示,而不是由名称表示 - 因此您需要知道要验证的 SIDS(或调用 Graph 来检索其名称) .
HTH
关于c# - 在部署到 Azure 的 Web 应用程序中确定登录用户是否是 AD 组的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35494825/