Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Getting error while accessing AD from Custom Web App under WSS site

  Asked By: Vernon    Date: Oct 02    Category: Sharepoint    Views: 1463

I created a custom ASP.NET website under..12/Template/Layouts/MyDir.
In this site, I'm getting the currently logged username and passing
it to different function under same .aspx file. Basically that
function return values from AD. The below is code in VB.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim web As SPWeb = SPControl.GetContextWeb(Context)
Dim loginUser As String = SPControl.GetContextWeb
(Context).CurrentUser.LoginName
Dim UserInfo As DirectoryServices.DirectoryEntry =
GetAdsObject(loginUser)
...
..
End Sub


Function GetAdsObject(ByVal strNtName As String) As
DirectoryServices.DirectoryEntry
On Error GoTo gotnothing
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

Dim objTrans = CreateObject("NameTranslate")
Dim objUser As DirectoryServices.DirectoryEntry ' User Object

objTrans.Set(ADS_NAME_TYPE_NT4, strNtName)

Dim strUserDN = objTrans.Get(ADS_NAME_TYPE_1779) ' Get LDAP Path
Dim ADSPath As String = "LDAP://" + strUserDN

objUser = New DirectoryServices.DirectoryEntry(ADSPath) ' Get
ADS object from path

Return objUser

gotnothing:
Return Nothing
End Function

After this code, I'm getting an error like
System.UnauthorizedAccessException: Access is denied. (Exception from
HRESULT: 0x80070005 (E_ACCESSDENIED)) at
Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall
(Object o, Type objType, String name, Object[] args, String[]
paramnames, Boolean[] CopyBack, Boolean IgnoreReturn) at
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object
Instance, Type Type, String MemberName, Object[] Arguments, String[]
ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean
IgnoreReturn) at
ASP._layouts_MyDir_getcurrentcmpuser_aspx.GetAdsObject(String
strNtName)

Any help would be really appreciated. I searched in google in and
found lots of workarounds but nothing helped.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Sharonda Mcfarland     Answered On: Oct 02

You got an access  denied error  because the code  ran under the context of a
user who wasn't authorized to execute the code.

MOSS 2007 allows you to "RunWithElevatedPrivileges" (SPS 2003 required
you to use impersonation) - see
msdn2.microsoft.com/en-us/library/ms368312.aspx,
msdn2.microsoft.com/.../...arepoint.spsecurity.run
withelevatedprivileges.aspx, and
http://daniellarson.spaces.live.com/blog/cns!D3543C5837291E93!927.entry
for
details.

Below is sample VB code on how to do this.

Private Sub EditSiteCreation()
Dim site  As SPSite = New SPSite(m_CurrUrl)
site.CatchAccessDeniedException = False
Dim web  As SPWeb = site.OpenWeb()
' Additional code
site.Dispose()
End Sub

Private Sub OriginalMethod()
Dim ElevateCode As New SPSecurity.CodeToRunElevated(AddressOf
EditSiteCreation)
SPSecurity.RunWithElevatedPrivileges(ElevateCode)
End Sub

 




Tagged: