0
이 테마는 새로운 것은 아닙니다. 하지만 전문가의 도움이 필요해. 로컬 시스템 (도메인 컴퓨터가 아님)에서 실행되는 Windows 양식 응용 프로그램을 만들고 있습니다. 응용 프로그램은 Active Directory 도메인의 공유 폴더에 폴더 및 일부 파일을 만듭니다. 나는 가장에 대한 읽었과 같은 어떤 것을 만들기 위해 노력했다 :C# ActiveDirectory 및 가장의 원격 서버에있는 파일 시스템
Impersonation by using LogonUser 그럼 내가 다음 코드를 작성 :
using System.Security.Principal;
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
private enum LogonSessionType : uint
{
Interactive = 2,
Network,
Batch,
Service,
NetworkCleartext = 8,
NewCredentials
}
private enum LogonProvider : uint
{
Default = 0,
WinNT35,
WinNT40,
WinNT50
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool LogonUser(
string principal,
string authority,
string password,
LogonSessionType logonType,
LogonProvider logonProvider,
out IntPtr token);
public Form1()
{
InitializeComponent();
}
protected void btnConnect_Click(object sender, EventArgs e)
{
IntPtr token = IntPtr.Zero;
WindowsImpersonationContext impersonateUser = null;
try
{
bool result = LogonUser("[email protected]", "192.168.1.1", "[email protected]",
LogonSessionType.Network, LogonProvider.Default, out token);
if(result)
{
WindowsIdentity id = new WindowsIdentity(token);
impersonateUser = id.Impersonate();
string showtext = string.Format("Identity: {0}", WindowsIdentity.GetCurrent().Name);
MessageBox.Show(showtext);
}
else
{
string showtext = string.Format("Identity: {0}", "Fail");
MessageBox.Show(showtext);
}
}
catch
{
}
finally
{
if(impersonateUser!=null)
impersonateUser.Undo();
if (token != IntPtr.Zero)
CloseHandle(token);
}
}
}
그러나 부울 결과을 = 항상 거짓입니다. 내가 도대체 뭘 잘못하고있는 겁니까?
것은 실제로 값 "[email protected]"을 통과하거나 당신이 당신의 진짜 이름과 암호를 넣지하는 데 사용 예에 불과하고 있습니까 : 여기에 사용 권리인가? – gabsferreira
LogonUser (...) 다음에 GetLastError()를 호출하여 Windows에서 문제가 무엇인지 확인합니다. – adelphus