![](/img/trans.png)
[英]How to use one table for two different contexts in two different domains?
[英]Using two different domains for validation
我目前正在為一個新項目進行驗證步驟。 除了這部分(如下所述)之外,所有代碼都在工作。 我正在嘗試檢查用戶(域 1)是否在域 2 的組中。 我正在嘗試的一切都是為用戶返回一個空值,我不知道為什么。
這是我正在使用的功能。
public void runTask(string serverName, string taskName)
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, loginWindow.getUsername());
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, ADGroup);
if (user.IsMemberOf(group))
{
using (TaskService tasksrv = new TaskService(serverName, loginWindow.getUsername(), domain, loginWindow.getPassword()))
{
Microsoft.Win32.TaskScheduler.Task task = tasksrv.FindTask(taskName);
if (!task.IsActive)
{
if (task.Enabled == true)
{
//task.Run();
}
else
{
log.LogMessage("Task was disabled. Enabling...");
task.Enabled = true;
//task.Run();
}
}
else
{
// send email
string body = "User: " + loginWindow.getUsername().ToUpper() + "<br/><br/>" + "Server: " + serverName + "<br/><br/>" + task.Name + " is already running.";
// send email confirmation
email = new Email(false);
email.SendMail(toEmail, fromEmail, serverName + subject, body);
}
}
}
}
當我在用戶名存在的地方設置 domain = 時,它返回一個值但找不到 ADGroup。 當我切換到ADGroup的域時,用戶為空。
loginWindow 是一個簡單的類,用於驗證用戶憑據。 任務是我試圖運行的 Windows 任務。
任何想法或提示都會有所幫助,因為現在我不知所措。
謝謝!
好吧,從代碼看來,您正在使用相同的域范圍PrincipalContext對象來查找用戶和組,並且由於它們實際上位於不同的域中,因此至少應該使用自己的上下文執行每個查找:
PrincipalContext userCtx = new PrincipalContext(ContextType.Domain, domain1);
UserPrincipal user = UserPrincipal.FindByIdentity(userCtx, loginWindow.getUsername());
PrincipalContext groupCtx = new PrincipalContext(ContextType.Domain, domain2);
GroupPrincipal group = GroupPrincipal.FindByIdentity(groupCtx, ADGroup);
這應該為您提供user和group 的非空結果。 不過,我不確定user.IsMemberOf(group)
是否會起作用。 Active Directory 使用foreignSecurityPrincipal記錄來保存跨域組成員身份的記錄,並且它僅使用組的成員屬性。 用戶記錄未獲得匹配的memberOf記錄。 有關詳細信息,請參閱我的答案here 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.