本文介绍了设置密码在.NET 2.0 Active Directory轻型目录服务(AD LDS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个新用户,并使用asp.net VB AD LDS中设置自己的密码。我绑定到一个目录项,这是工作的罚款的实例。我可以添加一个用户没有问题。问题是,我似乎不能当我添加用户设置的密码。这是正确的方式来设置密码?

 昏暗objADAM作为的DirectoryEntry = BindToInstance()

昏暗objUser作为的DirectoryEntry = objADAM.Children.Add(CN =吉米,用户)
objUser.Properties(SN)。值=劳合社
objUser.Properties(给定名称)。值=吉米·史密斯
objUser.Properties(的userpassword)。值=THEPASSWORD
objUser.CommitChanges()
 

这是我得到的错误:

我也试过这样的:

 昏暗objADAM作为的DirectoryEntry = BindToInstance()

昏暗objUser作为的DirectoryEntry = objADAM.Children.Add(CN =吉米,用户)
objUser.Properties(SN)。值=劳合社
objUser.Properties(给定名称)。值=吉米·史密斯
objUser.CommitChanges()
objUser.Invoke(SetPassword,新的对象(){123456789A $#})
objUser.CommitChanges()
 

这给了我这个错误:

解决方案

我的同事找到了解决办法。调用CreateUserSetPassword创建用户和设置于一个函数调用的密码。

仅供参考,如果设置密码失败,用户就已经被设置了,所以你需要删除用户或只是再次调用SetPassword函数。

类变量

 私人URI作为字符串
    ' { 得到;组; }
    私人OuUri作为字符串
    ' { 得到;组;}
    私人UserUri作为字符串
    ' { 得到;组; }
    你会想在某个地方的.config设置这两个参数,并传递给
    或者以其他方式提供给这一进程
    私人用户ID作为字符串=danny123
    私人PWD作为字符串=啪$$字1
 

新功能

 的Public Sub New(BYVAL uri__1作为字符串,BYVAL欧作为字符串)
    乌里= uri__1
    OuUri =LDAP://&放大器; uri__1和放大器; /和放大器;欧
    UserUri =LDAP://&放大器; uri__1和放大器; / CN = {0},&放大器;欧
结束小组
 

CreateUserSetPassword

 '''<总结>
'''创建新用户并设置密码
'''< /总结>
'''< PARAM NAME =username的>< /参数>
'''< PARAM NAME =密码>< /参数>
公共职能CreateUserSetPassword(BYVAL用户名作为字符串,BYVAL密码作为字符串)作为字符串
    昏暗oGUID作为字符串=的String.Empty
    oGUID = CreateUserAccount(用户名,密码)
    如果oGUID =的String.Empty然后
        oGUID = SetPassword(用户名,密码)
        如果oGUID =的String.Empty然后
            oGUID = EnableUser(用户名)
        结束如果
    结束如果
    返回oGUID
端功能
 

CreateUserAccount

 '''<总结>
'''创建用户的AD-LDS
'''< /总结>
'''< PARAM NAME =username的>< /参数>
'''< PARAM NAME =的userPassword>< /参数>
'''<返回>< /回报>
公共职能CreateUserAccount(BYVAL用户名作为字符串,BYVAL的userPassword作为字符串)作为字符串
    昏暗oGUID作为字符串=的String.Empty
    尝试
        昏暗的连接preFIX的String = OuUri
        使用dirEntry作为新的DirectoryEntry(连接preFIX,用户ID,PWD)
            昏暗新用户作为的DirectoryEntry = dirEntry.Children.Add(CN =&放大器;用户名,用户)
            newUser.Properties(的UserPrincipalName)。值=用户名
            newUser.CommitChanges()
            newUser.Close()

        结束使用
        赶上(System.DirectoryServices.DirectoryServicesCOMException E)
    卡子E作为例外
        oGUID = E.Message
    结束尝试
    返回oGUID
端功能
 

SetPassword

 '''<总结>
'''设置密码的AD-LDS用户
'''< /总结>
'''< PARAM NAME =用户>< /参数>
'''< PARAM NAME =密码>< /参数>
公共功能SetPassword(BYVAL用户作为字符串,BYVAL密码作为字符串)作为字符串
    昏暗oGUID作为字符串=的String.Empty
    常量adsOptionPasswordPortnumber只要= 6
    常量adsOptionPasswordMethod只要= 7
    常量adsPasswordEn codeClear作为整数= 1

    常量intPort作为整数= 50000
    昏暗objUser作为的DirectoryEntry
    用户对象。
    设置身份验证标志。
    昏暗的使用authTypes作为AuthenticationTypes = AuthenticationTypes.Signing或者AuthenticationTypes.Sealing或者AuthenticationTypes.Secure

    使用LDAP端口绑定到用户对象。
    尝试
        objUser =新的DirectoryEntry(的String.Format(UserUri,用户),用户ID,PWD,使用authTypes)
        使用GetDirectoryEntry获取对象
        'objUser = GetDirectoryEntry(用户);
        objUser.RefreshCache()

        objUser.Invoke(的SetOption,新的对象(){adsOptionPasswordPortnumber,intPort})
        objUser.Invoke(的SetOption,新的对象(){adsOptionPasswordMethod,adsPasswordEn codeClear})
        objUser.Invoke(SetPassword,新的对象(){}密码)
        objUser.CommitChanges()
    卡子E作为例外
        oGUID = e.Message和放大器; vbLf&安培; VBCR和放大器; Convert.ToString(e.InnerException)
    结束尝试
    返回oGUID
端功能
 

I am trying to create a new user and set their password in AD LDS using asp.net vb. I'm binding to an instance of a directory entry, which is working fine. And I can add a user without a problem. The problem is that I can't seem to set the password when I add the user. Is this the right way to set the password?

Dim objADAM As DirectoryEntry = BindToInstance()

Dim objUser As DirectoryEntry = objADAM.Children.Add("CN=Jimmy", "User")
objUser.Properties("sn").Value = "lloyd"
objUser.Properties("givenName").Value = "Jimmy Smith"
objUser.Properties("userpassword").Value = "THEPASSWORD"
objUser.CommitChanges()

This is the error that I get :

I've also tried this :

Dim objADAM As DirectoryEntry = BindToInstance()

Dim objUser As DirectoryEntry = objADAM.Children.Add("CN=Jimmy", "User")
objUser.Properties("sn").Value = "lloyd"
objUser.Properties("givenName").Value = "Jimmy Smith"
objUser.CommitChanges()
objUser.Invoke("SetPassword", New Object() {"123456789A$#"})
objUser.CommitChanges()

Which gave me this error :

解决方案

My coworker found a solution. You call CreateUserSetPassword to create the user and setup the password in one function call.

FYI, if the set password fails, the user will already be set up, so you'll need to either delete the user or just call the SetPassword function again.

Class variables

   Private Uri As String
    ' { get; set; }
    Private OuUri As String
    ' { get; set;}
    Private UserUri As String
    ' { get; set; }
    'You will want to set these two parameters somewhere in .config and pass to
    'or otherwise make available to this process
    Private userid As String = "danny123"
    Private pwd As String = "pa$$word1"

New function

Public Sub New(ByVal uri__1 As String, ByVal ou As String)
    Uri = uri__1
    OuUri = "LDAP://" & uri__1 & "/" & ou
    UserUri = "LDAP://" & uri__1 & "/CN={0}," & ou
End Sub

CreateUserSetPassword

''' <summary>
''' Creates new user and sets password
''' </summary>
''' <param name="userName"></param>
''' <param name="password"></param>
Public Function CreateUserSetPassword(ByVal userName As String, ByVal password As String) As String
    Dim oGUID As String = String.Empty
    oGUID = CreateUserAccount(userName, password)
    If oGUID = String.Empty Then
        oGUID = SetPassword(userName, password)
        If oGUID = String.Empty Then
            oGUID = EnableUser(userName)
        End If
    End If
    Return oGUID
End Function

CreateUserAccount

''' <summary>
''' Create user in the AD-LDS
''' </summary>
''' <param name="userName"></param>
''' <param name="userPassword"></param>
''' <returns></returns>
Public Function CreateUserAccount(ByVal userName As String, ByVal userPassword As String) As String
    Dim oGUID As String = String.Empty
    Try
        Dim connectionPrefix As String = OuUri
        Using dirEntry As New DirectoryEntry(connectionPrefix, userid, pwd)
            Dim newUser As DirectoryEntry = dirEntry.Children.Add("CN=" & userName, "user")
            newUser.Properties("userPrincipalName").Value = userName
            newUser.CommitChanges()
            newUser.Close()

        End Using
        'catch (System.DirectoryServices.DirectoryServicesCOMException E)
    Catch E As Exception
        oGUID = E.Message
    End Try
    Return oGUID
End Function

SetPassword

''' <summary>
''' Set password for the user in AD-LDS
''' </summary>
''' <param name="user"></param>
''' <param name="password"></param>
Public Function SetPassword(ByVal user As String, ByVal password As String) As String
    Dim oGUID As String = String.Empty
    Const adsOptionPasswordPortnumber As Long = 6
    Const adsOptionPasswordMethod As Long = 7
    Const adsPasswordEncodeClear As Integer = 1

    Const intPort As Integer = 50000
    Dim objUser As DirectoryEntry
    ' User object.
    ' Set authentication flags.
    Dim AuthTypes As AuthenticationTypes = AuthenticationTypes.Signing Or AuthenticationTypes.Sealing Or AuthenticationTypes.Secure

    ' Bind to user object using LDAP port.
    Try
        objUser = New DirectoryEntry(String.Format(UserUri, user), userid, pwd, AuthTypes)
        'Get object using GetDirectoryEntry
        'objUser = GetDirectoryEntry(user);
        objUser.RefreshCache()

        objUser.Invoke("SetOption", New Object() {adsOptionPasswordPortnumber, intPort})
        objUser.Invoke("SetOption", New Object() {adsOptionPasswordMethod, adsPasswordEncodeClear})
        objUser.Invoke("SetPassword", New Object() {password})
        objUser.CommitChanges()
    Catch e As Exception
        oGUID = e.Message & vbLf & vbCr & Convert.ToString(e.InnerException)
    End Try
    Return oGUID
End Function

这篇关于设置密码在.NET 2.0 Active Directory轻型目录服务(AD LDS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 00:25