{"id":767,"date":"2025-10-28T22:27:37","date_gmt":"2025-10-28T21:27:37","guid":{"rendered":"https:\/\/www.silverice.it\/IpercubeTechWP1\/?page_id=767"},"modified":"2025-10-28T22:27:37","modified_gmt":"2025-10-28T21:27:37","slug":"user-principal-comparer-service-class","status":"publish","type":"page","link":"https:\/\/www.silverice.it\/IpercubeTechWP1\/ipercube-utilities\/active-directory-functions\/user-principal-comparer-service-class\/","title":{"rendered":"User Principal Comparer (Service Class)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Custom comparer for\u00a0<strong>DirectoryServices.AccountManagement.UserPrincipal<\/strong>\u00a0objects based on their DistinguishedName property.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Useful for removing duplicate users from lists by comparing their DistinguishedName.<\/li>\n\n\n\n<li>Compares DistinguishedName values case-insensitively.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:75%\">\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column page-snippet is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-Geist-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-Geist-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">VB<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>    Public Class adUserPrincipalComparer\n        Implements IEqualityComparer(Of UserPrincipal)\n\n        ''' &lt;summary>\n        ''' Determines whether two &lt;see cref=\"UserPrincipal\"\/> instances are equal based on their DistinguishedName.\n        ''' &lt;\/summary>\n        ''' &lt;category>Service functions&lt;\/category>\n        ''' &lt;param name=\"x\">The first &lt;see cref=\"UserPrincipal\"\/> to compare.&lt;\/param>\n        ''' &lt;param name=\"y\">The second &lt;see cref=\"UserPrincipal\"\/> to compare.&lt;\/param>\n        ''' &lt;returns>\n        ''' &lt;c>True&lt;\/c> if both &lt;see cref=\"UserPrincipal\"\/> objects have the same DistinguishedName (case-insensitive); otherwise, &lt;c>False&lt;\/c>.\n        ''' &lt;\/returns>\n        Public Overloads Function Equals(x As UserPrincipal, y As UserPrincipal) As Boolean Implements IEqualityComparer(Of UserPrincipal).Equals\n            If x Is Nothing OrElse y Is Nothing Then\n                Return False\n            End If\n            Return String.Equals(x.DistinguishedName, y.DistinguishedName, StringComparison.OrdinalIgnoreCase)\n        End Function\n\n        ''' &lt;summary>\n        ''' Returns a hash code for the specified &lt;see cref=\"UserPrincipal\"\/> based on the DistinguishedName.\n        ''' &lt;\/summary>\n        ''' &lt;category>Service functions&lt;\/category>\n        ''' &lt;param name=\"obj\">The &lt;see cref=\"UserPrincipal\"\/> object.&lt;\/param>\n        ''' &lt;returns>\n        ''' A hash code based on the DistinguishedName if present; otherwise, &lt;c>0&lt;\/c>.\n        ''' &lt;\/returns>\n        Public Overloads Function GetHashCode(obj As UserPrincipal) As Integer Implements IEqualityComparer(Of UserPrincipal).GetHashCode\n            Return If(obj.DistinguishedName IsNot Nothing, obj.DistinguishedName.ToLower().GetHashCode(), 0)\n        End Function\n\n    End Class<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">Public Class <\/span><span style=\"color: #D4D4D4\">adUserPrincipalComparer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        Implements <\/span><span style=\"color: #DCDCAA\">IEqualityComparer<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">Of<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">UserPrincipal<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;summary&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; Determines whether two &lt;see cref=&quot;UserPrincipal&quot;\/&gt; instances are equal based on their DistinguishedName.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;\/summary&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;category&gt;Service functions&lt;\/category&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;param name=&quot;x&quot;&gt;The first &lt;see cref=&quot;UserPrincipal&quot;\/&gt; to compare.&lt;\/param&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;param name=&quot;y&quot;&gt;The second &lt;see cref=&quot;UserPrincipal&quot;\/&gt; to compare.&lt;\/param&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;returns&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;c&gt;True&lt;\/c&gt; if both &lt;see cref=&quot;UserPrincipal&quot;\/&gt; objects have the same DistinguishedName (case-insensitive); otherwise, &lt;c&gt;False&lt;\/c&gt;.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;\/returns&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">Public<\/span><span style=\"color: #D4D4D4\"> Overloads<\/span><span style=\"color: #569CD6\"> Function <\/span><span style=\"color: #DCDCAA\">Equals<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">x<\/span><span style=\"color: #D4D4D4\"> As <\/span><span style=\"color: #4EC9B0\">UserPrincipal<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #9CDCFE\"> y<\/span><span style=\"color: #D4D4D4\"> As <\/span><span style=\"color: #4EC9B0\">UserPrincipal<\/span><span style=\"color: #D4D4D4\">) As <\/span><span style=\"color: #4EC9B0\">Boolean<\/span><span style=\"color: #D4D4D4\"> Implements <\/span><span style=\"color: #DCDCAA\">IEqualityComparer<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">Of<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">UserPrincipal<\/span><span style=\"color: #D4D4D4\">).Equals<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">If<\/span><span style=\"color: #D4D4D4\"> x Is<\/span><span style=\"color: #569CD6\"> Nothing<\/span><span style=\"color: #D4D4D4\"> OrElse y Is<\/span><span style=\"color: #569CD6\"> Nothing<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">Then<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">Return<\/span><span style=\"color: #569CD6\"> False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">End If<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">Return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">String<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">Equals<\/span><span style=\"color: #D4D4D4\">(x.DistinguishedName, y.DistinguishedName, StringComparison.<\/span><span style=\"color: #9CDCFE\">OrdinalIgnoreCase<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">End Function<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;summary&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; Returns a hash code for the specified &lt;see cref=&quot;UserPrincipal&quot;\/&gt; based on the DistinguishedName.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;\/summary&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;category&gt;Service functions&lt;\/category&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;param name=&quot;obj&quot;&gt;The &lt;see cref=&quot;UserPrincipal&quot;\/&gt; object.&lt;\/param&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;returns&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; A hash code based on the DistinguishedName if present; otherwise, &lt;c&gt;0&lt;\/c&gt;.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\">&#39;&#39;&#39; &lt;\/returns&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">Public<\/span><span style=\"color: #D4D4D4\"> Overloads<\/span><span style=\"color: #569CD6\"> Function <\/span><span style=\"color: #DCDCAA\">GetHashCode<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">obj<\/span><span style=\"color: #D4D4D4\"> As <\/span><span style=\"color: #4EC9B0\">UserPrincipal<\/span><span style=\"color: #D4D4D4\">) As <\/span><span style=\"color: #4EC9B0\">Integer<\/span><span style=\"color: #D4D4D4\"> Implements <\/span><span style=\"color: #DCDCAA\">IEqualityComparer<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">Of<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">UserPrincipal<\/span><span style=\"color: #D4D4D4\">).GetHashCode<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">Return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">If<\/span><span style=\"color: #D4D4D4\">(obj.DistinguishedName IsNot<\/span><span style=\"color: #569CD6\"> Nothing<\/span><span style=\"color: #D4D4D4\">, obj.DistinguishedName.<\/span><span style=\"color: #DCDCAA\">ToLower<\/span><span style=\"color: #D4D4D4\">().<\/span><span style=\"color: #DCDCAA\">GetHashCode<\/span><span style=\"color: #D4D4D4\">(), <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">End Function<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">End Class<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Custom comparer for\u00a0DirectoryServices.AccountManagement.UserPrincipal\u00a0objects based on their DistinguishedName property.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":754,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-767","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.silverice.it\/IpercubeTechWP1\/wp-json\/wp\/v2\/pages\/767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silverice.it\/IpercubeTechWP1\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.silverice.it\/IpercubeTechWP1\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.silverice.it\/IpercubeTechWP1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silverice.it\/IpercubeTechWP1\/wp-json\/wp\/v2\/comments?post=767"}],"version-history":[{"count":2,"href":"https:\/\/www.silverice.it\/IpercubeTechWP1\/wp-json\/wp\/v2\/pages\/767\/revisions"}],"predecessor-version":[{"id":769,"href":"https:\/\/www.silverice.it\/IpercubeTechWP1\/wp-json\/wp\/v2\/pages\/767\/revisions\/769"}],"up":[{"embeddable":true,"href":"https:\/\/www.silverice.it\/IpercubeTechWP1\/wp-json\/wp\/v2\/pages\/754"}],"wp:attachment":[{"href":"https:\/\/www.silverice.it\/IpercubeTechWP1\/wp-json\/wp\/v2\/media?parent=767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}