Quan Tran
Quan Tran

Reputation: 11

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

Upvotes: 0

Views: 1717

Answers (3)

Mike Wills
Mike Wills

Reputation: 21275

I think this is what you are looking for. Sorry, I don't have access to my code at work that I know works right now. It was really simple to do and worked perfectly.

If you want my working code, please let me know and I'll pull that tomorrow.

UPDATE: Sorry, I didn't post my code earlier, we are swamped at work.

NetworkCredential nc = new NetworkCredential("user", "password", "domain");

using (new NetworkConnection(@"\\server\directory1\directory2", nc))
{
    // your IO logic here
}

Upvotes: 0

Quan Tran
Quan Tran

Reputation: 11

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.

Upvotes: 1

danny117
danny117

Reputation: 5651

Two solutions. Best Practice. Setup the iseries to use the same domain controller as everything else on your network. Then it will know the asp.net account requesting access and allow access to the IFS.

or

Setup a mapped drive on the machine hosting the asp.net page that points to the IFS. The mapped drive will have credentials saved in the vault.

Upvotes: 1

Related Questions