Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
hi friends
I want to open a windows shared folder through my website,but shared folder & website's host are in different domains on private interanet.
I'm using this code and tried many code before
        System.Diagnostics.Process command = new System.Diagnostics.Process();
        command.StartInfo.FileName = @"explorer";
        command.StartInfo.Arguments = @"\\server\shared folder";
        command.StartInfo.UserName = "username";
        command.StartInfo.Domain = "domain_name";
        System.Security.SecureString pwd = new System.Security.SecureString();
 
        foreach (char c in "user_password")
            pwd.AppendChar(c);
 
        command.StartInfo.Password = pwd;
        command.StartInfo.CreateNoWindow = false;
        command.StartInfo.Verb = "open";
        command.StartInfo.UseShellExecute = false;
 
        try
        {
            command.Start();
        }
 
        catch (Exception e11)
        {
            Label1.Text = e11.Message;
        }
        
        command.Close();
but every time I get this error :

login failed unknown username or bad password

I dont why and how to solve this..
Posted 20-Apr-13 19:53pm
Edited 20-Apr-13 20:06pm
v2
Comments
Sergey Alexandrovich Kryukov at 21-Apr-13 0:56am
   
It's generally a bad idea to use file sharing in Web development...
—SA
Neema Derakhshan at 21-Apr-13 1:02am
   
that's true but its a private interanet

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There are situations where such task is needed. But as Sergey highlighted, it should be avoided.
Still if you need too, the method you tried is far the worst, and will never work. You will get nothing from starting an explorer process on the server. The main question is what do you want to do with the opened share. Supposing, that you start the explorer instance, you would only get a hidden explorer on the server, and you can't do much with it.
So I assume, you want to access the files in it.
 
Well, there are two scenarios:
1) The domains are in the same forest, or inside necessary trust boundaries. This way you can create a thread with the remote credentials. Your application will run under the application pool credentials, but that thread runs under the desired one so you can access remote resources. If this applies to you, start here: A Complete Impersonation Demo in C#.NET[^].
Two notes:
- you could change the security context of whole worker thread if needed, but you might encounter problems with the local resources
- asp.net can impersonate logged-in or specified user by default, if this applies to you start here: http://msdn.microsoft.com/en-us/library/aa292118(v=vs.71).aspx[^]
 
2) The remote domain is not in any trust relation with the local one (or you need many remote shares at once), you can open remote network connections with WNetAddConnection2 API function. You need p/invoke, but here you can see how to implement and use it: http://stackoverflow.com/questions/295538/how-to-provide-user-name-and-password-when-connecting-to-a-network-share[^]
 
Be aware, that these methods give you only the possibility to access files (provided you have the rights also), but you will have to deal with the files.
  Permalink  
v2
Comments
Neema Derakhshan at 21-Apr-13 3:11am
   
thank you Zoltan
well,the shared folder is something like desktop for some far offices,and employees use website to connect to different company softwares,so I need to add this share folder to the website if it is possible!
by the way thank you and sorry for my poor english :)
Zoltán Zörgő at 21-Apr-13 5:33am
   
Well, it they are in the same forest and they have access right to those folders, you simply need to add a file url or modify their login script or GPO user preferences to have those folders mapped. You can not tell the client (browser) from server side to open a share with some credentials provided by the server. You could however do this with a custom service.
But as I see, you probably need some sort of virtualization here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 410
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 105
4 PIEBALDconsult 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 21 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100