How to present credentials in order to open file?

后端 未结 3 990
广开言路
广开言路 2020-12-06 12:55

How do I specify the username and password in order for my program to open a file for reading? The program that needs to access the file is running from an account that does

3条回答
  •  醉话见心
    2020-12-06 13:25

    I have used the Nuget package NuGet Gallery | Simple Impersonation Library 1.1.0 but there are others; search on Impersonation for the others.

    Example usage using the interactive login to work with file structures:

    using (Impersonation.LogonUser("{domain}",
                                   "{UserName}", 
                                   "{Password}", 
                                   LogonType.Interactive))
    {
         var directory = @"\\MyCorpServer.net\alpha\cars";
    
         Assert.IsTrue(Directory.Exists(directory));
    }
    

    James' answer below was before Nuget and before he would later have the most downloaded package on Nuget. Ironic eh?

提交回复
热议问题