获取专有名称从Active Directory中的当前登录的用户

编程入门 行业动态 更新时间:2024-10-24 16:33:14
本文介绍了获取专有名称从Active Directory中的当前登录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经搜索周围,但无法找到任何code段吧。

I have searched around but could not find any code snippet for it.

我怎样才能从当前登录的用户在C#中的Active Directory中的专有名称?

How can I get the distinguished name from Active Directory of the currently logged in user in C#?

不幸的是我还没有找到一个解决办法..也许有些你以前已经做到了这一点,或有一个片段而不是吗?

Unfortunately I have not found a solution yet.. Maybe some of you has done this before or has a snippet which does it?

推荐答案

检查下面的代码片段。你必须传递给 Identity.Name 的IPrincipal.我假设用户已经通过身份验证在Active Directory中(即使用标准的IIS授权方法)。

Check following snippet. You have pass to Identity.Name from IPrincipal. I assume that the user is already authenticated in Active Directory (ie. using standard IIS authorization methods).

private string GetUserName(string identity) { if (identity.Contains("\\")) { string[] identityList = identity.Split('\\'); return identityList[1]; } else { return identity; } } public string GetUserDn(string identity) { var userName = GetUserName(identity); using (var rootEntry = new DirectoryEntry("LDAP://" + adConfiguration.ServerAddress, null, null, AuthenticationTypes.Secure)) { using (var directorySearcher = new DirectorySearcher(rootEntry, String.Format("(sAMAccountName={0})", userName))) { var searchResult = directorySearcher.FindOne(); if (searchResult != null) { using (var userEntry = searchResult.GetDirectoryEntry()) { return (string)userEntry.Properties["distinguishedName"].Value; } } } } return null; }

更多推荐

获取专有名称从Active Directory中的当前登录的用户

本文发布于:2023-10-23 01:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名称   用户   Active   Directory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!