Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 手工创建模型_Asp.net Mvc_Active Directory - Fatal编程技术网

Asp.net mvc 手工创建模型

Asp.net mvc 手工创建模型,asp.net-mvc,active-directory,Asp.net Mvc,Active Directory,我需要能够从应用程序中的其他控制器访问值_CustomUserID。我创建了一个类,但不确定如何返回控制器要使用的值 using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; using System.Collections.Generic; using System.DirectoryServices.AccountManageme

我需要能够从应用程序中的其他控制器访问值_CustomUserID。我创建了一个类,但不确定如何返回控制器要使用的值

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Web.Security;
using System.DirectoryServices;

namespace myfirstapplication.Models
{
public static class ActiveDirectory
{
 public static UserPrincipal GetCustomID()
    {
      DirectoryEntry de = new DirectoryEntry();
      de.Path = "LDAP://10.1.1.1:389";
      DirectorySearcher deSearch = new DirectorySearcher(de);
      deSearch.PropertiesToLoad.Add("CustomUserID"); 
      deSearch.SearchScope = SearchScope.Subtree;

      deSearch.Filter = "(&(objectCategory=user)(SAMAccountName=" + Membership.GetUser().UserName + "))";
      SearchResult results = deSearch.FindOne();

      string _CustomUserID = (String)results.Properties["CustomUserID"][0];
    }
}
}

将方法签名更改为字符串,如下所示:

public static string GetCustomID()

{
DirectoryEntry de = new DirectoryEntry();
      de.Path = "LDAP://10.1.1.1:389";
      DirectorySearcher deSearch = new DirectorySearcher(de);
      deSearch.PropertiesToLoad.Add("CustomUserID"); 
      deSearch.SearchScope = SearchScope.Subtree;

      deSearch.Filter = "(&(objectCategory=user)(SAMAccountName=" + Membership.GetUser().UserName + "))";
      SearchResult results = deSearch.FindOne();

    Return (String)results.Properties["CustomUserID"][0];

}

//then in your controller all u have to say is :
string _customerId = ActiveDirectory.GetCustomID();

谢谢,希望这有帮助

工作得很漂亮!谢谢。另一个相关问题。如何传递多个属性?假设我还有Stringresults.Properties[CustomUserID][0]Stringresults.Properties[displayName][0]Stringresults.Properties[PostalAddress][0]谢谢您可以将方法签名更改为List ReturnCustomId,然后继续将其添加到您要返回的列表中!公共静态列表GetCustomID{ListreturnList=new List;..…相同的代码returnList.AddStringresults.Properties[CustomUserID][0];returnList.AddStringresults.Properties[CustomUserID][1]return returnreturnList;}//然后在控制器中,您所要说的就是:ListcustomeridSS=ActiveDirectory.GetCustomID;我的控制器似乎很高兴没有错误。但是,当我将其传递给类似以下列表的视图时,mylist=GetBannerIDfromAD.GetCustomID;ViewBag.myList=myList;返回视图列表;我收到这个信息。传递到字典的模型项的类型为System.Collections.Generic.List`1[System.String],但此字典需要System.Collections.Generic.IEnumerable类型的模型项显然是类型不匹配的,但无法确定如何显示它。请尝试使用return Viewmylist.ToList;