User.Identity.Name 中的全名而不是域 ID
User.Identity.Name
属性返回域登录 ID。
哪个类/属性公开了实际的用户名?
对于登录到提供 my_domain\jdoe 的 Web 应用程序的用户“John Doe”,
**User.Identity.Name -**
Returns : *my_domain\jdoe*
**System.Environment.UserName**
Returns: *jdoe*
返回哪个类/属性? ...“约翰·多伊”
The User.Identity.Name
property returns the domain login id.
Which class/property exposes the actual user name?
For user "John Doe" who logs into the web application supplying my_domain\jdoe
**User.Identity.Name -**
Returns : *my_domain\jdoe*
**System.Environment.UserName**
Returns: *jdoe*
Which class/property returns? ... "John Doe"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
听起来您不是在查找登录名,而是在查找 Active Directory 用户帐户的显示名称。 您可能想要做的是执行 AD 搜索 (DirectorySearcher) 并从搜索结果属性中获取显示名称。
我假设您处于 AD 环境中,因为您标记了问题 adsi。
注意:如果您使用 .NET 3.5,您可能需要查看 tvanfosson 的帖子。
Sounds like instead of the login name, you are after the display name of an Active Directory user account. What you might want to do is to do an AD search (DirectorySearcher) and get the display name from the search result property.
I'm assuming that you are in an AD environment, since you tagged the question adsi.
Note: If you are working with .NET 3.5, you might want to look at tvanfosson's post.
IIdentity 接口为 User.Identity 提供 Name 属性。 IIdentity 接口可以在任意数量的知道如何从数据存储(SQL Server、Active Directory 等)中查找用户的类上实现。
IIdentity 接口没有提供“John Doe”的属性。 如果该信息位于您的数据存储中,那么您将需要使用特定于该数据存储的工具来访问它。
也就是说,User.Identity 返回的对象完全有可能有一个包含“John Doe”的属性,您可以通过 IIdentity 之外的其他接口访问该属性(例如,我们的自定义 IIdentity 实现就是这样做的)。
The IIdentity interface is that which provides the Name property on User.Identity. The IIdentity interface can be implemented on any number of classes which know how to lookup users from a data-store (SQL Server, Active Directory, etc).
There is no property of the IIdentity interface which provides "John Doe". If that information is located in your data-store then you'll need to use the tools specific to that data-store to access it.
That said, its entirely possible that the object which is returned by User.Identity has a property which contains "John Doe" that you might be able to access through some other interface besides IIdentity (our custom IIdentity implementation does this, for example).
您可以
从 此处调用代码模拟它< /a>
and the you can just call
code mock it from here
也许我在某个地方犯了一个错误,但 WinNT://... 对我来说不适用于域帐户。 此外,如果用户与计算机不在同一域中,则 PrimaryContext 可能找不到所需的元素(因为它在当前上下文中搜索,如果按上述方式使用)。
以下内容应将 User.Identity.Name 提供的“友好域名”转换为 ldap 兼容域。 使用域的distinctName传递所需的ldap路径信息(这解决了我的跨域问题):
Maybe I have made a mistake somewhere, but WinNT://... hasn't worked for domain accounts for me. Additionally, if the user isn't in the same domain as the machine, than PrincipalContext may not find the wanted element (as it searches in the current context, if used as above).
The following should translate the "friendly domain name" as provided by User.Identity.Name to an ldap compliant domain. Using the distinguishName of the domain delivers the needed ldap path information (which has solved my cross domain problem):
现在距离最初的帖子已经过去 15 年了,上面 tvanfosson 的帖子效果很好,但是现在,您可以删除大部分代码并简单地使用:
这已经过测试可以在 .NET 4.8 中工作
It's now 15 years past the original post, and tvanfosson's post above works great, but now, you can drop most of that code and simply use:
This has been tested to work in .NET 4.8
如果您考虑的是 Active Directory,则需要找到与给定 samAccountName 对应的 UserPrincipal 并从中获取 DisplayName 属性。 请注意,它可能未设置。
If you are thinking Active Directory, you'll need to find the UserPrincipal that corresponds to the given samAccountName and get the DisplayName property from it. Note that it may not be set.