iT邦幫忙

0

(已解決)ASP.Net 要如何使用網域帳號進行跨server檔案複製

進行檔案複製時,出現錯誤〞登入失敗: 未授與使用者這個電腦所要求的登入類型。〞
https://ithelp.ithome.com.tw/upload/images/20190710/20033493RXTfsrbkP6.jpg

程式內容:
ServerA上執行一個Aspx網頁,使用FileUpload元件上傳一個Excel到ServerA\ExcelUpload目錄。
想要將上傳後的檔案,複製到另一台ServerB的C:\根目錄下。
(ServerA & ServerB 均已加入網域)

在web.config已加上
https://ithelp.ithome.com.tw/upload/images/20190710/20033493s20RenWYNf.jpg

Default.aspx

<div>
    
<asp:FileUpload ID="FileUpload1" runat="server" Width="481px" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUpload1"
ErrorMessage="必須是xlsx檔案" ValidationExpression="^.*?\.(xlsx)$"></asp:RegularExpressionValidator><br />    

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上傳" />
<br />
<asp:Label ID="Result" runat="server"></asp:Label>
<br />
<asp:Label ID="ResultMD5" runat="server"></asp:Label>

<br />
<asp:Label ID="Resultfilename" runat="server"></asp:Label>

<br />
<asp:Label ID="ResultUrl" runat="server"></asp:Label>
    
    </div>

Default.aspx.cs

if (FileUpload1.HasFile)
{
    string savePath = @"C:\ExcelUpload\";
    String fileName = FileUpload1.FileName;
    savePath += fileName;

    try
    {                    
        FileUpload1.SaveAs(savePath); //這行有執行,檔案有存入
        CopyFile(fileName); //執行這行時,發生錯誤
        Result.Text = "OK";
    }
    catch (Exception ex)
    {
        Result.Text = ex.Message;// "Error";
        ResultMD5.Text = "";
        Resultfilename.Text = "";
        ResultUrl.Text = "";
    }
}           
            
public void CopyFile(string fileName) 
{
    string sourceFile = System.IO.Path.Combine(SourcePath, fileName);
    string destFile = System.IO.Path.Combine(TargetPath, fileName);
    File.Copy(sourceFile, destFile, true);
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
丹尼爾
iT邦研究生 2 級 ‧ 2019-07-12 01:02:11
最佳解答
allenlwh iT邦高手 1 級 ‧ 2019-07-12 10:56:26 檢舉

通常登入類別選 9、登入提供者選 0

0
player
iT邦大師 1 級 ‧ 2019-07-10 23:22:44
看更多先前的回應...收起先前的回應...
allenlwh iT邦高手 1 級 ‧ 2019-07-11 10:30:57 檢舉

"拒絕存取路徑 '\server31\Excel\Quote.csv'"
已直接將路徑和檔案寫進code裡面,還是不行


NetworkShare.ConnectToShare(@"\\server31\Excel", "user\a0001", "pwd");
            
File.Copy(@"C:\Quote.csv", @"\\server31\Excel\Quote.csv");

NetworkShare.DisconnectFromShare(@"\\server31\Excel", false);
player iT邦大師 1 級 ‧ 2019-07-11 10:55:41 檢舉

你有試過先不寫code, 先用檔案總管手動操作一遍嗎?
如果手動也不行的話
那問題就出在目的的那台上
看是不是權限設定上忘了設了?

allenlwh iT邦高手 1 級 ‧ 2019-07-11 11:07:05 檢舉

手動是OK的,我有Admin的權限

allenlwh iT邦高手 1 級 ‧ 2019-07-12 10:55:25 檢舉

感謝您提供的解法,讓我有了方向。

最後的解法是:

IntPtr token = default(IntPtr);
if (LogonUser("username", "domain", "pwd", 9, 0, ref token) != 0)
{
    //......要處理的事件
    WindowsImpersonationContext impersonationContext = WindowsIdentity.Impersonate(token);

    try
    {
        string sourceFile = System.IO.Path.Combine(SourcePath, fileName);
        string destFile = System.IO.Path.Combine(TargetPath, fileName);
        File.Copy(sourceFile, destFile, true);
    }
    finally
    {
        impersonationContext.Undo();
    }
}

我要發表回答

立即登入回答