[英]How I can create a User in AD with Powershell in a ASP.NET Webapplication
[英]Create AD User from asp.net
我有代碼可以直接在Active Directory中為ADFS創建用戶我的示例代碼-
PrincipalContext principalContext = null;
try
{
principalContext = new PrincipalContext(ContextType.Domain);
UserPrincipal usr = UserPrincipal.FindByIdentity(principalContext, txt_username.Text);
if (usr != null)
{
MessageBox.Show(txt_username.Text + " already exists. Please use a different User Logon Name.");
}
else
{
UserPrincipal userPrincipal = new UserPrincipal(principalContext);
userPrincipal.Surname = txt_lastname.Text;
userPrincipal.GivenName = txt_firstname.Text;
userPrincipal.EmailAddress = txt_email.Text;
userPrincipal.UserPrincipalName = txt_username.Text + "@ad.net";
userPrincipal.SamAccountName = txt_username.Text;
userPrincipal.DisplayName = txt_lastname.Text + " " + txt_firstname.Text;
userPrincipal.SetPassword(txt_pwd.Text);
userPrincipal.Enabled = true;
userPrincipal.PasswordNeverExpires = true;
userPrincipal.Save();
MessageBox.Show("user Created Sucessfully");
}
}
catch (Exception ex)
{
MessageBox.Show("Failed to create PrincipalContext. Exception: " + ex);
}
它與窗口應用程序的工作正常,但如果我將相同的應用程序放在asp.net中,則會拋出錯誤-
userPrincipal異常:訪問被拒絕
任何建議
謝謝
這意味着用於向Active Directory進行身份驗證的帳戶沒有創建該帳戶的權限。
除非您另外指定,否則用於運行ASP.NET應用程序的帳戶是由IIS創建的,並且除了其所運行的服務器之外沒有任何權限。 您有兩種選擇:
PrincipalContext
使用不同的構造器,以接受用戶名和密碼進行身份驗證,並使用具有創建帳戶權限的憑據: principalContext = new PrincipalContext(ContextType.Domain, null, "DOMAIN\username", "password");
在此行中構造PrincipalContext時, principalContext = new PrincipalContext(ContextType.Domain);
確保傳遞其他參數,例如AD服務器名稱,用戶名,密碼等。它應該解決所有權限問題。 使用的用戶名和密碼應具有在AD中創建用戶的權限。 例如: principalContext = new PrincipalContext(ContextType.Domain, ADSERVER, $"OU=New Users,DC=MYDOMAIN,DC=CO,DC=UK", USERNAME, PASSWORD);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.