PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Access network share from ASP.NET

 
 
Yuriy Galanter
Guest
Posts: n/a
 
      10th Mar 2008
Hi all,

I need to access a file on a network share from an ASP.NET application
(using methods like file.readalltext). Of course ASP.NET doesn't have access
to that share. But I do have both UserID and password of a user who does
have access. How do I use them to supply credentials for file.readalltext
method (similar how I can do that for WebClient with NetworkCredentials)?

Thanks!

Yuriy.


 
Reply With Quote
 
 
 
 
sloan
Guest
Posts: n/a
 
      10th Mar 2008

Quickest way is impersonation with asp.net.

Inside of

<system.web>


<authentication mode="Windows" />
<identity impersonate="true" userName="mycompany\myname" password="mypwd"
/>

</system.web>


That should give you something to search on.


"Yuriy Galanter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi all,
>
> I need to access a file on a network share from an ASP.NET application
> (using methods like file.readalltext). Of course ASP.NET doesn't have
> access to that share. But I do have both UserID and password of a user who
> does have access. How do I use them to supply credentials for
> file.readalltext method (similar how I can do that for WebClient with
> NetworkCredentials)?
>
> Thanks!
>
> Yuriy.
>



 
Reply With Quote
 
George Ter-Saakov
Guest
Posts: n/a
 
      10th Mar 2008
Here you go....Exactly what you want without using impersonation.

#region WIN API Declarations

//used in calling WNetAddConnection2

[StructLayout(LayoutKind.Sequential)]

public struct NETRESOURCE

{

public int dwScope;

public int dwType;

public int dwDisplayType;

public int dwUsage;

[MarshalAs(UnmanagedType.LPStr)]

public string lpLocalName;

[MarshalAs(UnmanagedType.LPStr)]

public string lpRemoteName;

[MarshalAs(UnmanagedType.LPStr)]

public string lpComment;

[MarshalAs(UnmanagedType.LPStr)]

public string lpProvider;

}

//WIN32API - WNetAddConnection2

[DllImport("mpr.dll",

CharSet = System.Runtime.InteropServices.CharSet.Auto)]

private static extern int WNetAddConnection2A(

[MarshalAs(UnmanagedType.LPArray)] NETRESOURCE[] lpNetResource,

[MarshalAs(UnmanagedType.LPStr)] string lpPassword,

[MarshalAs(UnmanagedType.LPStr)] string lpUserName,

int dwFlags);

[DllImport("mpr.dll",

CharSet = System.Runtime.InteropServices.CharSet.Auto)]

private static extern int WNetCancelConnection2A(

[MarshalAs(UnmanagedType.LPStr)] string lpName,

int dwFlags, int fForce);

#endregion

private byte[] GetFSMSFile(string sFile)

{

NETRESOURCE[] nr = new NETRESOURCE[1];

nr[0].lpRemoteName = _sFSMSShare;

nr[0].lpLocalName = ""; //mLocalName;

nr[0].dwType = 1; //disk

nr[0].dwDisplayType = 0;

nr[0].dwScope = 0;

nr[0].dwUsage = 0;

nr[0].lpComment = "";

nr[0].lpProvider = "";

int iErr = WNetAddConnection2A(nr, _sFSMSShareUserPassword, _sFSMSShareUser,
0);

if (iErr > 0)

throw new Exception("Can not connect to FSMS share folder");

FileStream st = null;

try

{

st = new FileStream(_sFSMSShare + "\\" + sFile, FileMode.Open);

int iLen = (int)st.Length;

byte []b = new byte[iLen];

st.Read(b, 0, iLen);

return b;

}

finally

{

if( st != null )

st.Close();

WNetCancelConnection2A(_sFSMSShare, 0, -1);

}

}





"Yuriy Galanter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi all,
>
> I need to access a file on a network share from an ASP.NET application
> (using methods like file.readalltext). Of course ASP.NET doesn't have
> access to that share. But I do have both UserID and password of a user who
> does have access. How do I use them to supply credentials for
> file.readalltext method (similar how I can do that for WebClient with
> NetworkCredentials)?
>
> Thanks!
>
> Yuriy.
>



 
Reply With Quote
 
Yuriy Galanter
Guest
Posts: n/a
 
      12th Mar 2008
Thanks guys, you pointed me in the right direction and I got it working.
Great group!

Yuriy.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Network access to an XP Pro Share Cap Kirk Windows XP Security 1 5th Jun 2006 07:45 PM
Access to network share via VPN Chris Microsoft Windows 2000 Networking 1 17th Mar 2005 03:49 PM
can't access network share in DOS =?Utf-8?B?SGVucmlr?= Microsoft Access Security 0 15th Oct 2004 07:03 PM
Access Network Share =?Utf-8?B?SEsgZ3V5?= Microsoft C# .NET 1 13th Aug 2004 03:51 PM
Microsoft Access 2002 (XP) using DSN on the network share to access MSSQL2000 Shehzad Shabbir Microsoft Access 0 24th Mar 2004 07:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:36 PM.