Re: C#, Windows Authentification & Windows API

From: Manoj Shinde (m_s77_at_hotmail.com)
Date: 12/01/03


Date: 1 Dec 2003 04:51:23 -0800

Dear Kim

I dont know much about importing DLL's in C#, but I can see a mismatch
in the function declaration. In the SDK the LogonUserEx is declared as

BOOL LogonUserEx(
  LPTSTR lpszUsername,
  LPTSTR lpszDomain,
  LPTSTR lpszPassword,
  DWORD dwLogonType,
  DWORD dwLogonProvider,
  PHANDLE phToken,
  PSID* ppLogonSid,
  PVOID* ppProfileBuffer,
  LPDWORD pdwProfileLength,
  PQUOTA_LIMITS pQuotaLimits
);

And your declaration seems to miss many params.

May be this is your problem.

Regards

Manoj Shinde
m_s77@hotmail.com

"Kim Ipsen" <kim@dotnetcv.dk> wrote in message news:<OkqKzrotDHA.2132@TK2MSFTNGP10.phx.gbl>...
> I am currently writing a system of applications which need to authenticate
> on at a given server.
>
> But I want to be able to use AD for "defining" users.
> Furthermore I need it to be able to work using SOAP.
>
> I have found this code where I import the AdvAPI32.dll from windows and use
> the LogonUser() method.
> But I can't get it to work properly.
> Everytime it keeps saying that an Object Reference isn't set to an instance
> of an object.
>
> The code that I currently have (for a test application in a windows form!)
> looks like this:
>
> [DllImport("C:\\Windows\\System32\\advapi32.dll")]
>
> private static extern bool LogonUserEx(string lpszUsername, string
> lpszDomain, string lpszPassword, int dwLogonTyp, int dwLogonProvider);
>
> private int AuthenticateUser(AuthHeader secHeader)
>
> {
>
> bool logon = false;
>
> try
>
> {
>
> logon = LogonUserEx(secHeader.UserID, secHeader.Domain, secHeader.Password,
> 4, 3); //this is where the error occurs
>
> }
>
> catch(System.NullReferenceException n_ecx)
>
> {
>
> MessageBox.Show(this, "UserID: " + secHeader.UserID + ", Password: " +
> secHeader.Password + ", Domain: " + secHeader.Domain + ", authToken: " +
> authToken + ", Error message: " + n_ecx.Message);
>
> }
>
> if(logon)
>
> {
>
> return authToken;
>
> }
>
> else
>
> {
>
> MessageBox.Show(this, "User login failed!");
>
> return -1;
>
> }
>
> }
>
> I also "use" the System.Runtime.Interopservices namespace.
>
> A AuthHeader is defined as this:
>
> public class AuthHeader
>
> {
>
> public AuthHeader(){} //nothing happens in constructor
>
> public string UserID;
>
> public string Password;
>
> public string Domain;
>
> }
>
>
>
> The article which inspired me to take this road can be found here:
>
> http://www.ftponline.com/vsm/2002_09_14th/magazine/features/rauschenberger/default.aspx