Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C#
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 :
<br />
login failed unknown username or bad password <br />

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

1 solution

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.
 
Share this answer
 
v2
Comments
Neema Derakhshan 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ő 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900