App_Data サブフォルダーへの書き込みアクセスを必要とする ASP.NET アプリケーションがあります。アプリケーションのデプロイに使用される MSI は、アクセス許可を正しく設定しようとしますが、それにもかかわらず、アクセス許可が時々間違っているようです。ほとんどのアプリケーションは、この許可がなくても正常に動作します。権限が間違っていると、アプリケーションが起動しないことを望みます。
IIS ユーザー コンテキストに対して必要なアクセス許可が正しいことを確認するためのベスト プラクティスは何ですか? 理想的には、問題を修正するための簡単な手順を表示したいと考えています。そして、できるだけ多くの誤った構成でメッセージが表示されるようにします。
以下は、おそらくより良い方法または標準的な方法があることに気付くまで、これまでに試したことを説明しています。
これ入れてみたApplication_Start()
protected void Application_Start(Object sender, EventArgs e)
{
// Assert permissions on writeable folders are correct
var permissionsChecker = new AppDataPermissionsChecker();
permissionsChecker.AssertFolderIsWriteable(
HttpContext.Current.Server.MapPath("~/App_Data"));
// remainder of Application_Start()...
}
はAppDataPermissionsChecker
次のように定義されます。
public class AppDataPermissionsChecker
{
private bool CanWriteAccessToFolder(string folderPath)
{
try
{
// Attempt to get a list of security permissions from the folder.
// This will raise an exception if the path is read only or do not have access to view the permissions.
DirectorySecurity directorySecurity = Directory.GetAccessControl(folderPath);
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
}
public void AssertFolderIsWriteable(string folderPath)
{
if (!Directory.Exists(folderPath))
throw new Exception(String.Format("The {0} folder does not exist.", folderPath));
if (!CanWriteAccessToFolder(folderPath))
throw new Exception(String.Format("The ASPNET user does not have "
+ "access to the {0} folder. Please ensure the ASPNET user has "
+ "read/write/delete access on the folder. See 'The App_Data folder' "
+ "here: http://msdn.microsoft.com/en-us/library/06t2w7da.aspx'",
folderPath));
}
}
権利が正しくない場合、これは醜い例外をスローするだろうと思っていましたが (何もないよりはましです)、状況によっては HTTP エラー 503 が発生するだけです。