2011-11-17 7 views
10

मैं एक फ़ाइल फ़ाइल साझा सर्वर (फ़ोल्डर साझा किया जा रहा है) में फ़ाइल को तैनात करने के लिए एक .NET C# कंसोल प्रोग्राम बना रहा हूं। मार्ग है :: \\192.168.0.76\htdocs\publicफ़ाइल शेयर के माध्यम से नेटवर्क पर फ़ाइलों की प्रतिलिपि बनाएँ, उपयोगकर्ता प्रमाणीकरण

चलाने पर मैं त्रुटि हो रही है:

[09:35:29]: [Step 1/3] Unhandled Exception: System.UnauthorizedAccessException: Access to the path '\\192.168.0.76\htdocs\public' is denied. 
[09:35:29]: [Step 1/3] at DeployFileShare.Program.CopyDir(String source, String dest, String[] exclude, Boolean overwrite) 
[09:35:29]: [Step 1/3] at DeployFileShare.Program.Deploy(String num, String source) 
[09:35:29]: [Step 1/3] at DeployFileShare.Program.Main(String[] args) 
[09:35:29]: [Step 1/3] Process exited with code -532459699 

मुझे लगता है कि मैं अपने आप को प्रमाणित करने के लिए की जरूरत है। मैं इस का सामना करना पड़ा:

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); 
WindowsIdentity idnt = new WindowsIdentity(username, password); 
WindowsImpersonationContext context = idnt.Impersonate(); 

मैं भी कोशिश की है:

AppDomain.CreateDomain("192.168.0.76").SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); 
WindowsIdentity idnt = new WindowsIdentity("user", "pass"); 
WindowsImpersonationContext context = idnt.Impersonate(); 

मैं इसे कैसे उपयोग करने के लिए यकीन नहीं है। जब मैं अनुप्रयोग चलाने मैं:

C:\Users\Administrator>DeployFileShare 1 R:\BuildOutput\_PublishedWebsites\Web 2 
1 
Deploy Started Web, version 21 
-- Deploy Prepared 
-- Deploying to 1 

Unhandled Exception: System.Security.SecurityException: There are currently no l 
ogon servers available to service the logon request. 

    at System.Security.Principal.WindowsIdentity.KerbS4ULogon(String upn) 
    at System.Security.Principal.WindowsIdentity..ctor(String sUserPrincipalName, 
String type) 
    at DeployFileShare.Program.Authenticate(String server) 
    at DeployFileShare.Program.Deploy(String num, String source) 
    at DeployFileShare.Program.Main(String[] args) 
The Zone of the assembly that failed was: 
MyComputer 

यहाँ बुनियादी कोड है:

static void Main() 
{ 
Copy(); 
} 
static void Copy() 
{ 
AppDomain.CreateDomain(GetServerInfo(server, "server")).SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); 
      WindowsIdentity idnt = new WindowsIdentity(GetServerInfo(server, "user"), GetServerInfo(server, "pass")); 
      WindowsImpersonationContext context = idnt.Impersonate(); 
string source = "C:\\someDir"; 
string dest = "\\192.168.0.76\shareFolder" 
string[] sourceFiles = Directory.GetFiles(source, "*", SearchOption.AllDirectories); 
      foreach (string file in sourceFiles) 
      { 
       string local = file.Replace(source, ""); 
       if (exclude.Contains(local)) 
        continue; 
       if (!Directory.Exists(Path.GetDirectoryName(dest + "\\" + local))) 
        Directory.CreateDirectory(Path.GetDirectoryName(dest + "\\" + local)); 
       File.Copy(file, dest + "\\" + local, overwrite); 
       Console.WriteLine("-- -- [copied] {0} -> {1}", file, dest + "\\" + local); 
      } 
} 

पाश कार्यों के लिए, मैं इसे अपने स्थानीय सिस्टम पर परीक्षण किया है में कोड की नकल प्रणाली।

यदि कोई जानता है कि इसे काम करने के लिए मुझे WindowsIdentity और WindowsIdentity का उपयोग कैसे करना चाहिए, तो कृपया मुझे प्रबुद्ध करें। मैं चारों ओर देख रहा हूं और खिड़की के दस्तावेज बहुत मदद नहीं करता है।

असल में मैं सिस्टम में लॉग इन करके साझा की जा रही दूरस्थ निर्देशिका में कैसे प्रतिलिपि बना सकता हूं?

+1

आप इसके लिए पावरहेल का उपयोग करने पर विचार करना चाहेंगे। इसके बाद आप केवल नेट उपयोग * \\ 192.168.9.76 \/USER: कॉपी करने से पहले कुछ उपयोगकर्ता (जिसके लिए मैं Robocopy.exe का उपयोग करूंगा ...) –

+0

@ जेम्स जेम्ससन मैं आईपी का उपयोग कर रहा हूं क्योंकि हम वीएमवेयर के साथ काम कर रहे हैं और वीएमवेयर के तहत एक अलग डोमेन के तहत इस प्रोग्राम को चलाने वाली मशीन है। तो कंप्यूटर का नाम काम नहीं करता है। –

+0

मेरा मानना ​​है कि एक और सार्वभौमिक तरीका है: http://stackoverflow.com/questions/295538/how-to-provide-user-name-and-password-when-connecting-to-a-network-share/39540451# 39540451 –

उत्तर

5

वीबी लेकिन आसानी से सी # में अनुवाद किया गया। अपनी कॉपी से पहले यह करें: आप एक फ़ाइल स्थानांतरित करने के लिए में किसी दूरस्थ कंप्यूटर से प्रमाणित करने के लिए चाहते हैं

Private Sub Open_Remote_Connection(ByVal strComputer As String, ByVal strUserName As String, ByVal strPassword As String) 
    Dim ProcessStartInfo As New System.Diagnostics.ProcessStartInfo 
    ProcessStartInfo.FileName = "net" 
    ProcessStartInfo.Arguments = "use \\" & strComputer & "\c$ /USER:" & strUsername & " " & strPassword 
    ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden 
    System.Diagnostics.Process.Start(ProcessStartInfo) 
    System.Threading.Thread.Sleep(2000) 
End Sub 
5

, आप अपने प्रयोक्ता का रूप धारण करने के लिए और WindowsIdentity को LogonUser फ़ंक्शन का उपयोग कर सकते हैं।

/// <summary> 
/// Exécute une fonction en empruntant les credentials 
/// </summary> 
private T ApplyCredentials<T>(Func<T> func) 
{ 
    IntPtr token; 

    if (!LogonUser(
     _credentials.UserName, 
     _credentials.Domain, 
     _credentials.Password, 
     LOGON32_LOGON_INTERACTIVE, 
     LOGON32_PROVIDER_DEFAULT, 
     out token)) 
    { 
     Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); 
    } 

    try 
    { 
     // On doit être impersonifié seulement le temps d'ouvrir le handle. 
     using (var identity = new WindowsIdentity(token)) 
     using (var context = identity.Impersonate()) 
     { 
      return func(); 
     } 
    } 
    finally 
    { 
     CloseHandle(token); 
    } 
} 

// ... 

if (_credentials != null) 
{ 
    return this.ApplyCredentials(() => File.Open(path, mode, access, share)); 
} 

return File.Open(path, mode, access, share); 
संबंधित मुद्दे