试图访问Active Directory中的属性以添加到数据库中

问题描述 投票:0回答:1

根据输入的ID从AD中提取用户信息的工作。我收到的错误是“无法将类型“字符串”隐式转换为类型“ System.DirectoryServices.DirectoryEntry”,发生在保存方法中: DirectoryEntry de = new DirectoryEntry(); de = QueryAD(objSearchRolesViewModel.NID);

打开连接

 private DirectoryEntry GetDirectoryObject()
        {
            DirectoryEntry oDE;
            oDE = new DirectoryEntry("LDAP://myConnection");
            return oDE;
        }

查询广告

public string QueryAD(string userNID)
        {
            DirectorySearcher ds = new DirectorySearcher
            {
                SearchRoot = new DirectoryEntry(""),
                //start searching from local domain
                Filter = userNID
            };
            ds.PropertiesToLoad.Add("givenname");
            ds.PropertiesToLoad.Add("sn");
            ds.PropertiesToLoad.Add("mail");
            // start searching
            SearchResultCollection searchCollection = ds.FindAll();

            try
            {
                foreach (SearchResult result in searchCollection)
                {
                    if (result.Properties.PropertyNames != null)
                        foreach (string propKey in result.Properties.PropertyNames)
                        {
                            //Display each of the values for the property identified by the property name.
                            foreach (object prop in result.Properties[propKey])
                            {
                                if ((propKey == "userPrincipalName"))
                                {
                                    return prop.ToString();
                                }
                            }
                        }
                }
                return "Unkown User";
            }
            catch (Exception ex)
            {
                return "Unknown User";
            }
        }

保存新用户

public void SaveUser(SearchRolesViewModel objSearchRolesViewModel, string userID)
        {
           DirectoryEntry de = new DirectoryEntry();
                de = QueryAD(objSearchRolesViewModel.NID);

            USERACCOUNT objUserAccount = new USERACCOUNT
            {

                HPID = Convert.ToInt32(objSearchRolesViewModel.NewUserHealthPlans),
                DOMAIN = "Aeth",
                NTUSERID = objSearchRolesViewModel.User_Id,
                ROLEID = Convert.ToInt32(objSearchRolesViewModel.UserRole),
                FIRSTNAME = GIVENNAME GOES HERE,
                LASTNAME = SURNAME GOES HERE,
                EMAIL = MAIL GOES HERE,
                ACTIVE = true/*Convert.ToBoolean(objSearchRolesViewModel.ActiveStatus)*/,
                DEFAULTPLANID = Convert.ToInt32(objSearchRolesViewModel.NewUserPrimaryHealthPlan),
                CREATEID = userID,
                CREATEDATE = DateTime.Now,
                UPDATEID = userID,
                UPDATEDATE = DateTime.Now
            };
            _context.USERACCOUNTs.Add(objUserAccount);
            _context.SaveChanges();

        }

我需要能够从活动目录访问属性,并将其添加到添加新用户时发送到数据库的属性。

c# active-directory
1个回答
0
投票

在您的代码中,QueryAD(objSearchRolesViewModel.NID);返回一个字符串,但是您将其分配给DirectoryEntity。这将无法工作。

public void SaveUser(SearchRolesViewModel objSearchRolesViewModel, string userID)
        {
           DirectoryEntry de = new DirectoryEntry();
                de = QueryAD(objSearchRolesViewModel.NID); // <--- This is the issue.
...

从QueryAD函数中查找DirectoryEntry并返回该对象以使您的调用正常工作。

public string QueryAD(string userNID) // You will need to return DirectoryEntry to make your code work.
        {
            DirectorySearcher ds = new DirectorySearcher
© www.soinside.com 2019 - 2024. All rights reserved.