簡體   English   中英

.NET模擬問題

[英].NET Impersonation problem

我在http://www.dotnet247.com/247reference/msgs/28/144136.aspx上閱讀了有關.NET模擬的文章,這是我正在使用的基本代碼。

我在將文件復制到網絡共享時遇到問題。 完成后,此控制台應用程序將在遠程服務器上運行,並將文件復制到其他系統上的共享中。 這兩個系統可以看到對方。

我可以直接從計算機將文件復制到遠程共享,因此可以訪問該共享。

這是代碼。 我評論了引起問題的行:

Dim token1 As Integer

Dim loggedOn As Boolean = LogonUser("scotts", "MyDomain", "AwesomePw", 3, 0, token1)

Dim mWI1 As WindowsIdentity = WindowsIdentity.GetCurrent()

Dim token2 As IntPtr = New IntPtr(token1)

Dim mWI2 As WindowsIdentity = New WindowsIdentity(token2)

'This file copies a file successfully
File.Copy("c:\LogExtractorRecipients.txt", "\\RemoteServer\SHARED\QALogFiles\LogExtractorRecipients2.txt")

Dim mWIC As WindowsImpersonationContext = mWI2.Impersonate()

'This causes the error: "Access to the path '\\RemoteServer\SHARED\QALogFiles\QALogFiles\LogExtractorRecipients.txt' is denied."
File.Copy("c:\LogExtractorRecipients.txt", "\\RemoteServer\SHARED\QALogFiles\QALogFiles\LogExtractorRecipients.txt")

我正在冒充自己(以證明自己的真實),並具有對該共享的讀寫權限。 我正在本地運行此代碼。 在使用模擬之前,可以復制文件。 模擬之后,我不能。 模擬似乎很成功-使用名稱,令牌和其他屬性填充了mWI2變量。 ImpersonationLevel為3,而之前在wMI1上,ImpersonationLevel為0。這是我所能看到的唯一區別。

有什么建議么?

這是一個長鏡頭,但是在嘗試模擬用戶之前,請考慮調用DuplicateToken(...),因為它應該返回具有用戶安全上下文的主令牌。

我在http://msmvps.com/blogs/siva/archive/2007/03/01/impersonating-network-share-unc-path-access.aspx中找到了答案。 (我應該知道在發布問題后會找到它。)

我將第四個參數從3更改為8,並且能夠復制文件。 現在,我將確切找出原因。 但是我的頭痛已經好多了!

謝謝,斯科特

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM