gpt4 book ai didi

asp.net - 如何修改Web安装项目中的文件夹权限?

转载 作者:行者123 更新时间:2023-12-02 11:23:20 28 4
gpt4 key购买 nike

我正在使用 Web 安装项目来安装 ASP.NET 应用程序,该应用程序需要写入主虚拟目录文件夹下存在的文件夹。如何配置安装项目以授予 ASPNET 用户对该文件夹的权限?

最佳答案

实现方法是创建一个派生自System.Configuration.Install.Installer的类。覆盖 Install() 方法。以下是更改目录和文件权限的示例,您可能不希望如此宽松,但这取决于您的安全上下文。为了使其工作,安装项目必须将其作为自定义操作运行。从此类所在的任何项目中添加“主输出”。您还需要将目录传递给其属性中的自定义操作。第一个变量名称必须与代码匹配。像这样:/targetdir="[TARGETDIR]\"

[RunInstaller(true)]
public partial class SetPermissions : Installer
{
private const string STR_targetdir = "targetdir";
private const string STR_aspnetUser = "ASPNET";

public SetPermissions()
{
InitializeComponent();
}

public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);

Context.LogMessage(
Context.Parameters
.Cast<DictionaryEntry>()
.Select(entry => String.Format("String = {0} Value = {1}", entry.Key, entry.Value))
.Aggregate(new StringBuilder("From install\n"), (accumulator, next) => accumulator.AppendLine(next))
.ToString()
);

string targetDir = Context.Parameters[STR_targetdir];
string dbDir = Path.Combine(targetDir, "db");

AddFullControlPermissionToDir(dbDir, STR_aspnetUser);
string rimdbSqliteFilename = Path.Combine(dbDir, "db.sqlite");
AddFullControlPermissionToFile(rimdbSqliteFilename, STR_aspnetUser);
string logsDir = Path.Combine(targetDir, "logs");
AddFullControlPermissionToDir(logsDir, STR_aspnetUser);
}

private static void AddFullControlPermissionToDir(string dir, string user)
{
DirectorySecurity directorySecurity = Directory.GetAccessControl(dir);
directorySecurity.AddAccessRule(
new FileSystemAccessRule(
user,
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow));
Directory.SetAccessControl(dir, directorySecurity);
}

private static void AddFullControlPermissionToFile(string filename, string user)
{
FileSecurity fileSecurity = File.GetAccessControl(filename);
fileSecurity.AddAccessRule(
new FileSystemAccessRule(
user,
FileSystemRights.FullControl,
AccessControlType.Allow));
File.SetAccessControl(filename, fileSecurity);
}
}

关于asp.net - 如何修改Web安装项目中的文件夹权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1639860/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com