Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I set up the following test in a mini - windows app. The idea is that the code could be used in an ASP.NET project later on. The general problem here is authentication... I have all the user rights to write to the mentioned server, but I don't know how to pass my credentials allong with the write command...

 

The following is working: (writing to a new or overwriting an existing file locally)

 

private void button1_Click(object sender, System.EventArgs e)
     {
        StreamWriter sw = null;
        try
        {
           sw = File.CreateText(@"C:\new text files\test.txt");
           sw.Write("testing the method again again");
        }
        catch (Exception exc)
        {
           MessageBox.Show(exc.ToString());
        }
        finally
        {
           if (sw != null)
           {
              sw.Flush();
              sw.Close();
           }
        }
     }

 

 

not working: (Writing the file to a network share)

 

private void button1_Click(object sender, System.EventArgs e)
     {
        StreamWriter sw = null;
        try
        {
           sw = File.CreateText(@"\\10.0.0.20\files$\test.txt");
           sw.Write("testing the method again again");
        }
        catch (Exception exc)
        {
           MessageBox.Show(exc.ToString());
        }
        finally
        {
           if (sw != null)
           {
              sw.Flush();
              sw.Close();
           }
        }
     }

 

error:

System.UnauthorizedAccessException: Access to the path "\\10.0.0.20\files$\test.txt" is denied.

Also, mapping the "\\10.0.0.20\files$" folder to a new drive name (for example "Z:\") and using syntax as

File.CreateText(@"Z:\test.txt");

is not working.

 

Is there a way to attach some user credentials to the command, so it becomes authorized to write the file?

qrt
  • *Gurus*
Posted
You don't "pass" credentials to the CreateFile() or Write() methods. The credentials used will be the ones under which the thread is running. If you need for the code to access the files and directories in a specific path, you will need to configure file system ACLs to allow the user account under which the process (or thread) is running to access them. In most cases this involves adding the user account you're using, or adding the "ASPNET" account for Windows XP or Windows 2000 or the "Network Service" account for Windows Server 2003 to allow ASP.NET applications to access the files/directories.
Posted

Thanx Derek,

 

How to configure the ACLs? Should I make from the ASP.NET account on the server running the project a Domain account, and add that one to the server where the files should be written? Should I then right click the folder for the text files, choose properties and from the security tab add the user there? I guess I 'm a little confused.

 

Kurt

qrt

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...