如何使在iis中托管的wcf服务访问另一个服务器活动目录

时间:2013-08-25 07:00:07

标签: c# wcf security iis active-directory

如何使iis中托管的wcf服务访问另一个服务器活动目录

有2台服务器 1-在IIS上托管WCF服务的应用程序服务器 2-活动目录服务器 我想做的就是让这个WCF访问活动目录来添加,编辑或删除用户

如何使WCF服务访问同一网络中另一台服务器的AD 我正在使用Intranet门户网站,用户可以使用他们的Windows凭据“AD”登录,并希望开发一个管理页面以将用户添加到“AD”

在“AD”中创建用户的wcf服务没有权限这样做我怎么能这样做?

    public bool AddActiveDirectoryUser(ADUser User)
    {
        string userLoginName = User.Email.Split("@".ToArray())[0];
        // Creating the PrincipalContext
        PrincipalContext principalContext = null;
        try
        {
            principalContext = new PrincipalContext(ContextType.Domain, ADServer, ADPath.Substring(ADPath.IndexOf("DC")), ADUser, ADPassword);

        }
        catch (Exception e)
        {
            WriteLog(e);
            return false;
        }


        UserPrincipal usr = UserPrincipal.FindByIdentity(principalContext, userLoginName);
        if (usr != null)
        {
            WriteLog(Enum.LogType.Error, userLoginName + " already exists. Please use a different Username.");
            return false;
        }

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

        if (!string.IsNullOrEmpty(User.LastName) && User.LastName.Length > 0)
            userPrincipal.Surname = User.LastName;

        if (!string.IsNullOrEmpty(User.FirstName) && User.FirstName.Length > 0)
            userPrincipal.GivenName = User.FirstName;

        if (!string.IsNullOrEmpty(User.Email) && User.Email.Length > 0)
            userPrincipal.EmailAddress = User.Email;


        if (!string.IsNullOrEmpty(userLoginName) && userLoginName.Length > 0)
            userPrincipal.SamAccountName = userLoginName;

        userPrincipal.SetPassword("123456");

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

        try
        {
            userPrincipal.Save();

//这里抛出异常访问被拒绝!!!!?

        }
        catch (Exception e)
        {
            WriteLog(e);
            return false;
        }
        return true;
    }

1 个答案:

答案 0 :(得分:1)

好的,鉴于您提供的信息,问题如下。用于创建上下文的用户没有足够的权限来执行这些任务。您需要在创建用户的OU上向该用户授予权限,并且所有问题都应该消失。

查看此帖子,了解有关主题https://serverfault.com/questions/190566/what-permissions-are-needed-for-a-helpdesk-admin-to-create-users-in-ad

的更多信息