Asp.net mvc 手工创建模型
我需要能够从应用程序中的其他控制器访问值_CustomUserID。我创建了一个类,但不确定如何返回控制器要使用的值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
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{ListreturnList=new List;..…相同的代码returnList.AddStringresults.Properties[CustomUserID][0];returnList.AddStringresults.Properties[CustomUserID][1]return returnreturnList;}//然后在控制器中,您所要说的就是:ListcustomeridSS=ActiveDirectory.GetCustomID;我的控制器似乎很高兴没有错误。但是,当我将其传递给类似以下列表的视图时,mylist=GetBannerIDfromAD.GetCustomID;ViewBag.myList=myList;返回视图列表;我收到这个信息。传递到字典的模型项的类型为System.Collections.Generic.List`1[System.String],但此字典需要System.Collections.Generic.IEnumerable类型的模型项显然是类型不匹配的,但无法确定如何显示它。请尝试使用return Viewmylist.ToList;