Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 7-Feb-13 17:24pm
Comments
chester_it21 at 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..........
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

set the unc credentials with this
 

UNCAccess unc = new UNCAccess(@"\\host\share", "username", "domain", "password");
 
and here is the class
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;
                }
            }
 
        }
    
 
}
  Permalink  
v2
Comments
ashish.bathla at 12-Feb-13 0:32am
   
Thanks Jose,
it worked but after impersonation, the error occurs "unknown username or bad password".... any idea??
   
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 at 14-Feb-13 1:08am
   
yes, the "net use" command gives - "command completed successfully"
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can use Impersonation:
 
Windows Impersonation using C#[^]
 
Cheers,
Edo
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 205
1 ProgramFOX 120
2 Richard MacCutchan 100
3 ChauhanAjay 100
4 kbrandwijk 90
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 11 Feb 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