Problems impersonating and releasing .NET files

I have a webpage that allows a user to upload a file to a network share. When I launch a webpage locally (in VS 2008) and try to upload a file, it works! However, when I deploy a website to a web server and try to upload a file through a web page, it does not work!

The error returned to me on the web server says: "Access to the path \ 05prd1 \ emp \ test.txt" is denied. Therefore, this is a permission issue.

The network resource is configured to provide full access to me (NT authentication) and NETWORK SERVICE (this is the default .NET account and what we installed in our IIS application pool as the default user for this website).

I tried this with and without impersonation on a web server, and none of the methods work, but both methods work on my local machine (in other words, using and without impersonation on my local machine).

Below is the code that downloads the file. Please note that the code below includes impersonation, but as I said above, I tried it with and without impersonation, and that didn't matter.

if (fuCourses.PostedFile != null && fuCourses.PostedFile.ContentLength > 0) {
    System.Security.Principal.WindowsImpersonationContext impCtx;
    impCtx =
        ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();

    try {
        lblMsg.Visible = true;

        // The courses file to be uploaded
        HttpPostedFile file = fuCourses.PostedFile;
        string fName = file.FileName;
        string uploadPath = "\\\\05prd1\\emp\\";

        // Get the file name
        if (fName.Contains("\\")) {
            fName = fName.Substring(
                fName.LastIndexOf("\\") + 1);
        }

        // Delete the courses file if it is already on \\05prd1\emp
        FileInfo fi = new FileInfo(uploadPath + fName);
        if (fi != null && fi.Exists) {
            fi.Delete();
        }

        // Open new file stream on \\05prd1\emp and read bytes into it from file upload
        FileStream fs = File.Create(uploadPath + fName, file.ContentLength);

        using (Stream stream = file.InputStream) {
            byte[] b = new byte[4096];
            int read;

            while ((read = stream.Read(b, 0, b.Length)) > 0) {
                fs.Write(b, 0, read);
            }
        }

        fs.Close();

        lblMsg.Text = "File Successfully Uploaded";
        lblMsg.ForeColor = System.Drawing.Color.Green;
    }
    catch (Exception ex) {
        lblMsg.Text = ex.Message;
        lblMsg.ForeColor = System.Drawing.Color.Red;
    }
    finally {
        impCtx.Undo();
    }
}

Any help on this would be greatly appreciated!

+3
source share
2 answers

, Kerberos - . Kerberos . , , . . , .

, . , ( , ).

, UNC. 2- , , , , , -. , , / - UNC. .

+1

, IIS.

Windows Integrated/NTLM, , , Kerberos , (-) - " double-hop".

Basic ( IIS) Forms ( ASP.NET), . / .

, , < identity impersonate = "true" > .

KB 306158 .

+1

Source: https://habr.com/ru/post/1739185/


All Articles