locked
Copy file using service account RRS feed

  • Question

  • User919378809 posted

    Hi All,

    I have a requirement to copy a file to a share location using service account because only service account has access to that folder.  Is there is any way to copy the file using service account?  

    The network team only provide the service account and not password.  Is it possible to impersonate just using service account for copying a file?

    Thanks

    Selvakumar R

    Thursday, November 5, 2015 11:59 AM

Answers

  • User509211905 posted

    Hi there,

    If your network team doesn't give you the service account password, then even you do impersonation through code I think you still need the password.

    If your running in ASP.NET, you might want to consider to use a service account in your Application Pool in IIS. Once you have the service account set up by your network team, they can allow that service account to give write access to the share location.

    Hope that helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 5, 2015 5:09 PM
  • User919378809 posted

    Finally I implemented file uploading or downloading using web service and service account.  This code may help some one.

    1. Data Contract

    [DataContract]
        public class AttachmentDataContract
        {
            [DataMember]
            public int Id { get; set; } //Identity key
    
            [DataMember]
            public int? AccountId { get; set; } //Reference Id
    
            [DataMember]
            public string FileName { get; set; }
    
            [DataMember]
            public string FileExtension { get; set; }
    
            [DataMember]
            public string FileDescription { get; set; }
    
            [DataMember]
            public byte[] FileStream { get; set; } // this property is used to transfer file from client to service or viceversa
    
            [DataMember]
            public string FilePath { get; set; }
    
            [DataMember]
            public DateTime? CreatedDate { get; set; }
    
            [DataMember]
            public string CreatedBy { get; set; }
        }

    2. For uploading,

    Add following code in web service

    //Create new target folder, if necessary. And Copy file to target folder.
    if (!System.IO.Directory.Exists(targetPath))
    {
         System.IO.Directory.CreateDirectory(targetPath);
    }
                  
    var FilePosition = 0;
    System.IO.File.Create(destFile).Close();
    using (System.IO.FileStream fileStream = new System.IO.FileStream(destFile, System.IO.FileMode.Open,
                                         System.IO.FileAccess.ReadWrite, System.IO.FileShare.Read))
    {
         fileStream.Seek(FilePosition, System.IO.SeekOrigin.Begin);
         fileStream.Write(request.Contract.FileStream, 0, request.Contract.FileStream.Length);
    }

     Add following code to client like web or windows application

    var fileInfo = new FileInfo(sourcePath);
    if (fileInfo.Length > (10*1024*1024)) throw new FileLoadException("File size exceeds 10 MB");
    
    var attachment = new AttachmentDataContract
    {
        FileName = "Source File Name",
        FileDescription = "File Description,
        CreatedDate = DateTime.Now,
        CreatedBy = "User Name",
        FileExtension = "Source File Extension",
        FileStream = System.IO.File.ReadAllBytes(sourcePath) //Convert file into bytes
    };
    
    //Call service for transferring data
    proxy.UploadAttachment(attachment)

    3. For downloading or opening attachment

    Add following code to web service

    var attachment = new AttachmentDataContract()
    
    attachment.FileExtension = System.IO.Path.GetExtension(attachmentFilePath);
    
    if (System.IO.File.Exists(attachmentFilePath))
         attachment.FileStream = System.IO.File.ReadAllBytes(attachmentFilePath);
    
    return attachment;

    Add following code to the client like web or windows application

    var response = proxy.OpenAttachment(request); //request contain attachment id, path etc
    
    var fileStream = response.Contract.FileStream;
    var fileExtension = response.Contract.FileExtension;
    var fileName = System.IO.Path.GetTempFileName() + fileExtension;
    
    if (fileStream == null)
    {
        ShowErrorMessage("File not found! Please contact administrator.", "Attachments", DialogButton.OK, DialogImage.Information);
        return;
    }
    
    File.WriteAllBytes(fileName, fileStream);
    var process = System.Diagnostics.Process.Start(fileName);
    process.Exited += (s, e) => System.IO.File.Delete(fileName);

    4. MOST IMPORTANT STEP IS, If the file is uploading through a hosted web service then the service account or account configured in the service need to added in the IIS_IUSR group.

    I hope this will help someone :)

    Thanks

    Selvakumar R

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 19, 2015 4:24 PM

All replies

  • User509211905 posted

    Hi there,

    If your network team doesn't give you the service account password, then even you do impersonation through code I think you still need the password.

    If your running in ASP.NET, you might want to consider to use a service account in your Application Pool in IIS. Once you have the service account set up by your network team, they can allow that service account to give write access to the share location.

    Hope that helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 5, 2015 5:09 PM
  • User919378809 posted

    Finally I implemented file uploading or downloading using web service and service account.  This code may help some one.

    1. Data Contract

    [DataContract]
        public class AttachmentDataContract
        {
            [DataMember]
            public int Id { get; set; } //Identity key
    
            [DataMember]
            public int? AccountId { get; set; } //Reference Id
    
            [DataMember]
            public string FileName { get; set; }
    
            [DataMember]
            public string FileExtension { get; set; }
    
            [DataMember]
            public string FileDescription { get; set; }
    
            [DataMember]
            public byte[] FileStream { get; set; } // this property is used to transfer file from client to service or viceversa
    
            [DataMember]
            public string FilePath { get; set; }
    
            [DataMember]
            public DateTime? CreatedDate { get; set; }
    
            [DataMember]
            public string CreatedBy { get; set; }
        }

    2. For uploading,

    Add following code in web service

    //Create new target folder, if necessary. And Copy file to target folder.
    if (!System.IO.Directory.Exists(targetPath))
    {
         System.IO.Directory.CreateDirectory(targetPath);
    }
                  
    var FilePosition = 0;
    System.IO.File.Create(destFile).Close();
    using (System.IO.FileStream fileStream = new System.IO.FileStream(destFile, System.IO.FileMode.Open,
                                         System.IO.FileAccess.ReadWrite, System.IO.FileShare.Read))
    {
         fileStream.Seek(FilePosition, System.IO.SeekOrigin.Begin);
         fileStream.Write(request.Contract.FileStream, 0, request.Contract.FileStream.Length);
    }

     Add following code to client like web or windows application

    var fileInfo = new FileInfo(sourcePath);
    if (fileInfo.Length > (10*1024*1024)) throw new FileLoadException("File size exceeds 10 MB");
    
    var attachment = new AttachmentDataContract
    {
        FileName = "Source File Name",
        FileDescription = "File Description,
        CreatedDate = DateTime.Now,
        CreatedBy = "User Name",
        FileExtension = "Source File Extension",
        FileStream = System.IO.File.ReadAllBytes(sourcePath) //Convert file into bytes
    };
    
    //Call service for transferring data
    proxy.UploadAttachment(attachment)

    3. For downloading or opening attachment

    Add following code to web service

    var attachment = new AttachmentDataContract()
    
    attachment.FileExtension = System.IO.Path.GetExtension(attachmentFilePath);
    
    if (System.IO.File.Exists(attachmentFilePath))
         attachment.FileStream = System.IO.File.ReadAllBytes(attachmentFilePath);
    
    return attachment;

    Add following code to the client like web or windows application

    var response = proxy.OpenAttachment(request); //request contain attachment id, path etc
    
    var fileStream = response.Contract.FileStream;
    var fileExtension = response.Contract.FileExtension;
    var fileName = System.IO.Path.GetTempFileName() + fileExtension;
    
    if (fileStream == null)
    {
        ShowErrorMessage("File not found! Please contact administrator.", "Attachments", DialogButton.OK, DialogImage.Information);
        return;
    }
    
    File.WriteAllBytes(fileName, fileStream);
    var process = System.Diagnostics.Process.Start(fileName);
    process.Exited += (s, e) => System.IO.File.Delete(fileName);

    4. MOST IMPORTANT STEP IS, If the file is uploading through a hosted web service then the service account or account configured in the service need to added in the IIS_IUSR group.

    I hope this will help someone :)

    Thanks

    Selvakumar R

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 19, 2015 4:24 PM