与扩展s.ds.am.UserPrincipal类问题 [英] Problems with extending s.ds.am.UserPrincipal class

查看:316
本文介绍了与扩展s.ds.am.UserPrincipal类问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图扩大在VS 2010中VB应用程序的s.ds.am.UserPrincipal类,因为我需要访问AD用户属性,不是默认的UserPrincipal的一部分。这是一个新的概念对我来说,我已经发现了一些有用的code就在这里很好,但我遇到了2个问题与我的扩展,我似乎无法弄清楚。

问题1::当我试图用我的UserPrincipalEx检索我收到以下错误,用户

ERROR:

 类型MyApp.UserPrincipalEx的主要对象,不能针对这个店查询中使用
 

code,产生错误:

 昏暗CTX作为新PrincipalContext(ContextType.Domain,DomainController)
昏暗的用户作为UserPrincipalEx = UserPrincipalEx.FindByIdentity(CTX,的samAccountName)
 

问题2::当我试图创建与我的UserPrincipalEx一个新的用户我收到以下错误。当我跟踪PrincipalContext(CTX)入类似乎从使用DomainController断开连接。

ERROR:

 未知的错误(0x80005000)
 

code,产生错误:

 昏暗CTX作为新PrincipalContext(ContextType.Domain,DomainController,DirectoryOU)
昏暗NewADUser作为新UserPrincipalEx(CTX,newsamAccountName,新密码,真)
 

类:

 进口System.DirectoryServices.AccountManagement
公共类UserPrincipalEx
继承UserPrincipal

公共子新(BYVAL上下文PrincipalContext)
    MyBase.New(上下文)
结束小组

公共子新(BYVAL上下文PrincipalContext,BYVAL的samAccountName作为字符串,BYVAL密码作为字符串,BYVAL启用为布尔)
    MyBase.New(背景下,的samAccountName,密码,启用)
结束小组

< D​​irectoryProperty(标题)> _
公共属性Title()作为字符串
    得到
        如果ExtensionGet(标题)长度和LT;> 1然后
            返回任何结果
        结束如果


        返回DirectCast(ExtensionGet(标题)(0),字符串)
    最终获取
    设置(BYVAL值作为字符串)
        Me.ExtensionSet(标题,值)
    结束设定
高端物业
< D​​irectoryProperty(physicalDeliveryOfficeName)> _
公共财产physicalDeliveryOfficeName()作为字符串
    得到
        如果ExtensionGet(physicalDeliveryOfficeName)长度和LT;> 1然后
            返回任何结果
        结束如果


        返回DirectCast(ExtensionGet(physicalDeliveryOfficeName)(0),字符串)
    最终获取
    设置(BYVAL值作为字符串)
        Me.ExtensionSet(physicalDeliveryOfficeName,值)
    结束设定
高端物业
< D​​irectoryProperty(「本公司」)> _
公共物业公司()作为字符串
    得到
        如果ExtensionGet(公司)长度和LT;> 1然后
            返回任何结果
        结束如果


        返回DirectCast(ExtensionGet(公司)(0),字符串)
    最终获取
    设置(BYVAL值作为字符串)
        Me.ExtensionSet(公司,值)
    结束设定
高端物业
实施重载的搜索方法FindByIdentity。
公共共享阴影功能FindByIdentity(BYVAL上下文PrincipalContext,BYVAL identityValue作为字符串)作为UserPrincipalEx
    返回DirectCast(FindByIdentityWithType(背景下,的GetType(UserPrincipalEx),identityValue),UserPrincipalEx)
端功能

实施重载的搜索方法FindByIdentity。
公共共享阴影功能FindByIdentity(BYVAL上下文PrincipalContext,BYVAL identityType作为IdentityType,BYVAL identityValue作为字符串)作为UserPrincipalEx
    返回DirectCast(FindByIdentityWithType(背景下,的GetType(UserPrincipalEx),identityType,identityValue),UserPrincipalEx)
端功能
末级
 

解决方案

问题1

我是能够解决问题1的进口后,加入以下的类的顶部

 < D​​irectoryRdn preFIX(CN)> _
