简体   繁体   English

MVC4:获取Active Directory用户指南

[英]MVC4: Getting Active Directory User Guid

Once a user is logged into a Windows-Authentication site, how do I get their Active Directoy user guid from the User. 用户登录Windows身份验证站点后,如何从用户获取Active Directoy用户指南。

Eg in an Action: 例如在行动中:

ViewBag.Message = User.Identity.GUID????

You should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. 您应该检查System.DirectoryServices.AccountManagement (S.DS.AM)命名空间。 Read all about it here: 在这里阅读所有相关内容:

Basically, you can define a domain context and easily find users and/or groups in AD: 基本上,您可以定义域上下文并轻松查找AD中的用户和/或组:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find the current user
UserPrincipal user = UserPrincipal.Current;

if(user != null)
{
   // get guid
   var userGuid = user.Guid;
}

The new S.DS.AM makes it really easy to play around with users and groups in AD! 新的S.DS.AM使得在AD中与用户和群组玩游戏变得非常容易!

 string userName = user.Identity.Name.Split('\\')[1];

            using (var oRoot = new DirectoryEntry(ConfigurationManager.AppSettings["LDAPDomain"], null, null, AuthenticationTypes.Secure))
            {
                using (var deSearch = new DirectorySearcher(oRoot))
                {
                    deSearch.Filter = string.Format("(&(sAMAccountName={0}))", userName);
                    SearchResult searchResult = deSearch.FindOne();
                    if (searchResult != null)
                    {
                        DirectoryEntry de = searchResult.GetDirectoryEntry();

                    }
                }
            }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM