简体   繁体   English

从Azure Active Directory获取MVC Web应用程序中的samAccountName名称

[英]Getting samAccountName name in MVC Web App from Azure Active Directory

I have a requirement to get the AD samAccountName in an MVC C# application that's deployed to an Azure Web App. 我需要在部署到Azure Web应用程序的MVC C#应用程序中获取AD samAccountName。

The app is Windows Authenticated against Azure AD which is synced with our local on premise AD servers using ADConnect. 该应用程序已针对Azure AD进行了Windows身份验证,并使用ADConnect与我们本地的本地AD服务器进行了同步。

When we run the Web App locally (Visual Studio 2017), the value that's returned from: 当我们在本地运行Web App(Visual Studio 2017)时,返回的值是:

User.Identity.Name

is returned in the format DOMAIN\\UserName 以DOMAIN \\ UserName格式返回

But when looking at the WebApp in Azure, the same code returns it in the format firstname.lastname@domain.co.uk 但是,在Azure中查看WebApp时,相同的代码以firstname.lastname@domain.co.uk的格式返回它

I appreciate that we won't be able to use User.Identity.Name to achieve a consistent result, but we do need a way of getting this information when the site is running Azure. 我很欣赏我们无法使用User.Identity.Name来获得一致的结果,但是当站点运行Azure时,我们确实需要一种获取此信息的方法。

We've looked various ways of achieving this using Claims Descriptions and Extended Properties but have had no luck so far. 我们已经找到了使用Claims Descriptions和Extended Properties实现此目的的各种方法,但是到目前为止还没有运气。

I've tried to give as much information as possible, but I'm working in conjunction with our infrastructure team so may not have provided enough, please let me know if more info is required. 我已尝试提供尽可能多的信息,但是我正在与我们的基础架构团队合作,因此可能未提供足够的信息,请让我知道是否需要更多信息。

The "firstname.lastname@domain.co.uk" format would be the userPrincipalName attribute of the account. “ firstname.lastname@domain.co.uk”格式将是帐户的userPrincipalName属性。 So if you see an "@" in the name, you can connect to AD and search for the account and pull the sAMAccountName . 因此,如果您在名称中看到“ @”,则可以连接到AD并搜索该帐户并提取sAMAccountName Something like this: 像这样:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Active Directory检索SAMAccountName - How to retrieve SAMAccountName from Active Directory 通过Web应用程序中的Azure Active Directory进行身份验证 - Authenticating through Azure Active Directory in a Web App 使用用户SAMAccountName从Active Directory中读取嵌套的组名 - Reading Nested Groupnames from Active Directory using Users SAMAccountName MVC4网站从错误的Active Directory获取用户详细信息。 - MVC4 web getting the user details from wrong Active directory. 获取Azure Active Directory令牌 - Getting Azure Active directory token MVC Core + Azure Active Directory 从 GraphAPI 获取组 - MVC Core + Azure Active Directory Get Groups from GraphAPI 从Active Directory获取用户角色/组 - mvc3 - Getting user roles/groups from Active Directory - mvc3 从 C# 控制台应用程序访问 Azure Active Directory 并获得“权限不足,无法完成操作”。 错误信息 - Accessing Azure Active Directory from C# console app and getting "Insufficient privileges to complete the operation." error message 如何在 Azure Web App 中获取我的 C# 代码中的用户名? - How to get my Username in C# Code in Azure Web App while logged in with Azure Active Directory? 带有ASP.NET MVC的Azure活动目录 - Azure active directory with ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM