1

Создал класс в модели для получения данных из AD

namespace NetJumper_03.Models
{
    public class ActiveDirectory
    {
        public string DisplayName { set; get; }
        public string SAMAccountName { set; get; }
        public string SN { set; get; }
        public string GivenName { set; get; }

        public string Company { set; get; }
        public string Department { set; get; }
        public string Title { set; get; }

        public string PhysicalDeliveryOfficeName { set; get; }
        public string Mail { set; get; }
        public string TelephoneNumber { set; get; }
        public string Mobile { set; get; }

        public static IEnumerable<ActiveDirectory> GetUserFromAD()
        {
            var newUser = new ActiveDirectory();
            using (var context = new PrincipalContext(ContextType.Domain, "office.local"))
            {
                using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
                {
                    foreach (var result in searcher.FindAll())
                    {


                        DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
                        newUser.DisplayName = (String)de.Properties["displayName"].Value;
                        newUser.SAMAccountName = (String)de.Properties["sAMAccountName"].Value;
                        newUser.SN = (String)de.Properties["sn"].Value;
                        newUser.GivenName = (String)de.Properties["givenName"].Value;
                        newUser.Company= (String)de.Properties["company"].Value;
                        newUser.Department = (String)de.Properties["department"].Value;
                        newUser.Title = (String)de.Properties["mobile"].Value;
                        newUser.Mail = (String)de.Properties["title"].Value;
                        newUser.TelephoneNumber = (String)de.Properties["physicalDeliveryOfficeName"].Value;
                        newUser.Mobile = (String)de.Properties["mail"].Value;

                    }
                }

                return (IEnumerable<ActiveDirectory>)newUser;
            }
        }



    }
}

В контроллере выглядит так

 public ActionResult Index()
        {
            return View(ActiveDirectory.GetUserFromAD());
        }

В виде вот так

@model IEnumerable<NetJumper_03.Models.ActiveDirectory>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@foreach(var items in Model)
{
    <p>@items.DisplayName</p>
}

Все бы ничего, но выдает ошибку

Additional information: Не удалось привести тип объекта "NetJumper_03.Models.ActiveDirectory" к типу "System.Collections.Generic.IEnumerable`1

Понимаю,что согрешил здесь return (IEnumerable<ActiveDirectory>)newUser;

Объясните как мне поступить?

1 ответ 1

1
    public static IEnumerable<ActiveDirectory> GetUserFromAD()
    {
        List<ActiveDirectory> result = new List<ActiveDirectory>()

        using (var context = new PrincipalContext(ContextType.Domain, "office.local"))
        {
            using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
            {
                foreach (var result in searcher.FindAll())
                {
                    var newUser = new ActiveDirectory();

                    DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
                    newUser.DisplayName = (String)de.Properties["displayName"].Value;
                    ...
                    newUser.Mobile = (String)de.Properties["mail"].Value;

                    result.Add(newUser);
                }
            }
        }

        return result;
    }

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.