简体   繁体   English

在ASP.Net Web窗体中动态创建单个用户文件夹

[英]Dynamically create individual user folders in ASP.Net Web Forms

I created an ASP.Net website with the Web Forms Template - - with its default SQL Database for membership - - in Visual Studio (copies in VS 2015 and VS 2017). 我在Visual Studio中使用Web表单模板-具有默认的SQL数据库成员身份-创建了一个ASP.Net网站(在VS 2015和VS 2017中复制)。 I am familiar with dynamically creating folders, but not to the extent of what I am trying to achieve. 我熟悉动态创建文件夹,但是不熟悉我想要实现的范围。 What I am trying to achieve is dynamically create a folder accessible to only one individual user. 我试图做到的,是动态创建只有一个单独用户访问的文件夹。 Ideally this should happen right as the individual user registers their account. 理想情况下,这应在单个用户注册其帐户时正确进行。

What changes do I have to make to the website in order to create folders accessible to an individual user? 为了创建个人用户可以访问的文件夹,我必须对网站进行哪些更改?

Here is the basic code I normally use to create folders: 这是我通常用于创建文件夹的基本代码:

protected void CreateUser_Click(object sender, EventArgs e)
{
    var manager = new UserManager();
    var user = new ApplicationUser() { UserName = UserName.Text };
    IdentityResult result = manager.Create(user, Password.Text);
    if (result.Succeeded)
    {
        IdentityHelper.SignIn(manager, user, isPersistent: false);
        IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
        string name = user.UserName;
        string foldername = "~/" + name + "Folder/";
        string johndoefolderpath = Server.MapPath(foldername);
        if (!Directory.Exists(johndoefolderpath))
        {
            Directory.CreateDirectory(johndoefolderpath);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Directory already exists.');", true);
        }
    }
    else
    {
        ErrorMessage.Text = result.Errors.FirstOrDefault();
    }
}

I assume you are referencing NTFS permissions. 我假设您正在引用NTFS权限。 Your issue is that when you create the folder it is created with the permissions of the account that created it - which is probably NOT the user that you are creating the folder for since for that to happen every potential user would need create permissions on that folder. 您的问题是,当您创建文件夹时,它是使用创建该文件夹的帐户的权限创建的-可能不是您正在创建该文件夹的用户,因为要实现此目的,每个潜在用户都需要对该文件夹创建权限。 NOT a good security practice. 不好的安全习惯。

So, what you will need to do is create the folder as you do above and then change the permissions on the created folder to add write permissions for that user. 因此,您需要做的就是像上面一样创建文件夹,然后更改对创建的文件夹的权限以为该用户添加写权限。 Take a look at the FileSystemAccessRule class for how to handle assigning those permissions. 查看FileSystemAccessRule类,了解如何处理分配这些权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM