Writing a text file to the file system

Discussion in 'ASP .Net Security' started by cwbp, Mar 21, 2005.

  1. cwbp

    cwbp Guest

    Using Visual Studio C#

    When I ran the following code:

    System.IO;

    private void Button1_Click(object sender, System.EventArgs e)
    {
    //FileStream fs = File.Create(Server.MapPath("test.txt"));
    FileStream fs = File.Create("C:\\MYSAVEDFILES\\test.txt");
    StreamWriter sw = new StreamWriter(fs);
    sw.Write(TextBox1.Text);
    sw.Close();
    fs.Close();

    }

    I initially got an error message that "access was denied." The message
    suggested that I give ASP.NET user access rights/permissions to the folder.

    I then [manually] gave the logged in user write permission to the folder.

    Then, when I ran the above code, the text file was created.

    Is there another way [i.e., programatically using C#] to allow my code to
    write a text file to the file system without giving access rights to a user?
    Can the permissions be given to the app [the code] instead of to a user?

    Any suggestions would be appreciated.

    Thanks.

    bebop
    cwbp, Mar 21, 2005
    #1
    1. Advertising

  2. Are you using impersonation in your application? The app should use the
    ASP.NET process account's credentials unless you are impersonating.

    If there is confusion, always check the value of
    System.Security.Principal.WindowsIdentity.GetCurrent().Name.

    It is also possible to impersonate in code or move the file writing code to
    a COM+ component running under a different identity, but that probably isn't
    needed here.

    Joe K.

    "cwbp" <> wrote in message
    news:...
    > Using Visual Studio C#
    >
    > When I ran the following code:
    >
    > System.IO;
    >
    > private void Button1_Click(object sender, System.EventArgs e)
    > {
    > //FileStream fs = File.Create(Server.MapPath("test.txt"));
    > FileStream fs = File.Create("C:\\MYSAVEDFILES\\test.txt");
    > StreamWriter sw = new StreamWriter(fs);
    > sw.Write(TextBox1.Text);
    > sw.Close();
    > fs.Close();
    >
    > }
    >
    > I initially got an error message that "access was denied." The message
    > suggested that I give ASP.NET user access rights/permissions to the
    > folder.
    >
    > I then [manually] gave the logged in user write permission to the folder.
    >
    > Then, when I ran the above code, the text file was created.
    >
    > Is there another way [i.e., programatically using C#] to allow my code to
    > write a text file to the file system without giving access rights to a
    > user?
    > Can the permissions be given to the app [the code] instead of to a user?
    >
    > Any suggestions would be appreciated.
    >
    > Thanks.
    >
    > bebop
    >
    >
    >
    Joe Kaplan \(MVP - ADSI\), Mar 22, 2005
    #2
    1. Advertising

  3. cwbp

    M Guest

    >> ASP.NET process account's

    Could you explain exactly how one uses this. How does one get get his hands
    on this, programatically?

    Thanks.



    "Joe Kaplan (MVP - ADSI)" wrote:

    > Are you using impersonation in your application? The app should use the
    > ASP.NET process account's credentials unless you are impersonating.
    >
    > If there is confusion, always check the value of
    > System.Security.Principal.WindowsIdentity.GetCurrent().Name.
    >
    > It is also possible to impersonate in code or move the file writing code to
    > a COM+ component running under a different identity, but that probably isn't
    > needed here.
    >
    > Joe K.
    >
    > "cwbp" <> wrote in message
    > news:...
    > > Using Visual Studio C#
    > >
    > > When I ran the following code:
    > >
    > > System.IO;
    > >
    > > private void Button1_Click(object sender, System.EventArgs e)
    > > {
    > > //FileStream fs = File.Create(Server.MapPath("test.txt"));
    > > FileStream fs = File.Create("C:\\MYSAVEDFILES\\test.txt");
    > > StreamWriter sw = new StreamWriter(fs);
    > > sw.Write(TextBox1.Text);
    > > sw.Close();
    > > fs.Close();
    > >
    > > }
    > >
    > > I initially got an error message that "access was denied." The message
    > > suggested that I give ASP.NET user access rights/permissions to the
    > > folder.
    > >
    > > I then [manually] gave the logged in user write permission to the folder.
    > >
    > > Then, when I ran the above code, the text file was created.
    > >
    > > Is there another way [i.e., programatically using C#] to allow my code to
    > > write a text file to the file system without giving access rights to a
    > > user?
    > > Can the permissions be given to the app [the code] instead of to a user?
    > >
    > > Any suggestions would be appreciated.
    > >
    > > Thanks.
    > >
    > > bebop
    > >
    > >
    > >

    >
    >
    >
    M, Mar 22, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BLF
    Replies:
    4
    Views:
    1,267
    Mike Treseler
    Aug 7, 2004
  2. HNguyen
    Replies:
    4
    Views:
    2,384
    HNguyen
    Dec 21, 2004
  3. =?Utf-8?B?Y3dicDE1?=

    Writing a text file to the file system

    =?Utf-8?B?Y3dicDE1?=, Mar 21, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    517
    shiva_ananthoju
    Oct 29, 2005
  4. =?Utf-8?B?Y3dicA==?=

    Writing a text file to the file system

    =?Utf-8?B?Y3dicA==?=, Mar 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    315
    =?Utf-8?B?Y3dicA==?=
    Mar 21, 2005
  5. icedragon
    Replies:
    0
    Views:
    636
    icedragon
    Jul 13, 2010
Loading...

Share This Page