问题
I am finishing my web project and have added a little error log feature. It writes details of exceptions in a dated file in the App_Data folder.
When I do this on my development machine, it works. But when I switch over to my IIS machine, it stops working and I get a UnauthorizedAccessException.
Stack Trace:
[UnauthorizedAccessException: Der Zugriff auf den Pfad "c:\inetpub\wwwroot\App_Data\Error_Logs\" wurde verweigert.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +9726526
System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj) +9431442
System.IO.Directory.CreateDirectory(String path) +146
WerIstWo.Logger.writeToLog(String text) in c:\Users\Teichler\Dropbox\Praktikum\IHK-Projekt\WerIstWo\WerIstWo\Logger.cs:18
WerIstWo.NeuerBenutzer.btnSpeichern_Click(Object sender, EventArgs e) in c:\Users\Teichler\Dropbox\Praktikum\IHK-Projekt\WerIstWo\WerIstWo\NeuerBenutzer.aspx.cs:325
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
Code:
public static void writeToLog( string text )
{
string date = DateTime.Today.ToShortDateString().ToString();
string path = AppDomain.CurrentDomain.GetData( "DataDirectory" ).ToString() + "\\Error_Logs\\";
if (!Directory.Exists( path ))
{
Directory.CreateDirectory( path );
}
string fullPath = path + date + ".txt";
if (!File.Exists( path + date ))
{
File.Create( fullPath ).Close();
}
using (StreamWriter writer = new StreamWriter( fullPath, true ))
{
writer.Write( text );
}
}
In the error details, it tells me that I need to set permission for IUSR_MachineName to this folder but I'm not sure if this is a good idea or how to do it in IIS 5.1 (which I am using).
回答1:
That is indeed what you need to do. On your development machine, the IIS (or IIS express) executable has write permissions for your App_Data
directory. On a production machine, you usually don't by default. There is only one solution and that is to grant the IUSR_MachineName
user write permission on the directory. There is nothing fundamentally wrong with that, as long as you are aware that this means that other websites running in the same IIS instance potentially could also write to that directory.
来源:https://stackoverflow.com/questions/18077122/no-access-permission-for-app-data