后台智能服务BITS - 模拟帐户

时间:2014-12-30 10:39:28

标签: c# windows bits

平台是Windows 7及以上版本

早上所有BITS现在都让我感到疯狂。

我甚至不知道这是否可行。

所以我想在作为服务运行的系统帐户下启动BITS作业。 我想传递NT域名信用域\用户名等凭据。 我希望这项工作从网络共享转移(下载),但不能在该用户登录时进行。

因此,只需重新使用BITS从网络共享下载服务,但需要域名信用来访问共享。

我尝试了很多不同的东西来冒充NT域帐号(用户没有登录,因此Job不会启动)。

将凭据传递给BITS作业,但几乎没有成功。

尝试在Credential Manager中添加网络共享,但失败并显示错误1312

如果有人有任何建议,我将不胜感激。

由于

这里有一些代码 - 我使用SharpBits作为BITS包装器。 http://sharpbits.codeplex.com/

我尽量减少课程,使其尽可能简单。发布代码的道歉并不像我预期的那样简单。

using SharpBits.Base;
public class BitsFetchFiles : IDisposable
{
    BitsCredentials _BitsCredentials;
    BitsManager mManager = new BitsManager();
    List<string> _JobIds = new List<string>();
    DirectoryInfo _Source; //Set by constructor
    DirectoryInfo _Cache; //Set by constructor

    //Impersonation class to wrap credentials in
    InfoDllImpersonation _Impersonation = null;

    public void GetJob(string Domain, string Username, string Password)
    {
        _BitsCredentials = new BitsCredentials();
        _BitsCredentials.UserName = Domain + "\\" + Username;
        _BitsCredentials.Password = Password;
        _BitsCredentials.AuthenticationTarget = AuthenticationTarget.Server;
        _BitsCredentials.AuthenticationScheme = AuthenticationScheme.Negotiate;

       _Impersonation = new InfoDllImpersonation(Username, Password, Domain);

       //Just a method to the impersonated user access to the destination directory
       GiveAllUserAccessToTheFolder(DirectoryDestination);


       BitsJob Bjob = mManager.CreateJob("Service Download", JobType.Download);
       Bjob.OnJobError += Bjob_OnJobError;
       Bjob.OnJobTransferred += Bjob_OnJobTransferred;
       Bjob.Priority = JobPriorityForDownload;
       Bjob.OnJobModified += Bjob_OnJobModified;
       _JobIds.Add(Bjob.JobId.ToString());
       JobInformation.JobId = Bjob.JobId.ToString();
       Bjob.TakeOwnership();

       using (_Impersonation.ImpersonatedUser)
       {
         using (new NetworkConnection(_Source.FullName, new System.Net.NetworkCredential(
         _Impersonation.UserName, _Impersonation.Password, _Impersonation.Domain)))
         {
             Bjob.AddCredentials(_BitsCredentials);
             StartIncludingSubs(remoteFiles, Bjob, ref totalFileBytes);
         }
       }

      //Enumerate all jobs listed and resume - e.g Bjob.Resume();
      StartAllJobs();
    }

   void StartIncludingSubs(FileInfo[] remoteFiles, BitsJob Bjob, ref long totalFileBytes)
     {
         _LogFile.Log(string.Format("Using network credentials user : {0}",
                       _Impersonation.UserName), LogFile.LogLevel.Debug);


         remoteFiles = _Source.GetFiles("*.*", SearchOption.AllDirectories);
         JobInformation.TotalNumberOfFiles = remoteFiles.Length;

         //Collected all remote files...
         JobInformation.StatusInformation =
             "Source Files have been collected and read with a total of "
             + remoteFiles.Length.ToString() + " files";        


         foreach (var fi in remoteFiles)
         {
             string newCacheFile = _Cache + fi.FullName.
                 Substring(_Source.FullName.Length);


             string direct = newCacheFile.Substring(0, newCacheFile.LastIndexOf("\\"));
             Directory.CreateDirectory(direct);
             Bjob.AddFile(fi.FullName, newCacheFile);

             if (_CancelTheJobAddingFiles)
             {                    
                 return;
             }
             //Console.WriteLine("remote file {0} and Local is {1}", _Source.FullName + "\\" + fi.Name, newCacheFile);
             totalFileBytes += fi.Length;
         }
     }

}

0 个答案:

没有答案