我的意思是,现在我正在使用System.DirectoryServices.AccountManagement,如果我使用UserPrincipal类,我只看到名称,中间名等
所以在我的代码中它就像
UserPrincipal myUser = new UserPrincipal(pc);
myUser.Name = "aaaaaa";
myUser.SamAccountName = "aaaaaaa";
.
.
.
.
myUser.Save();
Run Code Online (Sandbox Code Playgroud)
我怎样才能看到像手机或信息这样的属性?
我有一个使用ActiveDirecotry授权的应用程序,并且已经确定它需要支持嵌套的AD组,例如:
MAIN_AD_GROUP
|
|-> SUB_GROUP
|
|-> User
Run Code Online (Sandbox Code Playgroud)
所以,用户不是直接成员MAIN_AD_GROUP.我希望能够递归地查找用户,搜索嵌套的组MAIN_AD_GROUP.
主要问题是我使用的是.NET 3.5,并且.NET 3.5中存在一个错误System.DirectoryServices.AccountManagement,该方法UserPrincipal.IsMemberOf() 不适用于拥有超过1500个用户的组.所以我不能使用UserPrincipal.IsMemberOf()和不,我也无法切换到.NET 4.
我用以下函数解决了这个最后一个问题:
private bool IsMember(Principal userPrincipal, Principal groupPrincipal)
{
using (var groups = userPrincipal.GetGroups())
{
var isMember = groups.Any(g =>
g.DistinguishedName == groupPrincipal.DistinguishedName);
return isMember;
}
}
Run Code Online (Sandbox Code Playgroud)
但userPrincipal.GetGroups()只返回用户是其直接成员的组.
如何让它与嵌套组一起使用?
任何人都可以提供如何循环通过System.DirectoryServices.PropertyCollection并输出属性名称和值的示例?
我正在使用C#.
@JaredPar - PropertyCollection没有Name/Value属性.它有一个PropertyNames和Values,类型为System.Collection.ICollection.我不知道构成PropertyCollection对象的基线对象类型.
@JaredPar再次 - 我最初错误地标记了错误类型的问题.那是我的坏事.
更新: 基于Zhaph - Ben Duguid输入,我能够开发以下代码.
using System.Collections;
using System.DirectoryServices;
public void DisplayValue(DirectoryEntry de)
{
if(de.Children != null)
{
foreach(DirectoryEntry child in de.Children)
{
PropertyCollection pc = child.Properties;
IDictionaryEnumerator ide = pc.GetEnumerator();
ide.Reset();
while(ide.MoveNext())
{
PropertyValueCollection pvc = ide.Entry.Value as PropertyValueCollection;
Console.WriteLine(string.Format("Name: {0}", ide.Entry.Key.ToString()));
Console.WriteLine(string.Format("Value: {0}", pvc.Value));
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试使用PrincipalContext类通过Active Directory服务进行身份验证.我想让我的应用程序使用密封和SSL上下文对域进行身份验证.为了做到这一点,我必须使用PrincipalContext的以下构造函数(链接到MSDN页面):
public PrincipalContext(
ContextType contextType,
string name,
string container,
ContextOptions options
)
Run Code Online (Sandbox Code Playgroud)
具体来说,我正在使用构造函数:
PrincipalContext domainContext = new PrincipalContext(
ContextType.Domain,
domain,
container,
ContextOptions.Sealing | ContextOptions.SecureSocketLayer);
Run Code Online (Sandbox Code Playgroud)
MSDN说"容器":
商店中的容器用作上下文的根.所有查询都在此根下执行,所有插入都将在此容器中执行.对于Domain和ApplicationDirectory上下文类型,此参数是容器对象的可分辨名称(DN).
容器对象的DN是多少?如何找出容器对象是什么?我可以查询Active Directory(或LDAP)服务器吗?
我正在尝试使用以下代码获取用户的所有Active Directory组:
private static IEnumerable<string> GetGroupNames(string userName)
{
using (var context = new PrincipalContext(ContextType.Domain))
{
using (var userPrincipal = UserPrincipal.FindByIdentity(context, userName))
{
var groupSearch = userPrincipal.GetGroups(context);
var result = new List<string>();
foreach (var principal in groupSearch)
{
Log.LogDebug("User {0} is member of group {0}", userPrincipal.DisplayName, principal.DisplayName);
result.Add(principal.SamAccountName);
}
return result;
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码正确查找用户主体,但在使用PrincipalOperationException调用GetGroups时失败:未知错误(0x80005000).
根异常:
at System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOf(Principal foreignPrincipal, StoreCtx foreignContext)
at System.DirectoryServices.AccountManagement.Principal.GetGroupsHelper(PrincipalContext contextToQuery)
at System.DirectoryServices.AccountManagement.Principal.GetGroups(PrincipalContext contextToQuery)
at [line of the GetGroup call]
Run Code Online (Sandbox Code Playgroud)
内部异常(COMException):
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject() …Run Code Online (Sandbox Code Playgroud) 以下代码可以在我们域上的各种计算机上正常运行.
var context = new PrincipalContext(ContextType.Domain);
var principal = UserPrincipal.FindByIdentity(context, @"domain\username")
Run Code Online (Sandbox Code Playgroud)
但是,如果我在不在域上的计算机上运行此类似的代码,它可以工作,但FindByIdentity行需要2秒以上.
var context = new PrincipalContext(ContextType.Machine);
var principal = UserPrincipal.FindByIdentity(context, @"machinename\username")
Run Code Online (Sandbox Code Playgroud)
通过向PrincipalContext构造函数和/或FindByIdentity方法提供特殊参数,可以解决这种性能差异吗?IIS或Windows中是否有可以调整的设置?
至少,任何人都可以告诉我为什么在第二种情况下它可能会变慢?
该代码从Windows Server 2008 R2上的IIS 7.5(集成管道)中托管的ASP.NET MVC 3应用程序运行.
题:
我使用http://support.microsoft.com/kb/306273上的代码
添加Windows用户.问题是我需要将用户添加到组,但组名已本地化.
例如,MS-example使用英文计算机,这意味着您可以像这样获取访客组:grp = AD.Children.Find("Guests","group")
但是在非英语计算机上,"访客"组名是本地化的,例如在我的德语操作系统上,来宾的组名是"Gäste".
这意味着支持示例在我的计算机上运行我需要将该行更改为grp = AD.Children.Find("Gäste","group")
然后它工作.
现在,如果操作系统是任何其他语言,我如何找到访客用户的名称?或者如何从sid获取来宾用户名?
注意:.NET 2.0,而不是3.0或3.5
我在网站上收到错误,我使用Windows身份验证.
奇怪的事情:
这是我在日志邮件中得到的:
来源:System.DirectoryServices
消息:服务器无法运行.
跟踪:
在System.DirectoryServices.DirectoryEntry.Bind(布尔throwIfFail)
在System.DirectoryServices.DirectoryEntry.Bind()
在System.DirectoryServices.DirectoryEntry.get_AdsObject()
在System.DirectoryServices.DirectorySearcher.FindAll(布尔findMoreThanOne)
在的System.DirectoryServices
.DirectorySearcher.FindOne ()在Smarthouse.Labs.DataAccess.UserListManager.SaveUser(String windowsUserName)
这是我实现DirectorySearch的方式:
private void SaveUser(string windowsUserName)
{
string[] domainAndUser = windowsUserName.Split('\\');
string domain = domainAndUser[0];
string username = domainAndUser[1];
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain);
DirectorySearcher search = new DirectorySearcher(entry);
try
{
// Bind to the native AdsObject to force authentication.
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("givenName");
search.PropertiesToLoad.Add("mail");
SearchResult result = search.FindOne();
if (result == null)
{
throw new Exception("No …Run Code Online (Sandbox Code Playgroud) asp.net directoryservices active-directory windows-authentication
CS0234:名称空间"系统"中不存在类型或命名空间名称"DirectoryServices"(您是否缺少程序集引用?)
此页面工作正常,直接显示来自服务的记录,没有错误.但现在它给出了上述错误.
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false" DataSourceID="odsUsers"
AllowPaging="true" AllowSorting="true" Width="100%">
<Columns>
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<%#((System.DirectoryServices.DirectoryEntry)Container.DataItem).Properties["userPrincipalName"].Value%>
</ItemTemplate>
</asp:TemplateField>
/Columns>
</asp:GridView>
Run Code Online (Sandbox Code Playgroud)
项目构建成功,但是当我打开页面时,它会出错
我正在尝试使用.NET 3.5 System.DirectoryServices.AccountManagement命名空间通过SSL加密的LDAP连接验证针对我们的Active Directory LDAP服务器的用户凭据.这是示例代码:
using (var pc = new PrincipalContext(ContextType.Domain, "sd.example.com:389", "DC=sd,DC=example,DC=com", ContextOptions.Negotiate))
{
return pc.ValidateCredentials(_username, _password);
}
Run Code Online (Sandbox Code Playgroud)
此代码在不安全的LDAP(端口389)上工作正常,但我宁愿不以明文形式传输用户/传递组合.但是,当我更改为LDAP + SSL(端口636)时,我得到以下异常:
System.DirectoryServices.Protocols.DirectoryOperationException: The server cannot handle directory requests.
at System.DirectoryServices.Protocols.ErrorChecking.CheckAndSetLdapError(Int32 error)
at System.DirectoryServices.Protocols.LdapSessionOptions.FastConcurrentBind()
at System.DirectoryServices.AccountManagement.CredentialValidator.BindLdap(NetworkCredential creds, ContextOptions contextOptions)
at System.DirectoryServices.AccountManagement.CredentialValidator.Validate(String userName, String password)
at System.DirectoryServices.AccountManagement.PrincipalContext.ValidateCredentials(String userName, String password)
at (my code)
Run Code Online (Sandbox Code Playgroud)
端口636适用于其他活动,例如查找该LDAP/AD条目的非密码信息...
UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, _username)
Run Code Online (Sandbox Code Playgroud)
...所以我知道这不是我的LDAP服务器的SSL设置,因为它适用于其他查找的SSL.
有没有人接到ValidateCredentials(...)通过SSL工作的电话?你能解释一下吗?或者是否有另一种/更好的方法可以安全地验证AD/LDAP凭据?