Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用System.DirectoryServices.AccountManagement创建的用户无法访问Sharepoint网站_C#_Sharepoint_Active Directory - Fatal编程技术网

C# 使用System.DirectoryServices.AccountManagement创建的用户无法访问Sharepoint网站

C# 使用System.DirectoryServices.AccountManagement创建的用户无法访问Sharepoint网站,c#,sharepoint,active-directory,C#,Sharepoint,Active Directory,相关代码:-这是一项正在进行的工作,我仍处于发现阶段,因此并非所有异常路径都已完成。不要再抱怨重新抛出异常了——我会开始的 我还必须说,我对广告的经验很少。我以前从未真正想到过广告 大多数代码来自Microsoft示例 public static bool AddUser(string firstName, string lastName, string userLogonName, string employeeID, string emailAddress, string telep

相关代码:-这是一项正在进行的工作,我仍处于发现阶段,因此并非所有异常路径都已完成。不要再抱怨重新抛出异常了——我会开始的

我还必须说,我对广告的经验很少。我以前从未真正想到过广告

大多数代码来自Microsoft示例

public static bool AddUser(string firstName, string lastName, string userLogonName,
    string employeeID, string emailAddress, string telephone, string address,
    string Password, DateTime expiry)
{
    PrincipalContext principalContext = GetContext();

    // Check if user object already exists in the store
    if (UserExists(userLogonName))
    {
        throw new Exception(userLogonName + " already exists. Please use a different User Logon Name.");
    }

    // Create the new UserPrincipal object
    UserPrincipal userPrincipal = new UserPrincipal(principalContext);

    if (lastName != null && lastName.Length > 0)
    {
        userPrincipal.Surname = lastName;
    }

    if (firstName != null && firstName.Length > 0)
    {
        userPrincipal.GivenName = firstName;
    }

    if (employeeID != null && employeeID.Length > 0)
    {
        userPrincipal.EmployeeId = employeeID;
    }

    if (emailAddress != null && emailAddress.Length > 0)
    {
        userPrincipal.EmailAddress = emailAddress;
    }

    if (telephone != null && telephone.Length > 0)
    {
        userPrincipal.VoiceTelephoneNumber = telephone;
    }

    if (userLogonName != null && userLogonName.Length > 0)
    {
        userPrincipal.SamAccountName = userLogonName;
    }

    userPrincipal.AccountExpirationDate = expiry;


    userPrincipal.SetPassword(Password);

    userPrincipal.Enabled = true;
    userPrincipal.PasswordNeverExpires = true;

    try
    {
        userPrincipal.Save();
    }
    catch (Exception e)
    {
        throw new Exception("Exception saving user object. ", e);
    }
    return true;
}

当我运行测试时,添加用户并检查Active Directory,用户就在那里。到目前为止还不错

然后我运行了“添加到组”测试,用户在广告中显示为组的成员。同样,一切都如预期的那样

现在,我导航到Sharepoint网站,并尝试以新创建的用户身份登录。很抱歉,此网站尚未与您共享

。。。插曲:对组和权限进行了大量的调侃,但都无济于事

接下来,我在AD中手动创建了一个用户,然后运行添加到组测试。广告中的一切看起来都很好,我可以成功登录到Sharepoint网站


所以,我怀疑AddUser方法有问题,但我不知道是什么。我看不出以编程方式创建的用户和手动创建的用户之间有什么区别。

正如我们在评论中提到的,在测试之前,请等待您的更改复制到所有域控制器

根据GetContext方法的编写方式,创建帐户时甚至可能会出现复制问题。如果它每次都创建一个新的PrincipalContext对象,理论上它可以在第二次连接到不同的DC,而新帐户还不存在。虽然它试图把你和最近的一个联系起来,所以它很可能永远都是同一个

为了避免获得不同的DC,您可以重用相同的PrincipalContext对象,也可以读取PrincipalContext的属性,该属性将告诉您最终使用的DC。然后,您可以在以后使用它来确保在同一个DC上进行所有更改

PrincipalContext的构造函数将允许您将特定DC作为域名传递,前提是您要将特定DC作为目标:

var context = new PrincipalContext(ContextType.Domain, "dc1.domain.com");

广告很挑剔。我将首先比较两个帐户的属性,然后导航到帐户所在的OU,并查看属性编辑器选项卡。如果您没有直接转到帐户所在的OU,并且没有打开高级功能,则不会显示此选项卡。我的猜测是,用AD GUI创建的一个正在设置一些您的代码没有设置的属性。是的,这也是我的怀疑。这只是一个弄清楚是什么的问题。如果您有多个域控制器,您是否在测试之前等待复制足够长的时间?半个小时就足够了,验证帐户是否被禁用,密码是否设置为下次登录时需要更改,以及帐户的到期日是否在将来。@GabrielLuci我想这就是答案。我昨天创建的帐户突然起作用了。看起来像答案,闻起来像答案…太棒了!我将写一个答案和一些避免复制问题的技巧。
var context = new PrincipalContext(ContextType.Domain, "dc1.domain.com");