我需要能够访问值" _CustomUserID"来自应用程序内的其他控制器。我创建了一个类但不确定如何返回Controller要使用的值。
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];
}
}
}
答案 0 :(得分: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();
感谢希望这有帮助!