다른 사람이 내가 가지고있는 것과 동일한 문제에 부딪히는 경우를 대비해 여기에 답이 나와 있습니다. 대부분의 코드는 MSDN 사이트에서 가져온 것입니다 ... 작동합니다. 하지만 기본적으로 원하는 것은 사용자를 가장하는 것입니다.
을 필요한 곳에
private bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}
이
if (impersonateValidUser("username", "domain", "password"))
{
// code that needs to access Db
// make sure you undo when no longer needed
impersonationContext.Undo();
}
당신의 SQL 사용자 이름과 연결 문자열에 지정된 암호 또는 있습니까 구현 :
, 귀하의 using이에
쓰기 다음과 같은 방법으로 이러한 추가 Windows 인증에 의존하고 있습니까? –
Windows 인증 전용 – edepperson