Re: Windows Auth to SQL Server from ATL Web Service not working...
On Jan 18, 5:09 pm, mike <mikebi...@hotmail.com> wrote:
On Jan 18, 8:54 am, "Brian Muth" <bm...@mvps.org> wrote:
I think those are login attempts to the web server, not the database ser=
ver, am I correct? What does the audit event log look like
on the database server?
The db server only has one entry...
Login failed for user "NT AUTHORITY\ANONYMOUS LOGON'
Okay. I finally got this to work without using domain accounts. I
have to use the following code within my web service method.
CComBSTR bstrErr( _T( "" ) );
CAccessToken tok;
CAccessToken tokImpr;
HANDLE token;
hr = CoInitialize( NULL );
if( FAILED( hr ) )
{
bstrErr.Append( _T( "CoInitialize failed" ) );
goto AuthenticateEncoder_End;
}
if ( !m_spServerContext->GetImpersonationToken( &token ) )
{
*bOutput = false;
hr = E_FAIL;
bstrErr.Append( _T( "GetImpersonationToken failed" ) );
goto AuthenticateEncoder_End;
}
else
{
tok.Attach( token );
if( !tok.GetProcessToken( TOKEN_READ | TOKEN_DUPLICATE,
tokImpr.GetHandle() ) )
{
*bOutput = false;
hr = E_FAIL;
bstrErr.Append( _T( "GetProcessToken failed" ) );
goto AuthenticateEncoder_End;
}
if( SetThreadToken( NULL, tokImpr.GetHandle() ) )
{
/* this is where you do the work you want to under the account of the
application pool */
SetThreadToken( NULL, NULL );
}
else
{
bstrErr.Append( _T( "SetThreadToken failed" ) );
goto AuthenticateEncoder_End;
}
}
AuthenticateEncoder_End:
CoUninitialize();
if( bstrErr.Length() != 0 )
{
*bOutput = false;
hr = E_FAIL;
return SoapFault( SOAP_E_SERVER, bstrErr, bstrErr.Length() );
}
else
{
return hr;
}