< D​​irectoryObjectClass(用户)> _
 

问题2

我也一直在敲我的头这要长,终于能解决问题2. DirectoryOU变量不包含正确的路径预期的AD OU。我是缺少一个逗号盯着一切为了30分钟后,我终于发现了问题。

I have been trying to extend the s.ds.am.UserPrincipal class in VS 2010 VB app as I require access to AD User properties that are not by default a part of the UserPrincipal. This is a new concept to me and I have found some useful code on here as well but I have run into 2 problems with my extensions that I cannot seem to figure out.

Problem 1: When I attempt to use my UserPrincipalEx to retrieve a user I receive the following error.

ERROR:

Principal objects of type MyApp.UserPrincipalEx can not be used in a query against this store

CODE TO PRODUCE ERROR:

Dim ctx As New PrincipalContext(ContextType.Domain, DomainController)
Dim user As UserPrincipalEx = UserPrincipalEx.FindByIdentity(ctx, samAccountName)

Problem 2: When I attempt to create a new user with my UserPrincipalEx I receive the following error. When I trace the PrincipalContext (ctx) into the class it appears to disconnect from the DomainController.

ERROR:

Unknown error (0x80005000)

CODE TO PRODUCE ERROR:

Dim ctx As New PrincipalContext(ContextType.Domain, DomainController, DirectoryOU)
Dim NewADUser As New UserPrincipalEx(ctx, newsamAccountName, newPassword, True)

CLASS:

Imports System.DirectoryServices.AccountManagement
Public Class UserPrincipalEx
Inherits UserPrincipal

Public Sub New(ByVal context As PrincipalContext)
    MyBase.New(context)
End Sub

Public 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

<DirectoryProperty("Title")> _
Public Property Title() As String
    Get
        If ExtensionGet("title").Length <> 1 Then
            Return Nothing
        End If


        Return DirectCast(ExtensionGet("title")(0), String)
    End Get
    Set(ByVal value As String)
        Me.ExtensionSet("title", value)
    End Set
End Property
<DirectoryProperty("physicalDeliveryOfficeName")> _
Public Property physicalDeliveryOfficeName() As String
    Get
        If ExtensionGet("physicalDeliveryOfficeName").Length <> 1 Then
            Return Nothing
        End If


        Return DirectCast(ExtensionGet("physicalDeliveryOfficeName")(0), String)
    End Get
    Set(ByVal value As String)
        Me.ExtensionSet("physicalDeliveryOfficeName", value)
    End Set
End Property
<DirectoryProperty("Company")> _
Public Property Company() As String
    Get
        If ExtensionGet("company").Length <> 1 Then
            Return Nothing
        End If


        Return DirectCast(ExtensionGet("company")(0), String)
    End Get
    Set(ByVal value As String)
        Me.ExtensionSet("company", value)
    End Set
End Property
' Implement the overloaded search method FindByIdentity. 
Public Shared Shadows Function FindByIdentity(ByVal context As PrincipalContext, ByVal identityValue As String) As UserPrincipalEx
    Return DirectCast(FindByIdentityWithType(context, GetType(UserPrincipalEx), identityValue), UserPrincipalEx)
End Function

' Implement the overloaded search method FindByIdentity. 
Public Shared Shadows Function FindByIdentity(ByVal context As PrincipalContext, ByVal identityType As IdentityType, ByVal identityValue As String) As UserPrincipalEx
    Return DirectCast(FindByIdentityWithType(context, GetType(UserPrincipalEx), identityType, identityValue), UserPrincipalEx)
End Function
End Class

解决方案

PROBLEM 1

I was able to solve Problem 1 by adding the following to the top of the class after the Imports

<DirectoryRdnPrefix("CN")> _
<DirectoryObjectClass("user")> _

PROBLEM 2

I have also been banging my head against this for to long and was finally able to resolve Problem 2. The DirectoryOU variable did not contain a correct path the intended AD OU. I was missing a comma and after staring at everything for 30 minutes I finally noticed the issue.

这篇关于与扩展s.ds.am.UserPrincipal类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