マイナーでもいいよね??

殆どVB系、でも .NET じゃない VB は知らないよん

目次

Blog 利用状況

書庫

日記カテゴリ

DirectoryAccessクラスに追加したメソッド

ドメインユーザを表す DomainUser クラスのデータバインド用のメソッドから呼び出してる DirectoryAccess クラスのメソッドです。

先にVBのコードを。

※引数チェックは省略します。

 

'指定した名前と種類の Directory オブジェクトを検索します。

Public Shared Function FindDirectoryObject(name As String, objectCategory As CategoryType) As DirectoryObject

  Using root = GetRootEntry()    'ルートのDirectoryEntryを取得    参考:ユーザやグループの検索

    If CanConnectDomain Then    'ドメインに接続できる時

      Dim filter As String

      Select Case objectCategory

        Case CategoryType.User

          filter = String.Format("(&(objectCategory={0})(sAMAccountName={1}))", objectCategory, name)

        Case CategoryType.PrintQueue

          filter = String.Format("(&(objectCategory={0})(printerName={1}))", objectCategory, name)

        Case Else

          filter = String.Format("(&(objectCategory={0})(name={1}))", objectCategory, name)

      End Select

      Using searcher As New DirectorySearcher(root, filter)

        Dim result = searcher.FindOne()

        Return If(result Is Nothing, Nothing, CreateInstance(result.GetDirectoryEntry()))

      End Using

    Else    'ドメインに接続できない時  <-- こっちはローカル

      Return CreateInstance(root.Children.Find(name, objectCategory.ToString()))

    End If

  End Using

End Function

 

'ユーザの所属するグループを取得します。

Public Shared Function GetBelongGroups(user As IUser) As ReadOnlyCollection(Of String)

  Dim groups As New List(Of String)()

  For Each group As IADs In user.Native.Groups()    '所属するグループ数分(プライマリ グループ以外)

    groups.Add(group.Get("cn").ToString())    'IADs.Get メソッドで cn の値を取得

  Next

  If TypeOf user Is DomainUser Then    'ドメインユーザの時

    Dim primaryGroupId = DirectCast(user, DomainUser).PrimaryGroupId

    groups.Add(GroupTokens.Item(primaryGroupId))

    'GroupTokens プロパティ:IDictionary(Of Integer, String) は下のLoadGroupTokens メソッド内で要素がセットされます。

  End If

  groups.Sort()

  Return groups.AsReadOnly()

End Function

 

'ユーザを取得します。

Public Shared Function GetUsers(Of T As {DirectoryObject, IUser})() As IList(Of T)

  Dim users As New List(Of T)()

  Using root = GetRootEntry() 'ルートのDirectoryEntryを取得

    If CanConnectDomain Then    'ドメインに接続できる時

      Dim filter = String.Format("(objectCategory={0})", CategoryType.User)

      Using searcher As New DirectorySearcher(root, filter)

        Using results = searcher.FindAll()

          For Each res As SearchResult In results

            users.Add(DirectCast(CreateInstance(res.GetDirectoryEntry()), T))

          Next

        End Using

        If GroupTokens.Count = 0 Then    'ドメイングループのPrimaryGroupTokenと名前の組のリストがない時

          LoadGroupTokens(searcher)    'ドメイングループのPrimaryGroupTokenと名前の組のリストをロード

        End If

      End Using

    Else    'ドメインに接続できない時  <-- こっちはローカル

      root.Children.SchemaFilter.Add(CategoryType.User.ToString())

      For Each entry As DirectoryEntry In root.Children

        users.Add(DirectCast(CreateInstance(entry), T))

      Next

    End If

  End Using

  Return users

End Function

 

'DirectoryObject のインスタンスを作成します。

Private Shared Function CreateInstance(entry As DirectoryEntry) As DirectoryObject

  Dim category As CategoryType

  If [Enum].TryParse(Of CategoryType)(entry.SchemaClassName, True, category) = False Then

    Throw New ArgumentException("entry の種類が CategoryType に該当しません。", "entry")

  End If

  Select Case category

    Case CategoryType.User

      If CanConnectDomain Then    'ドメインに接続できる時

        Return New DomainUser(entry)

      Else    'ドメインに接続できない時

        Return New LocalUser(entry)

      End If

    Case Else

      Throw New NotImplementedException()

  End Select

End Function

 

'ドメイングループの PrimaryGroupToken と名前の組のリストをロードします。

Private Shared Sub LoadGroupTokens(searcher As DirectorySearcher)

  searcher.Filter = String.Format("(objectCategory={0})", CategoryType.Group)

  Using results = searcher.FindAll()

    For Each res As SearchResult In results

      Dim entry = res.GetDirectoryEntry()

      entry.Invoke("GetInfoEx", New Object() {"primaryGroupToken"}, 0)

      Dim token = Convert.ToInt32(entry.Properties.Item("primaryGroupToken").Value)

      GroupTokens.Add(token, entry.Properties.Item("cn").Value.ToString())

    Next

  End Using

End Sub

 

ユーザの所属するグループの取得に関する説明は別途書きます。

投稿日時 : 2013年7月10日 23:56

コメントを追加

