C# 用于添加/编辑/删除active directory用户的MVC模型
以下代码:C# 用于添加/编辑/删除active directory用户的MVC模型,c#,asp.net-mvc,active-directory,C#,Asp.net Mvc,Active Directory,以下代码: public string AddADUser(string user_name, string password, string first_name, string last_name, string company_name) { try { List<ADUser> ADUsers = new List<ADUser>(); string admin_userName = "Administrator";
public string AddADUser(string user_name, string password, string first_name, string last_name, string company_name) {
try {
List<ADUser> ADUsers = new List<ADUser>();
string admin_userName = "Administrator";
string admin_password = "Password!";
string domain = "sneaky";
var context = new PrincipalContext(ContextType.Domain, domain, admin_userName, admin_password);
UserPrincipal NewUserPrincipal = new UserPrincipal(context, user_name, password, true);
NewUserPrincipal.UserPrincipalName = user_name;
NewUserPrincipal.SamAccountName = user_name;
// company NewUserPrincipal.GetUnderlyingObject.
NewUserPrincipal.GivenName = first_name;
NewUserPrincipal.Surname = last_name;
NewUserPrincipal.DisplayName = user_name;
NewUserPrincipal.Enabled = true;
NewUserPrincipal.Save();
return "User Saved Sucessfully";
} catch (Exception ex) {
return "Error saving user: \n" + ex.ToString();
}
}
public string AddADUser(字符串用户名、字符串密码、字符串名、字符串姓、字符串公司名){
试一试{
列表管理员=新列表();
字符串admin\u userName=“Administrator”;
字符串admin_password=“password!”;
string domain=“鬼鬼祟祟”;
var context=新的PrincipalContext(ContextType.Domain、Domain、admin\u用户名、admin\u密码);
UserPrincipal NewUserPrincipal=new UserPrincipal(上下文、用户名、密码、true);
NewUserPrincipal.UserPrincipalName=用户名;
NewUserPrincipal.SamAccountName=用户名;
//公司NewUserPrincipal.GetUnderlyingObject。
NewUserPrincipal.GivenName=第一个\u名称;
NewUserPrincipal.姓氏=姓氏;
NewUserPrincipal.DisplayName=用户名;
NewUserPrincipal.Enabled=true;
NewUserPrincipal.Save();
返回“用户保存成功”;
}捕获(例外情况除外){
返回“保存用户时出错:\n”+ex.ToString();
}
}
产生以下错误:
保存用户时出错:System.DirectoryServices.AccountManagement.PrincipalOperationException:RPC服务器不可用。(来自HRESULT:0x800706BA的异常)--->System.Runtime.InteropServices.COMException:RPC服务器不可用。(来自HRESULT:0x800706BA的异常)---内部异常堆栈跟踪的结尾---System.DirectoryServices.AccountManagement.ADStoreCtx.Insert(主体p)位于System.DirectoryServices.AccountManagement.Principal.Save()位于project.Models.UserBAL.AddAdudUser(字符串用户名、字符串密码、字符串首字母、字符串姓氏、字符串公司名称)在C:\Users\skillyguy\Desktop\project\project\Models\UserBAL.cs中:第107行
wierd的事情是,我得到了另一个用户的访问拒绝错误,我想他可能没有权限创建用户。。。所以我尝试了管理员帐户,现在我得到了这个错误。我知道最终我需要这样做,以便执行操作的用户必须具有在active directory中创建/编辑和删除用户的权限。。。我也需要知道如何在广告中设置这些权限。我还需要为用户设置“公司”字段。
RPC服务器不可用。
错误消息表明您的VS开发计算机在联系域控制器添加用户时遇到困难。是的。我尝试了另一个user/pass,它以前给出了拒绝访问错误,现在它给出了相同的RPC错误。。。我不知道发生了什么变化,我改变了用户和密码,然后我开始出现这个错误。我知道我可以ping服务器。另外,另一个获取系统用户的代码区域正在工作,因此它正在与AD进行通信。。。只有在我尝试设置新用户时才会产生此错误。我不确定代码是否正确。我在visual studio服务器资源管理器中添加了AD svr。。。但它仍然产生相同的错误。关闭广告防火墙,没有改变。