Directory Programming .NET

Active Directory and ADAM programming support for .NET developers
Welcome to Directory Programming .NET Sign in | Join | Help
in Search

Update attributes in .Net 3.5

Last post 06-05-2008, 2:56 AM by RoyM. 7 replies.
Sort Posts: Previous Next
  •  05-14-2008, 2:59 AM 3551

    Update attributes in .Net 3.5

    What's the best way to update some attributres in AD using Asp.Net 3.5? Let's say I wanna update my own "homePhone" attribute... I know how this is done in Asp.Net 2.0, but now I am using 3.5, so maybe it's better to use UserPrincipal? In case, how?

    Roy

  •  05-14-2008, 10:45 AM 3554 in reply to 3551

    Re: Update attributes in .Net 3.5

    If you want to use S.DS.AccountManagement (UserPrincipal and such), but you also need to update an attribute that doesn't have a built in property mapping to one of the Principal-derived classes, then you probably would want to implement your own extension class.  I discuss how to do this in my Jan 2008 MSDN magazine article on S.DS.AM and it is has also been discussed in the forums several times.

    If you don't want to bother with S.DS.AM or don't want to go through this extra effort, you .NET 2.0 code will still work as is.

  •  05-16-2008, 1:44 AM 3577 in reply to 3554

    Re: Update attributes in .Net 3.5

    Thanks.
    Im just having a small problem with my 2.0 code... If a user have not set for example the "homePhone" attribute, I am not able to ad it. I don't get any exception, just nothing happens. Any idea? This is also a reason I would like to use plain 3.5 code where I can use UserPrincipal and GroupPrincipal...

    Roy

  •  05-16-2008, 10:15 AM 3581 in reply to 3577

    Re: Update attributes in .Net 3.5

    Can you show an example here?  I'd like to see what might be going wrong.

    It is definitely possible to implement this functionality with .NET 3.5, but lets try to solve the immediate problem first.

  •  05-19-2008, 2:10 AM 3589 in reply to 3581

    Re: Update attributes in .Net 3.5

    This is the "redifined code that im using. Sometimes it works, sometimes not. Very strange.

    <code>

    Public Shared Function updateUserAttribute(ByVal attribute As String, ByVal inVal As String) As Boolean

    Try
    Dim userName As String = Nothing
    Dim passWord As String = Nothing
    getImpersonation(userName, passWord)
    Dim adPath As String = ConnectionStrings("connAD_users").ConnectionString
    Dim adsEntry As New DirectoryEntry(adPath, userName, passWord, AuthenticationTypes.Secure)
    Dim search As New DirectorySearcher(adsEntry, "(SAMAccountName=" & USERID() & ")", New String() {"cn", attribute})
    Dim result As SearchResult = search.FindOne()

    If Not result Is Nothing Then
    If result.Properties.Contains("cn") Then
    Dim name As String = result.Properties("cn")(0).ToString()
    End If

    If result.Properties.Contains(attribute) Then
    Dim delpval As String = result.Properties(attribute)(0).ToString()
    End If

    Using user As DirectoryEntry = result.GetDirectoryEntry()
    user.Properties(attribute).Value = inVal
    user.CommitChanges()
    End Using
    End If

    Catch ex As Exception
    'Do some exception handling
    End Try

    End Function 'updateUserAttribute

    Public Shared Sub getImpersonation(ByRef UserName As String, ByRef Password As String)

    Dim configPath As String = HttpRuntime.AppDomainAppVirtualPath
    Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(configPath)
    Dim section As New IdentitySection
    section = config.GetSection(
    "system.web/identity")
    UserName = section.UserName
    Password = section.Password

    End Sub 'getImpersonation

    </code>

  •  05-19-2008, 11:23 AM 3601 in reply to 3589

    Re: Update attributes in .Net 3.5

    What is the exception you get when this doesn't work?  The full stack trace would be helpful.
  •  05-19-2008, 10:24 PM 3610 in reply to 3601

    Re: Update attributes in .Net 3.5

    Here's an example of what has worked for me in extending the UserPrincipal and using the 3.5 stuff.... Sorry if I've doubled up on previous posts...

    One quirk I've found though is that if I set AccountExpirationDate to a value and save, then set and save another property, the AccountExpirationDate value is lost from the current instance. It is stored to AD successfully though and if you retrieve the user again you should see the value present...

    Hope that doesn't confuse anyone :)

    Imports System.DirectoryServices.AccountManagement

    <DirectoryRdnPrefix("CN")> _

    <DirectoryObjectClass("user")> _

    Public Class MyUserPrincipal

    Inherits UserPrincipal

    ''' <summary>

    ''' AD Property: mycustomattribute

    ''' </summary>

    <DirectoryProperty("mycustomattribute")> _

    Public Property BusinessUnitLocationCode() As String

    Get

    If MyBase.ExtensionGet("mycustomattribute") IsNot Nothing AndAlso MyBase.ExtensionGet("mycustomattribute").Length > 0 Then

    ' return first item

    Return MyBase.ExtensionGet("mycustomattribute")(0).ToString()

    Else

    Return ""

    End If

    End Get

    Set(ByVal value As String)

    MyBase.ExtensionSet("mycustomattribute", value)

    End Set

    End Property

    ''' <summary>

    ''' Initializes a new instance of the UserPrincipal class by using the specified context

    ''' </summary>

    ''' <param name="context">The PrincipalContext that specifies the server or domain against which operations are performed</param>

    ''' <remarks></remarks>

    Sub New(ByVal context As PrincipalContext)

    MyBase.New(context)

    End Sub

    ''' <summary>

    ''' Initializes a new instance of the UserPrincipal class by using the specified context,

    ''' SAM account name, password, and enabled value

    ''' </summary>

    ''' <param name="context">The PrincipalContext that specifies the server or domain against which operations are performed</param>

    ''' <param name="samAccountName">The SAM account name for this user principal</param>

    ''' <param name="password">The password for this account</param>

    ''' <param name="enabled">A Boolean value that specifies whether the account is enabled</param>

    ''' <remarks></remarks>

    Sub New(ByVal context As PrincipalContext, ByVal samAccountName As String, ByVal password As String, ByVal enabled As Boolean)

    MyBase.New(context, samAccountName, password, enabled)

    End Sub

    ''' <summary>

    ''' Returns a user principal object that matches the specified identity value

    ''' </summary>

    ''' <param name="context">The PrincipalContext that specifies the server or domain against which operations are performed</param>

    ''' <param name="identityValue">The identity of the user principal. This parameter can be any format that is contained in the IdentityType enumeration</param>

    ''' <returns>A UserPrincipal object that matches the specified identity value, or null if no matches are found</returns>

    ''' <exception cref="MultipleMatchesException">Multiple user principal objects matching the current user object were found</exception>

    ''' <remarks></remarks>

    Shared Shadows Function FindByIdentity(ByVal context As PrincipalContext, ByVal identityValue As String) As MyUserPrincipal

    Return DirectCast(FindByIdentityWithType(context, GetType(MyUserPrincipal), identityValue), MyUserPrincipal)

    End Function

    ''' <summary>

    ''' Returns a user principal object that matches the specified identity type, and value.

    ''' This version of the FindByIdentity method determines the format of the identity value

    ''' </summary>

    ''' <param name="context">The PrincipalContext that specifies the server or domain against which operations are performed</param>

    ''' <param name="identityType">A IdentityType enumeration value that specifies the format of the identityValue parameter</param>

    ''' <param name="identityValue">The identity of the user principal. This parameter can be any format that is contained in the IdentityType enumeration</param>

    ''' <returns>A UserPrincipal object that matches the specified identity value, or null if no matches are found</returns>

    ''' <exception cref="MultipleMatchesException">Multiple user principal objects matching the current user object were found</exception>

    ''' <remarks></remarks>

    Shared Shadows Function FindByIdentity(ByVal context As PrincipalContext, ByVal identityType As IdentityType, ByVal identityValue As String) As MyUserPrincipal

    Return DirectCast(FindByIdentityWithType(context, GetType(MyUserPrincipal), identityType, identityValue), MyUserPrincipal)

    End Function

    End Class

    Regards Paul

  •  06-05-2008, 2:56 AM 3800 in reply to 3610

    Re: Update attributes in .Net 3.5

    Thanks, Paul. Now I got my code to work as well..
View as RSS news feed in XML