Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i need to access a file from a network computer(file server) in asp.net C#, file server is on a domain but web server isn't....also i can't have identical local user on these servers...tried a lot of solutions but didn't worked....

can anyone help...

thanks in advance.
Posted
Comments
chester_it21 8-Feb-13 1:36am    
if you've tried to share its folder, by adding user permision to Modify Network and NETWORKSERVICE in the folder that will be shared..........

set the unc credentials with this


C#
UNCAccess unc = new UNCAccess(@"\\host\share", "username", "domain", "password");


and here is the class
C#
using System;
using System.Runtime.InteropServices;
using BOOL = System.Boolean;
using DWORD = System.UInt32;
using LPWSTR = System.String;
using NET_API_STATUS = System.UInt32;

namespace CommonFunctions
{
        public class UNCAccess
        {
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            internal struct USE_INFO_2
            {
                internal LPWSTR ui2_local;
                internal LPWSTR ui2_remote;
                internal LPWSTR ui2_password;
                internal DWORD ui2_status;
                internal DWORD ui2_asg_type;
                internal DWORD ui2_refcount;
                internal DWORD ui2_usecount;
                internal LPWSTR ui2_username;
                internal LPWSTR ui2_domainname;
            }

            [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            internal static extern NET_API_STATUS NetUseAdd(
                LPWSTR UncServerName,
                DWORD Level,
                ref USE_INFO_2 Buf,
                out DWORD ParmError);

            [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            internal static extern NET_API_STATUS NetUseDel(
                LPWSTR UncServerName,
                LPWSTR UseName,
                DWORD ForceCond);

            private string sUNCPath;
            private string sUser;
            private string sPassword;
            private string sDomain;
            private int iLastError;
            public UNCAccess()
            {
            }
            public UNCAccess(string UNCPath, string User, string Domain, string Password)
            {
                login(UNCPath, User, Domain, Password);
            }
            public int LastError
            {
                get { return iLastError; }
            }

            /// <summary>
            /// Connects to a UNC share folder with credentials
            /// </summary>
            /// <param name="UNCPath">UNC share path</param>
            /// <param name="User">Username</param>
            /// <param name="Domain">Domain</param>
            /// <param name="Password">Password</param>
            /// <returns>True if login was successful</returns>
            public bool login(string UNCPath, string User, string Domain, string Password)
            {
                sUNCPath = UNCPath;
                sUser = User;
                sPassword = Password;
                sDomain = Domain;
                return NetUseWithCredentials();
            }
            private bool NetUseWithCredentials()
            {
                uint returncode;
                try
                {
                    USE_INFO_2 useinfo = new USE_INFO_2();

                    useinfo.ui2_remote = sUNCPath;
                    useinfo.ui2_username = sUser;
                    useinfo.ui2_domainname = sDomain;
                    useinfo.ui2_password = sPassword;
                    useinfo.ui2_asg_type = 0;
                    useinfo.ui2_usecount = 1;
                    uint paramErrorIndex;
                    returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                    iLastError = (int)returncode;
                    return returncode == 0;
                }
                catch
                {
                    iLastError = Marshal.GetLastWin32Error();
                    return false;
                }
            }

            /// <summary>
            /// Closes the UNC share
            /// </summary>
            /// <returns>True if closing was successful</returns>
            public bool NetUseDelete()
            {
                uint returncode;
                try
                {
                    returncode = NetUseDel(null, sUNCPath, 2);
                    iLastError = (int)returncode;
                    return (returncode == 0);
                }
                catch
                {
                    iLastError = Marshal.GetLastWin32Error();
                    return false;
                }
            }

        }
    

}
 
Share this answer
 
v2
Comments
ashish.bathla 12-Feb-13 0:32am    
Thanks Jose,
it worked but after impersonation, the error occurs "unknown username or bad password".... any idea??
José Amílcar Casimiro 12-Feb-13 5:33am    
The code above is the equivalent to "net use \\myserver /user:username password", can you access your network folder trough the command line?
ashish.bathla 14-Feb-13 1:08am    
yes, the "net use" command gives - "command completed successfully"
You can use Impersonation:

Windows Impersonation using C#[^]

Cheers,
Edo
 
Share this answer
 

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