jj: Start work on ad import

This commit is contained in:
Jonathan Jenne 2018-05-04 16:22:56 +02:00
parent 54d712f67d
commit 794f18fd83
4 changed files with 302 additions and 0 deletions

View File

@ -60,6 +60,9 @@
<Reference Include="System.Data" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Deployment" />
<Reference Include="System.DirectoryServices" />
<Reference Include="System.DirectoryServices.AccountManagement" />
<Reference Include="System.DirectoryServices.Protocols" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web.Services" />
<Reference Include="System.Windows.Forms" />
@ -83,6 +86,12 @@
<Import Include="System.Threading.Tasks" />
</ItemGroup>
<ItemGroup>
<Compile Include="frmADImport.Designer.vb">
<DependentUpon>frmADImport.vb</DependentUpon>
</Compile>
<Compile Include="frmADImport.vb">
<SubType>Form</SubType>
</Compile>
<Compile Include="frmMain.vb">
<SubType>Form</SubType>
</Compile>
@ -115,6 +124,9 @@
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="frmADImport.resx">
<DependentUpon>frmADImport.vb</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="frmMain.resx">
<DependentUpon>frmMain.vb</DependentUpon>
</EmbeddedResource>

View File

@ -0,0 +1,62 @@
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class frmADImport
Inherits System.Windows.Forms.Form
'Das Formular überschreibt den Löschvorgang, um die Komponentenliste zu bereinigen.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Wird vom Windows Form-Designer benötigt.
Private components As System.ComponentModel.IContainer
'Hinweis: Die folgende Prozedur ist für den Windows Form-Designer erforderlich.
'Das Bearbeiten ist mit dem Windows Form-Designer möglich.
'Das Bearbeiten mit dem Code-Editor ist nicht möglich.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.gridAD_Groups = New DevExpress.XtraGrid.GridControl()
Me.GridView1 = New DevExpress.XtraGrid.Views.Grid.GridView()
CType(Me.gridAD_Groups, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.GridView1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'gridAD_Groups
'
Me.gridAD_Groups.Dock = System.Windows.Forms.DockStyle.Left
Me.gridAD_Groups.Location = New System.Drawing.Point(0, 0)
Me.gridAD_Groups.MainView = Me.GridView1
Me.gridAD_Groups.Name = "gridAD_Groups"
Me.gridAD_Groups.Size = New System.Drawing.Size(294, 456)
Me.gridAD_Groups.TabIndex = 0
Me.gridAD_Groups.ViewCollection.AddRange(New DevExpress.XtraGrid.Views.Base.BaseView() {Me.GridView1})
'
'GridView1
'
Me.GridView1.GridControl = Me.gridAD_Groups
Me.GridView1.Name = "GridView1"
'
'frmADImport
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(865, 456)
Me.Controls.Add(Me.gridAD_Groups)
Me.Name = "frmADImport"
Me.Text = "frmADImport"
CType(Me.gridAD_Groups, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.GridView1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents gridAD_Groups As DevExpress.XtraGrid.GridControl
Friend WithEvents GridView1 As DevExpress.XtraGrid.Views.Grid.GridView
End Class

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,108 @@
Imports System.DirectoryServices
Imports System.DirectoryServices.ActiveDirectory
Imports System.DirectoryServices.AccountManagement
Public Class frmADImport
Private excludedGroupNames As New List(Of String) From {
"Abgelehnte",
"DHCP",
"Distributed COM",
"Dns",
"Domänen-Gäste",
"Domänencomput",
"Druck",
"Einstellungen eingehender",
"Ereignis",
"Gäst",
"Hyper-V",
"IIS_",
"Klonbare",
"Konten-Oper",
"Kryptografie",
"Leistungs",
"Netzwerkskon",
"PrivUser",
"Protected User",
"Prä-Windows",
"RAS- und IAS-Server",
"RDS-",
"Remoteverwaltungs",
"Replikations",
"Reporting",
"Richtlinien-Ersteller",
"SQLAccess",
"Schreibgeschützte Domänen",
"Server-Operatore",
"Sicherungs",
"Terminalserver-Liz",
"WinRMR",
"Windows-Auth",
"Zertifikat",
"Zugriffssteuerungs",
"Zulässige"
}
Private Sub frmADImport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim groups = GetActiveDirectoryGroups(Environment.UserName)
gridAD_Groups.DataSource = groups
End Sub
Private Function FindUser(rootEntry As DirectoryEntry, samAccountName As String) As DirectoryEntry
Dim userSearch = New DirectorySearcher(
rootEntry,
$"(samAccountName={samAccountName})",
New String() {"displayName"}
)
Dim result = userSearch.FindOne()
If IsNothing(result) Then
Return Nothing
Else
Return result.GetDirectoryEntry()
End If
End Function
Private Function GetGroupNameFromTokenGroupEntry(rootEntry As DirectoryEntry, tokenGroup As Byte())
Dim sID As New Security.Principal.SecurityIdentifier(tokenGroup, 0)
Dim sIDSearch = New DirectorySearcher(rootEntry, $"(objectSid={sID.Value})", New String() {"name"})
Dim sIDResult = sIDSearch.FindOne()
If IsNothing(sIDResult) Then
Return Nothing
Else
Return sIDResult.Properties("name").Item(0).ToString()
End If
End Function
Private Function GetActiveDirectoryGroups(samAccountName As String) As List(Of String)
Dim groups As New List(Of String)
Dim adRoot As New DirectoryEntry() With {
.AuthenticationType = AuthenticationTypes.Secure
}
Dim user As DirectoryEntry = FindUser(adRoot, samAccountName)
If IsNothing(user) Then
MsgBox($"Benutzer {samAccountName} wurde nicht in der Active Directory gefunden!")
Return groups
End If
user.RefreshCache(New String() {"tokenGroups"})
For Each tokenGroup As Byte() In user.Properties("tokenGroups")
Dim groupName As String = GetGroupNameFromTokenGroupEntry(adRoot, tokenGroup)
If Not IsNothing(groupName) Then
Dim isValidGroup As Boolean = excludedGroupNames.Where(Function(excludedGroup) Not groupName.StartsWith(excludedGroup)).Any()
If isValidGroup Then
groups.Add(groupName)
End If
End If
Next
Return groups
End Function
End Class