asp.net操作AD的问题

72 篇文章 0 订阅
41 篇文章 0 订阅
1、

在ASP.NET中, 启用Windows集成验证,这样我们就有了登陆网页的用户名和密码的哈希版本,但是我们是不能直接使用的,因为是密码的哈希版本,而不是密码本身。

比如:

System.DirectoryServices.DirectoryEntry de = new System.DirectoryServices.DirectoryEntry(
             
"LDAP://22.11.21.232:389/OU=ou0000_team,OU=ou0000_unit,OU=ou0000_division,DC=bocadtest,DC=com",

                "administrator""admin",System.DirectoryServices.AuthenticationTypes.Secure);

 我们不能使用此构造函数来传入用户名和密码,所以只能把此ASP.NET运行线程的安全上下文传递过去,这样的话,我们需要在Web.config中,加入下面的标记:

    <authentication mode="Windows" />
    <identity impersonate="true"/>   


这样的话,就可以把安全上下文传递下去了,使用下面的构造函数:

 

System.DirectoryServices.DirectoryEntry de = new System.DirectoryServices.DirectoryEntry(
                
"LDAP://22.11.21.232:389/OU=ou0000_team,OU=ou0000_unit,OU=ou0000_division,DC=bocadtest,DC=com");

这样就可以用用户的权限来操作active directory了。当然如果使用带用户名密码的构造函数也可以,因为用户名和密码已经不起作用。


2、修改密码时,

修改密码应为:

var directoryEntry = new DirectoryEntry(fullPath , adminUser, adminPassword, AuthenticationTypes.Secure);

directoryEntry.Invoke("ChangePassword", new object[] {"alvin_yang","P@ssW0rd1" });
directoryEntry.CommitChanges();

directoryEntry.Close();

必须设置帐号和密码,必须具有管理员权限,原密码也必须设置正确,新密码要满足密码强度策略,不然会出现“出现一个约束冲突”错误。

后来又出现各种报错:“调用的目标发生了异常”、“出现一个约束冲突”等等,我X

下面的出现“出现一个约束冲突”

directoryEntry.Invoke("ChangePassword", new object[] { txtOldPwd.Text, txtNewPwd.Text });
directoryEntry.CommitChanges();
 directoryEntry.Close();

下面的又可以:

directoryEntry.Invoke("SetPassword", new object[] { txtNewPwd.Text });
directoryEntry.CommitChanges();
 directoryEntry.Close();

尼玛昨天SetPassword不行,ChangePassword可以,今天又反过来了。

“调用的目标发生了异常”:密码强度不够;

出现一个约束冲突:英文缩写超过6个字符也会出现这个错误;

3、启用用户出现如下错误:

该服务器不愿意处理该请求。

说明:执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.DirectoryServices.DirectoryServicesCOMException: 该服务器不愿意处理该请求。

错误原因:启用代码发错位置了,应该在设置密码之后

错误代码

DirectoryEntry usr = ou.Children.Add("CN=" + commonName, "user");
//usr.Properties["userAccountControl"].Value = 512 | 66048;//这里出现异常:该服务器不愿意处理该请求        
usr.Properties["samAccountName"].Value = sAMAccountName;
usr.Properties["telephoneNumber"].Value = "15014088110";
usr.Properties["streetAddress"].Value = "深圳南山";
usr.CommitChanges();
//1.
  usr.Invoke("ChangePassword", new object[] {null, "P@ssw0rd" });   
//2.
usr.Invoke("SetPassword", new object[] { "P@ssw0rd" });
usr.CommitChanges();

正确代码

DirectoryEntry usr = ou.Children.Add("CN=" + commonName, "user");
usr.Properties["samAccountName"].Value = sAMAccountName;
usr.Properties["telephoneNumber"].Value = "15014088110";
usr.Properties["streetAddress"].Value = "深圳南山";
usr.CommitChanges();
//1.
//usr.Invoke("ChangePassword", new object[] {null, "P@ssw0rd" });   
//2.
usr.Invoke("SetPassword", new object[] { "P@ssw0rd" });
usr.Properties["userAccountControl"].Value = 512 | 66048;
 usr.CommitChanges();
