【问题标题】:Getting samAccountName name in MVC Web App from Azure Active Directory从 Azure Active Directory 获取 MVC Web App 中的 samAccountName 名称
【发布时间】:2018-10-19 13:51:30
【问题描述】:

我需要在部署到 Azure Web 应用程序的 MVC C# 应用程序中获取 AD samAccountName。

该应用已针对 Azure AD 进行 Windows 身份验证,Azure AD 使用 ADConnect 与我们本地的本地 AD 服务器同步。

当我们在本地运行 Web 应用程序 (Visual Studio 2017) 时,从以下位置返回的值:

User.Identity.Name

以 DOMAIN\UserName 格式返回

但是在 Azure 中查看 WebApp 时,相同的代码以 firstname.lastname@domain.co.uk 格式返回它

感谢我们无法使用 User.Identity.Name 来获得一致的结果,但我们确实需要一种在站点运行 Azure 时获取此信息的方法。

我们已经研究了使用声明描述和扩展属性实现这一目标的各种方法,但到目前为止都没有运气。

我已尝试提供尽可能多的信息,但我正在与我们的基础架构团队合作,因此可能提供的信息不够多,如果需要更多信息,请告诉我。

【问题讨论】:

  • 在 Azure 中运行时,这会给您帐户的 SID 吗?:((System.Security.Principal.WindowsIdentity) User.Identity).User.Value
  • @GabrielLuci 不幸的是,我们无法将“System.Security.Claims.ClaimsIdentity”类型的对象转换为“System.Security.Principal.WindowsIdentity”类型

标签: c# azure model-view-controller active-directory azure-active-directory


【解决方案1】:

“firstname.lastname@domain.co.uk”格式将是帐户的userPrincipalName 属性。因此,如果您在名称中看到“@”,则可以连接到 AD 并搜索帐户并拉取sAMAccountName。像这样的:

var searcher =
    new DirectorySearcher(
        new DirectoryEntry("LDAP://domain.co.uk"),
        $"(&(objectClass=user)(userPrincipalName={User.Identity.Name}))");
searcher.PropertiesToLoad.Add("sAMAccountName");
var result = searcher.FindOne();
var sAmAccountName = result.Properties["sAMAccountName"][0] as string;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多