0
votes

I got this error

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

when I try to create Virtual Directory using vb.net code with asp.net web forms and the following the code that I used ..

Private Sub CreateVirtualDir(ByVal WebSite As String, ByVal AppName As String, ByVal Path As String)

        Dim IISSchema As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/Schema/AppIsolated")
        Dim CanCreate As Boolean = Not IISSchema.Properties("Syntax").Value.ToString.ToUpper() = "BOOLEAN"
        IISSchema.Dispose()

        If CanCreate Then
            Dim PathCreated As Boolean

            Try
                Dim IISAdmin As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/W3SVC/1/Root")

                'make sure folder exists
                If Not System.IO.Directory.Exists(Path) Then
                    System.IO.Directory.CreateDirectory(Path)
                    PathCreated = True
                End If

                'If the virtual directory already exists then delete it
                For Each VD As System.DirectoryServices.DirectoryEntry In IISAdmin.Children
                    If VD.Name = AppName Then
                        IISAdmin.Invoke("Delete", New String() {VD.SchemaClassName, AppName})
                        IISAdmin.CommitChanges()
                        Exit For
                    End If
                Next VD

                'Create and setup new virtual directory
                Dim VDir As System.DirectoryServices.DirectoryEntry = IISAdmin.Children.Add(AppName, "IIsWebVirtualDir")
                VDir.Properties("Path").Item(0) = Path
                VDir.Properties("AppFriendlyName").Item(0) = AppName
                VDir.Properties("EnableDirBrowsing").Item(0) = False
                VDir.Properties("AccessRead").Item(0) = True
                VDir.Properties("AccessExecute").Item(0) = True
                VDir.Properties("AccessWrite").Item(0) = False
                VDir.Properties("AccessScript").Item(0) = True
                VDir.Properties("AuthNTLM").Item(0) = True
                VDir.Properties("AuthBasic").Item(0) = True
                VDir.Properties("AspBufferingOn").Item(0) = True
                VDir.Properties("EnableDefaultDoc").Item(0) = True
                VDir.Properties("DefaultDoc").Item(0) = "default.htm,default.aspx,default.asp"
                VDir.Properties("AspEnableParentPaths").Item(0) = True
                VDir.Properties("AuthAnonymous").Item(0) = True
                VDir.CommitChanges()

                'the following are acceptable params
                'INPROC = 0
                'OUTPROC = 1
                'POOLED = 2
                VDir.Invoke("AppCreate", 1)

            Catch Ex As Exception
                If PathCreated Then
                    System.IO.Directory.Delete(Path)
                End If



            End Try
        End If

    End Sub

any help please >????

2
What user runs your code? Access denied is a security issue, meaning check external factors as well. - Joel Coehoorn
What do you mean about What user runs your code ? know could I now this user ? - Amr Elnashar
Your code... all code... runs under the auspices of some specific user account, and has only the security authorizations afforded that account. I'm wondering whether it's running under a different user account than you expect. - Joel Coehoorn
I appropriated for your help but I still don't know how to get that user and give him the permissions . - Amr Elnashar
Search for the identity of the application pool - Steve B

2 Answers

1
votes

If you are running the application by visual studio, start visual studio as Administrator

If you are running by IIS, set your user as administrator or an user that have the required permissions on Application Pool
Open IIS / go to Application Pools / right click your application pool / Advanced Settings / Process Model / Load User Profile = true/
click Identity and choose your account

Good luck

0
votes

This Solution worked for me ==>

http://blogs.msdn.com/b/jpsanders/archive/2009/05/13/iis-7-adsi-error-system-runtime-interopservices-comexception-0x80005000-unknown-error-0x80005000.aspx?CommentPosted=true#commentmessage ..

In Addition i did this also which resolved my issue ::

Issue i was getting ==>

[System.Runtime.InteropServices.COMException] {"Access is denied.\r\n"}     System.Runtime.InteropServices.COMException

ErrorCode 0x80070005

Solution which get my issue resolved :: After doing all above stated things in Link.

I have to do this also to resolve Access denied error ==> Run all Administrator in admin approval mode ==> Disabled ..