4、添加用户时,设置密码有两种方式:
DirectoryEntry usr = ou.Children.Add("CN=" + commonName, "user");   
usr.Properties["samAccountName"].Value = sAMAccountName;
usr.Properties["telephoneNumber"].Value = "15014088110";
usr.Properties["streetAddress"].Value = "深圳南山";
usr.CommitChanges();
//1.
//usr.Invoke("ChangePassword", new object[] {null, "P@ssw0rd" });   
//2.
usr.Invoke("SetPassword", new object[] { "P@ssw0rd" });
usr.CommitChanges();

5.未指定的错误
LDAP 路径缺少 LDAP 协议标识符。确保路径中包含 LDAP 协议标识符以大写形式
6.拒绝访问
7、指定的目录对象无法与远程资源链接
8、出现了一个操作错误
异常详细信息: System.DirectoryServices.DirectoryServicesCOMException: 出现了一个操作错误。
IIS跟AD服务器不在同一个域

9、未经授权:访问由于凭据无效被拒绝。 
解决方法1:IIS-身份验证-匿名身份验证使用特定用户(具有管理员权限);
解决方法2:IIS-身份验证-启用基本身份验证,这样进行操作时会弹出框提示数据帐号、密码(具有管理员权限)
解决方法3:新建应用程序池,右键新建的应用程序池-属性,进程模型-标识-自定义账户,设置具有管理员权限的用户,IS-身份验证-匿名身份验证-启用应用程序池标识
第三种方法最好使,其他站点身份验证匿名身份验证直接启用引用程序池标识。
第6、8点问题应该也是这种解决方法。

10、添加用户时:该服务器不愿意处理该请求

当 只勾选密码永不过期时,设置密码应在设置userAccountControl、pwdLastSet属性前。

11、AD的密码策略(大于6-7个字符,字母、数字、特殊字符组的组合)







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET 域自动登录(AD自动登录)可以让用户在访问Web应用程序时无需再次输入他们的凭据。它允许用户在他们的计算机上通过Windows身份验证登录,并自动通过ASP.NET应用程序进行身份验证。 以下是实现ASP.NET域自动登录(AD自动登录)的步骤: 1. 在Web.config文件中启用Windows身份验证: ``` <authentication mode="Windows" /> ``` 2. 在Global.asax文件中添加以下代码: ``` protected void Application_AuthenticateRequest(Object sender, EventArgs e) { if (HttpContext.Current.User != null) { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.User.Identity is WindowsIdentity) { // Get the Windows identity. var windowsIdentity = (WindowsIdentity)HttpContext.Current.User.Identity; // Get the user name and domain name. var userName = windowsIdentity.Name; var domainName = windowsIdentity.Name.Split('\\')[0]; // Authenticate the user against Active Directory. if (AuthenticateUser(userName, domainName)) { // Create a new identity using the user name and domain name. var identity = new GenericIdentity(userName, "Windows"); // Get the roles for the user from Active Directory. var roles = GetRolesForUser(userName, domainName); // Attach the roles to the identity. HttpContext.Current.User = new GenericPrincipal(identity, roles); } else { // Redirect the user to the login page. FormsAuthentication.RedirectToLoginPage(); } } } } } private bool AuthenticateUser(string userName, string domainName) { // Authenticate the user against Active Directory. // Return true if the user is authenticated, false otherwise. } private string[] GetRolesForUser(string userName, string domainName) { // Get the roles for the user from Active Directory. // Return an array of role names. } ``` 3. 在Web应用程序中创建一个登录页面。 4. 在登录页面中添加以下代码: ``` protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.IsAuthenticated) { // Redirect the user to the default page. Response.Redirect("~/Default.aspx"); } } } protected void btnLogin_Click(object sender, EventArgs e) { // Authenticate the user against Active Directory. if (AuthenticateUser(txtUserName.Text, txtPassword.Text)) { // Redirect the user to the default page. FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false); } else { // Display an error message. lblErrorMessage.Text = "Invalid username or password."; } } private bool AuthenticateUser(string userName, string password) { // Authenticate the user against Active Directory. // Return true if the user is authenticated, false otherwise. } ``` 这些步骤将帮助您实现ASP.NET域自动登录(AD自动登录)。当用户访问您的Web应用程序时,他们将通过Windows身份验证自动登录,并自动通过ASP.NET应用程序进行身份验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值