-
Aug 20th, 2015, 11:26 PM
#1
Thread Starter
Frenzied Member
[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.
-
Aug 21st, 2015, 03:03 AM
#2
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
-
Aug 21st, 2015, 11:19 AM
#3
Thread Starter
Frenzied Member
Re: Access Denied - Creating Directories when not authenticated
Originally Posted by NeedSomeAnswers
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.
-
Aug 21st, 2015, 11:44 AM
#4
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.
-
Aug 21st, 2015, 03:51 PM
#5
Thread Starter
Frenzied Member
Re: Access Denied - Creating Directories when not authenticated
Originally Posted by szlamany
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.
-
Aug 21st, 2015, 03:59 PM
#6
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
-
Aug 21st, 2015, 04:02 PM
#7
Re: Access Denied - Creating Directories when not authenticated
Is that what MapPath does?
-
Aug 21st, 2015, 04:25 PM
#8
Thread Starter
Frenzied Member
Re: Access Denied - Creating Directories when not authenticated
Originally Posted by szlamany
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.
-
Aug 21st, 2015, 04:27 PM
#9
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|