Search code examples
c#asp.netibm-midrangeiseries-navigator

Write a simple text file to IBM iSeries IFS from my ASP.NET web app


So part of my job is to write a file to iSeries IFS, or this path in particular \ServerIPAddress\home\test\ I have an ASP.NET web application to do this, and this is the code (C#) I use to write a simple text file to that directory:

        string filename = @"\\SomeIPAdress\home\test\test.txt";
        byte[] file = Encoding.ASCII.GetBytes("hello world");
        FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
        fs.Write(file, 0, file.Length);
        fs.Close();

When executing this code, the program gives me "Access Denied" error

Exception Details: System.UnauthorizedAccessException: Access to the path '\SomeIPAddress\home\test\test.txt' is denied.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity ...

I can access this directory \SomeIPAddress\home\test using windows file explorer using IBM UID and password, and I can create and edit a text file manually as well.

I know it has to have something to do with granting access right to my ASP.NET app by providing that UID and password, but I can't quite figure it out, and I have been stuck for few days.

Let me know if you need any extra information. Thanks for the help


Solution

  • Thanks Mike Wills for leading me to a solution. This is the code I use to connect to the network share using P/Invoke WNet Connection, which is from this answer here

    DisconnectFromShare(@"\\server-a\DBFiles", true); //Disconnect in case we are currently connected with our credentials;
    
    ConnectToShare(@"\\server-a\DBFiles", username, password); //Connect with the new credentials
    
    if (!Directory.Exists(@"\\server-a\DBFiles\"))
        Directory.CreateDirectory(@"\\server-a\DBFiles\"+Test);   
    File.Copy("C:\Temp\abc.txt", @"\\server-a\DBFiles\");
    
    DisconnectFromShare(@"\\server-a\DBFiles", false); //Disconnect from the server.
    

    Thanks guys for the help.