我已经构建了一个应用程序,它将与ClickOnce一起部署,启动时需要检查当前登录用户的身份/名称,并将其与我们域上的Active Directory组进行比较,以便在应用程序中设置适当的权限。使用新的PrincipalContext(ContextType.Domain,“domain.name.com”)`而不必提供用户名和密码
我可以通过“硬编码”一个用户名和密码的应用程序,并通过这些凭据到PrincipalContext
构造,即
var pc = new PrincipalContext(ContextType.Domain, "domain.name.com", username, password);
的问题是,我不希望有这样做将用户名和密码存储在应用程序中(我不想提示用户输入用户名和密码)。
我见过很多使用构造函数的示例,但没有指定凭据,但这不适用于我们的域。
在域或“域用户”组属性中是否有设置/权限,我可以通过配置使其生效?
我当前的代码如下所示(请注意:
/// <summary>
/// Gets a list of all Active Directory Groups the specified username is a member of.
/// </summary>
/// <param name="userName">The username of the user in DOMAIN\Username format</param>
/// <param name="domain">The name of the domain server, e.g., server.domain.com (not DOMAIN or DOMAIN.COM or SERVER)</param>
/// <returns></returns>
public static List<string> GetUserGroups(string userName, string domain)
{
if (userName.IsNullOrWhiteSpace()) return null;
var pc = new PrincipalContext(ContextType.Domain, domain);
var userPc = UserPrincipal.FindByIdentity(pc, userName);
if (userPc == null) return null;
var src = userPc.GetGroups(pc);
var result = new List<string>();
src.ToList().ForEach(sr => result.Add(sr.SamAccountName));
return result;
}
而且我实质上调用是这样的:
var userGroups = GetUserGroups(WindowsIdentity.GetCurrent.Name, "server.domain.com");
“......这不适用于我们的网域。”你能解释一下吗?你是说你不能在没有用户名和密码的情况下查询LDAP/ADS服务器? – sfuqua 2015-02-11 22:42:45