简体   繁体   中英

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). 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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