Access network folder using username and passwordhttp://forums.asp.net/t/1797387.aspx/1?Access+network+folder+using+username+and+passwordMon, 30 Apr 2012 06:15:31 -040017973874953275http://forums.asp.net/p/1797387/4953275.aspx/1?Access+network+folder+using+username+and+passwordAccess network folder using username and password <p>Hi friends,</p> <p>I need to access a network folder using the login details in order by to copy the files from it.</p> <p>I am using the below code snippet inorder to do that;</p> <p>string str = &quot;NET USE &quot; <span size="2" style="font-size:small">&#43; directory &#43; </span> <span color="#a31515" size="2" style="font-size:small; color:#a31515"><span color="#a31515" size="2" style="font-size:small; color:#a31515">&quot; /user:&quot;</span></span><span size="2" style="font-size:small"> &#43; username &#43; </span><span color="#a31515" size="2" style="font-size:small; color:#a31515"><span color="#a31515" size="2" style="font-size:small; color:#a31515">&quot; &quot;</span></span><span size="2" style="font-size:small"> &#43; password</span></p> <p>Process <span size="2" style="font-size:small">process = </span><span color="#0000ff" size="2" style="font-size:small; color:#0000ff"><span color="#0000ff" size="2" style="font-size:small; color:#0000ff">new</span></span><span size="2" style="font-size:small"> </span><span color="#2b91af" size="2" style="font-size:small; color:#2b91af"><span color="#2b91af" size="2" style="font-size:small; color:#2b91af">Process</span></span><span size="2" style="font-size:small"> { StartInfo = </span><span color="#0000ff" size="2" style="font-size:small; color:#0000ff"><span color="#0000ff" size="2" style="font-size:small; color:#0000ff">new</span></span><span size="2" style="font-size:small"> </span><span color="#2b91af" size="2" style="font-size:small; color:#2b91af"><span color="#2b91af" size="2" style="font-size:small; color:#2b91af">ProcessStartInfo</span></span><span size="2" style="font-size:small"> { WindowStyle = </span><span color="#2b91af" size="2" style="font-size:small; color:#2b91af"><span color="#2b91af" size="2" style="font-size:small; color:#2b91af">ProcessWindowStyle</span></span><span size="2" style="font-size:small">.Hidden, FileName = </span><span color="#a31515" size="2" style="font-size:small; color:#a31515"><span color="#a31515" size="2" style="font-size:small; color:#a31515">&quot;cmd.exe&quot;</span></span><span size="2" style="font-size:small">, Arguments = args } };</span></p> <p>process.Start();</p> <p>process.WaitForExit(5000);</p> <p>process.Close();</p> <p><span size="2" style="font-size:small">but the problem is sometime i am able to access the network folder and sometimes it gives me a error saying Failed to login;</span></p> <p><span size="2" style="font-size:small">Not sure on this.</span></p> <p><span size="2" style="font-size:small">Did&nbsp;anyone else&nbsp;have faced&nbsp;the same issue??</span></p> 2012-04-26T15:47:34-04:004953295http://forums.asp.net/p/1797387/4953295.aspx/1?Re+Access+network+folder+using+username+and+passwordRe: Access network folder using username and password <p>Once your login session (running from your AppPool's login) has already mapped a drive to a server then you can't map another with different credentials, and in fact it makes no sense to map a second time because it remains mapped. In any event, you only need to do this mapping once (per login session). If you use different username/passwords each time then you really should look for a different approach since you might have two requests at the same time trying to contend for this single mapped network drive resource (if that makes sense).</p> 2012-04-26T15:56:30-04:004953312http://forums.asp.net/p/1797387/4953312.aspx/1?Re+Access+network+folder+using+username+and+passwordRe: Access network folder using username and password <p>hi,</p> <p>I did used the same username and password details everytime.</p> <p>Also, the code for closing connection is</p> <p>&quot;NET USE &quot; <span size="2" style="font-size:small">&#43; directory &#43; </span><span size="2" color="#a31515" style="font-size:small; color:#a31515">&quot; /delete&quot;</span></p> <p><span size="2" color="#a31515" style="font-size:small; color:#a31515"></span></p> 2012-04-26T16:06:17-04:004953331http://forums.asp.net/p/1797387/4953331.aspx/1?Re+Access+network+folder+using+username+and+passwordRe: Access network folder using username and password <p>Ok, then my guess is that maybe you have simultaneous requests trying to perform this action and one maps the drive and the second produces the error because there's already a mapped drive.</p> <p></p> 2012-04-26T16:13:53-04:004953540http://forums.asp.net/p/1797387/4953540.aspx/1?Re+Access+network+folder+using+username+and+passwordRe: Access network folder using username and password <p>Thanks,</p> <p>that may be the case;</p> <p>Is there a way to check if the drive is mapped or not using command line??</p> <p></p> 2012-04-26T18:41:54-04:004953555http://forums.asp.net/p/1797387/4953555.aspx/1?Re+Access+network+folder+using+username+and+passwordRe: Access network folder using username and password <p>You have a timing issue. Best thing would be to protect this seciton of code with a monitor, but that introduces quite a bottleneck.</p> <p>I don't think you can just do this once and leave it mapped and then keep a flag indicating that you already mapped... reason being is that your app pool might recycle and thus your flag would be lost.</p> <p>The last idea might be to just try to do this mapping in App_Start and wrap a try/catch for the situation where it's already mapped. I don't really like this as an approach, but I don't know of a way to check if it's already mapped (other than trying to access the share).</p> <p>This is a tricky thing and certainly uncommon for server processes to do this -- mapping a network drive is typically for an interactive user. Perhaps consider alternatives? I'm not quite sure what the mapped drive is used for, so it's hard to provide concrete suggestions.</p> <p></p> 2012-04-26T18:50:38-04:004953563http://forums.asp.net/p/1797387/4953563.aspx/1?Re+Access+network+folder+using+username+and+passwordRe: Access network folder using username and password <p>Hi Brock,</p> <p>Thanks for replying, there are files that are dumped on the mapped drive by some other processes and i need to copy those files on the local server and process the data and create a&nbsp; report using .net</p> 2012-04-26T18:55:50-04:004953566http://forums.asp.net/p/1797387/4953566.aspx/1?Re+Access+network+folder+using+username+and+passwordRe: Access network folder using username and password <p></p> <blockquote><span class="icon-blockquote"></span> <h4>psm_8210</h4> <p></p> <p>Hi Brock,</p> <p>Thanks for replying, there are files that are dumped on the mapped drive by some other processes and i need to copy those files on the local server and process the data and create a&nbsp; report using .net</p> <p></p> </blockquote> <p></p> <p>And this is done as part of an ASP.NET request?</p> <p></p> <p></p> 2012-04-26T18:58:40-04:004953573http://forums.asp.net/p/1797387/4953573.aspx/1?Re+Access+network+folder+using+username+and+passwordRe: Access network folder using username and password <p>Hi Brock,</p> <p>Below things i am doing;</p> <p>1. Created a console application which would be scheduled to run.</p> <p>2. This console application would copy files if any from the mapped drive using username and password to the local drive.</p> <p>3. The local files are processed and the data is added to the MS SQL server database tables.</p> <p>4. These tables data are then used by some other process to generate reports - THIS POINT IS NOT IN SCOPE OF CONSOLE APPLICATION.</p> <p>Its only until point 3 that the console application needs to implement.</p> 2012-04-26T19:04:47-04:004953576http://forums.asp.net/p/1797387/4953576.aspx/1?Re+Access+network+folder+using+username+and+passwordRe: Access network folder using username and password <p>Also,</p> <p>I found a link http://stackoverflow.com/questions/1432213/copy-file-on-a-network-shared-drive which tells another approach to map drive</p> 2012-04-26T19:06:48-04:004953655http://forums.asp.net/p/1797387/4953655.aspx/1?Re+Access+network+folder+using+username+and+passwordRe: Access network folder using username and password <p></p> <blockquote><span class="icon-blockquote"></span> <h4>psm_8210</h4> <p></p> <p>Also,</p> <p>I found a link http://stackoverflow.com/questions/1432213/copy-file-on-a-network-shared-drive which tells another approach to map drive</p> <p></p> </blockquote> <p></p> <p>Yes, I'd encourage you to take this approach.</p> <p></p> 2012-04-26T20:01:58-04:004957508http://forums.asp.net/p/1797387/4957508.aspx/1?Re+Access+network+folder+using+username+and+passwordRe: Access network folder using username and password <p>Hi psm_8210,</p> <p>For .NET applications that need to programmtically access files on remote machine(via UNC share folders), you do not need to execute the ceratin &quot;net view&quot; or related commands via Process.Start. You can directly use the File access classes under System.IO namespace to read or write files against UNC share path as long as your application is running under an account which has permission on remote machine.</p> <p>BTW,for windows authentication through NTLM, there is a trick that you can create two duplicated local accounts (with the same account name and password) on two machines, for example:</p> <p><strong>machineA\testaccount&nbsp; on machineA</strong></p> <p><strong>machineB\testaccount on machineB</strong></p> <p>Then, you can run application on machineA under machineA\testaccount and access machineB(such as share folder on it), and machineB will authenticate the access from machineA as the machineB\testaccount user.</p> <p>&nbsp;</p> 2012-04-30T06:15:31-04:00