0

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

Community
  • 1
  • 1
Quan Tran
  • 11
  • 1
  • 5
  • What is the user running the application pool of your site? Check the access rights of that user. – Jeroen Heier Apr 26 '17 at 18:44
  • I believe it is IIS APPPOOL\DefaultAppPool. How can I check access rights of that user? – Quan Tran Apr 26 '17 at 19:15
  • 1
    You should probably create a dedicated service account for that app pool that has access to that resource. Once you create it, you can sign on as the service account to verify the permissions work, or just use [RunAs](https://technet.microsoft.com/en-us/library/cc771525(v=ws.11).aspx). – John Wu Apr 26 '17 at 19:21

3 Answers3

1

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.

danny117
  • 5,581
  • 1
  • 26
  • 35
1

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.

Community
  • 1
  • 1
Quan Tran
  • 11
  • 1
  • 5
0

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
}
Community
  • 1
  • 1
Mike Wills
  • 20,959
  • 28
  • 93
  • 149
  • Thank you! I am trying some solution from your link, but it'd be great if you can post your code as well. Thanks again. – Quan Tran Apr 27 '17 at 14:33
  • Updated the answer with the code sample I was going to provide. Sorry, it's jumping from one thing to another without time to breath or hang out on SO. – Mike Wills May 01 '17 at 13:45