Results 1 to 9 of 9

Thread: [RESOLVED] Access Denied - Creating Directories when not authenticated

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Resolved [RESOLVED] Access Denied - Creating Directories when not authenticated

    As part of my new user setup, when they sign up for an account I create want to be able to create a directory under a Documents folder for them and then write a web.config file in that folder limiting access to it to the new account. However because they are still in the process they are an unauthenticated user at that point (Forms authentication), and I get an error that they don't have rights to that folder I want to create their sub-folder in. What do I need to set the folder security at to be able to create a folder and a file in that folder from code?
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Access Denied - Creating Directories when not authenticated

    However because they are still in the process they are an unauthenticated user at that point (Forms authentication), and I get an error that they don't have rights to that folder I want to create their sub-folder in. What do I need to set the folder security at to be able to create a folder and a file in that folder from code?
    Where are you trying to create this folder on the file system ??

    Surely you are creating the folder in code, and therefore creating the folder should have nothing to do with the users rights?? your application should have the rights!!

    However a Web application does not have the same access to the file system that a windows application it is limited to creating folders and files in its root directory.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Access Denied - Creating Directories when not authenticated

    Quote Originally Posted by NeedSomeAnswers View Post
    Where are you trying to create this folder on the file system ??

    Surely you are creating the folder in code, and therefore creating the folder should have nothing to do with the users rights?? your application should have the rights!!

    However a Web application does not have the same access to the file system that a windows application it is limited to creating folders and files in its root directory.
    I'm doing it under a ~\Documents\ directory. How do I determine what account the application runs under in debug on the local machine? In production it is Network Service in the app pool. I don't know how to check that in debug. I've never had issue on the local machine until I tried to do it when I am not authenticated. If I look at the directory security I see an Authenticated User having rights.

    Sorry I'm awful with security.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Access Denied - Creating Directories when not authenticated

    So you have created a Documents folder in your wwwroot top level directory.

    And now you want CODE BEHIND to create a sub-directory.

    Show the code you are using to create this directory.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Access Denied - Creating Directories when not authenticated

    Quote Originally Posted by szlamany View Post
    So you have created a Documents folder in your wwwroot top level directory.

    And now you want CODE BEHIND to create a sub-directory.

    Show the code you are using to create this directory.
    Sure.

    m_strSavePath = "~\Documents\#" where number is a new ID. I bolded where it excepts because I don't have proper rights.



    Code:
    Public Function CreateCompanyDirectoriesAndWriteWebConfigs(ByVal intCompanyID As Int32) As Boolean
            Dim boolResult As Boolean = True
            Dim oSQL As New clsSQL
    
            Try
                Dim strUserRole As String = intCompanyID.ToString() & "_reg"
                Dim strAdminRole As String = intCompanyID.ToString() & "_admin"
    
                Roles.CreateRole(strUserRole)
                Roles.CreateRole(strAdminRole)
    
                Directory.CreateDirectory(m_strSavePath)
                Directory.CreateDirectory(m_strSavePath & "\Customer")
                Directory.CreateDirectory(m_strSavePath & "\Internal")
                Directory.CreateDirectory(m_strSavePath & "\Shared")
    
                Dim strWebConfig As String = m_strSavePath & "\Customer\web.config"
    
                Dim fs As New FileStream(HttpContext.Current.Server.MapPath(strWebConfig), FileMode.Create, FileAccess.Write)
                Dim sw As New StreamWriter(fs)
    
                sw.WriteLine("<?xml version=""1.0""?>")
                sw.WriteLine("<configuration>")
                sw.WriteLine("")
                sw.WriteLine("<system.web>")
                sw.WriteLine("<authorization>")
                sw.WriteLine("<allow roles=""" & strUserRole & """/>")
                sw.WriteLine("<allow roles=""" & strAdminRole & """/>")
                sw.WriteLine("<deny users=""*""/>")
                sw.WriteLine("</authorization>")
                sw.WriteLine("</system.web>")
                sw.WriteLine("")
                sw.WriteLine("</configuration>")
    
                sw.Close()
                fs.Close()
    
                strWebConfig = m_strSavePath & "\Internal\web.config"
    
                fs = New FileStream(HttpContext.Current.Server.MapPath(strWebConfig), FileMode.Create, FileAccess.Write)
                sw = New StreamWriter(fs)
    
                sw.WriteLine("<?xml version=""1.0""?>")
                sw.WriteLine("<configuration>")
                sw.WriteLine("")
                sw.WriteLine("<system.web>")
                sw.WriteLine("<authorization>")
                sw.WriteLine("<allow roles=""" & strAdminRole & """/>")
                sw.WriteLine("<deny users=""*""/>")
                sw.WriteLine("</authorization>")
                sw.WriteLine("</system.web>")
                sw.WriteLine("")
                sw.WriteLine("</configuration>")
    
                sw.Close()
                fs.Close()
    
                strWebConfig = m_strSavePath & "\Shared\web.config"
    
                fs = New FileStream(HttpContext.Current.Server.MapPath(strWebConfig), FileMode.Create, FileAccess.Write)
                sw = New StreamWriter(fs)
    
                sw.WriteLine("<?xml version=""1.0""?>")
                sw.WriteLine("<configuration>")
                sw.WriteLine("")
                sw.WriteLine("<system.web>")
                sw.WriteLine("<authorization>")
                sw.WriteLine("<allow roles=""" & strUserRole & """/>")
                sw.WriteLine("<allow roles=""" & strAdminRole & """/>")
                sw.WriteLine("<deny users=""*""/>")
                sw.WriteLine("</authorization>")
                sw.WriteLine("</system.web>")
                sw.WriteLine("")
                sw.WriteLine("</configuration>")
    
                sw.Close()
                fs.Close()
    
            Catch ex As Exception
                boolResult = False
    
                m_strLastError = ex.Message
            End Try
    
            Return boolResult
        End Function
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Access Denied - Creating Directories when not authenticated

    When I touch the virtual directory from backend code I use the whole C:\ path and it always works.

    Code:
    <add key="logfolder" value="C:\inetpub\wwwroot\logs" />
    Code:
    Dim strLogFolder As String = System.Web.Configuration.WebConfigurationManager.AppSettings("logfolder")
    strLogFile = Path.Combine(strLogFolder, strLogFile)
    Using fs1 As FileStream = New FileStream(strLogFile, FileMode.Append, FileAccess.Write)
    Do not use the ~\ folder specification

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Access Denied - Creating Directories when not authenticated

    Is that what MapPath does?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Access Denied - Creating Directories when not authenticated

    Quote Originally Posted by szlamany View Post
    Is that what MapPath does?
    Yep, it is, and I missed that I wasn't doing it on my directory creation. Thanks so much that fixed it.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] Access Denied - Creating Directories when not authenticated

    I've got clients that might map a share for things like this and MapPath won't handle that.

    I always web.config my paths, fwiw.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width