# stromectol tablets buy online http://stromectolabc.com/
ivermectin 5 2022/02/08 9:24 Busjdhj

stromectol tablets buy online http://stromectolabc.com/
ivermectin 5

# ivermectin 4000 mcg http://stromectolabc.com/
ivermectin generic cream 2022/02/08 16:26 Busjdhj

ivermectin 4000 mcg http://stromectolabc.com/
ivermectin generic cream

# buy doxycycline hyclate 100mg without a rx https://doxycyline1st.com/
doxycycline tablets 2022/02/26 8:07 Doxycycline

buy doxycycline hyclate 100mg without a rx https://doxycyline1st.com/
doxycycline tablets

# cheap doxycycline online https://doxycyline1st.com/
doxycycline hyc 2022/02/26 19:49 Doxycycline

cheap doxycycline online https://doxycyline1st.com/
doxycycline hyc

# vizquoljafmv 2022/06/03 20:42 hxsyshnm

https://erythromycinn.com/# how to use erythromycin ophthalmic ointment

# paxlovid nebenwirkungen https://paxlovid.best/
pfizer covid drug 2022/09/08 7:23 Paxlovid

paxlovid nebenwirkungen https://paxlovid.best/
pfizer covid drug

# cheapest ed pills online https://erectiledysfunctionpills.shop/ 2022/10/14 22:40 Erectile

cheapest ed pills online https://erectiledysfunctionpills.shop/

# prednisone capsules https://prednisone20mg.icu/ 2022/10/15 13:13 Prednisone

prednisone capsules https://prednisone20mg.icu/

# best dating websites https://topdatingsites.fun/
chinese dating 2022/11/15 0:19 DatingTop

best dating websites https://topdatingsites.fun/
chinese dating

# can you buy prednisone over the counter https://prednisonepills.site/
prednisone 50 mg prices 2022/11/28 23:43 Prednisone

can you buy prednisone over the counter https://prednisonepills.site/
prednisone 50 mg prices

# online love dating flash iframe https://datingonlinehot.com/
date women free 2022/12/09 19:12 Dating

online love dating flash iframe https://datingonlinehot.com/
date women free

# best ed pills online https://edpills.science/
ed pills gnc 2023/01/07 13:43 EdPills

best ed pills online https://edpills.science/
ed pills gnc

# popular dating sites https://datingonline1st.com/
farmers only dating 2023/01/17 22:24 Dating

popular dating sites https://datingonline1st.com/
farmers only dating

# Everything what you want to know about pills. Prescription Drug Information, Interactions & Side.
https://edonlinefast.com
Long-Term Effects. Comprehensive side effect and adverse reaction information. 2023/02/17 7:13 EdOnline

Everything what you want to know about pills. Prescription Drug Information, Interactions & Side.
https://edonlinefast.com
Long-Term Effects. Comprehensive side effect and adverse reaction information.

# buy cytotec - https://cytotecsale.pro/# 2023/04/28 23:23 Cytotec

buy cytotec - https://cytotecsale.pro/#

# rx prices https://pillswithoutprescription.pro/# 2023/05/14 22:06 PillsPresc

rx prices https://pillswithoutprescription.pro/#

# pills erectile dysfunction https://edpills.ink/# - buy erection pills 2023/07/26 19:58 EdPills

pills erectile dysfunction https://edpills.ink/# - buy erection pills

# buy valtrex online no prescription https://valtrex.auction/ cost of valtrex in india 2023/10/24 17:39 Valtrex

buy valtrex online no prescription https://valtrex.auction/ cost of valtrex in india

# paxlovid for sale https://paxlovid.bid/ paxlovid india 2023/10/25 18:21 Paxlovid

paxlovid for sale https://paxlovid.bid/ paxlovid india

# farmacia online migliore https://farmaciait.pro/ acquisto farmaci con ricetta 2023/12/04 7:02 Farmacia

farmacia online migliore https://farmaciait.pro/ acquisto farmaci con ricetta

# ed meds online without doctor prescription https://edpills.tech/# best medication for ed 2023/12/23 4:37 EdPills

ed meds online without doctor prescription https://edpills.tech/# best medication for ed

# steroids prednisone for sale https://prednisone.bid/ buy generic prednisone online 2023/12/27 6:50 Prednisone

steroids prednisone for sale https://prednisone.bid/ buy generic prednisone online

# order prednisone 10 mg tablet https://prednisonepharm.store/ prednisone 30 mg tablet 2024/01/20 15:52 Prednisone

order prednisone 10 mg tablet https://prednisonepharm.store/ prednisone 30 mg tablet

# lana rhoades izle - https://lanarhoades.fun/ lana rhoades video
2024/03/02 23:54 LanaRho

lana rhoades izle - https://lanarhoades.fun/ lana rhoades video

# eva elfie new video https://evaelfie.site/ eva elfie photo
2024/03/07 4:37 EvaElfie

eva elfie new video https://evaelfie.site/ eva elfie photo

# sweet bonanza hilesi https://sweetbonanza.bid/ - sweet bonanza demo oyna
2024/03/27 22:09 Bonanzaj

sweet bonanza hilesi https://sweetbonanza.bid/ - sweet bonanza demo oyna

タイトル
名前
URL
コメント