Search code examples
asp.netvb.netprincipalcontext

UserPrincipal.FindByIdentity - throwing error on dev


I am working with an ASP.Net (using VB.Net) application. Testing and working locally this code block executes perfectly fine.

Try
        Dim dc As PrincipalContext = New PrincipalContext(ContextType.Domain, "sanuk")
        Dim adUser As UserPrincipal = UserPrincipal.FindByIdentity(dc, IdentityType.Name, uId)
        Return adUser.EmailAddress
    Catch ex As Exception
        'Return "User Not Authenticated"
        Return ex.StackTrace
    End Try

However since promoting it up to our development web server for initial testing, however instead of getting the users email address from AD, I am getting this error message.

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject() at System.DirectoryServices.PropertyValueCollection.PopulateList()
at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()
at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType,Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue) at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue) at ChangeControl.HelpersSystem.GetEmail(String& uId) in C:\Work\Simon\ChangeControl\ChangeControl\HelperClasses\HelpersSystem.vb:line 24

I have had a look around, and this is the closest thing that match's what I am trying to do

Previous similar BUT NOT THE SAME issue. and this too Very similar, same solution as what I have but not working for me

What I would like help with is what other variables if any do I need to provide, as I have tried with a user name and password too and this didn't work either and gave this same error message.

I have also tried without the IdentityType too and still get the same issue. Any and all help very much appreciated.


Solution

  • This sounds like a permission issue. Check the App Pool that the web site is running under and make sure it has the access needed.

    You may want to create an App Pool for this site and have it run as a user that has access to make the Active Directory calls you're doing.

    On your local computer, it probably has heightened access that it needs and works. By default, the App Pools don't have access to much of anything.