views:

37

answers:

1

I use the following code to validate users through windows authentication for my winform application. This works fine with windows XP but when the user is using windows 2000, it says that the userid or password is in valid.

How do I do this in Windows 2000. Further, How do I detect whether the user is using Windows Xp or windows 2000.

        [System.Runtime.InteropServices.DllImport("advapi32.dll")]
    public static extern bool LogonUser(string userName, string domainName, string password, int LogonType, int LogonProvider, ref IntPtr phToken);

    public bool IsValidateCredentials(string userName, string password, string domain)
    {
        IntPtr tokenHandler = IntPtr.Zero;
        bool isValid = LogonUser(userName, domain, password, 2, 0, ref tokenHandler);
        return isValid;
    }

Thanks

A: 

As stated in the documentation for LogonUser:

Windows 2000: If you need to validate credentials, use the SSPI APIs. For information about using the SSPI APIs, see How To Validate User Credentials on Microsoft Operating Systems. Use the LogonUser or LogonUserEx function if you need to impersonate the user by using the returned access token and access a resource.

To check what version of Windows you're running on, check Environment.OSVersion.Version.

Windows 2000 is 5.0; XP is 5.2.

Also, you need to close the handle by calling CloseHandle.

SLaks
Thanks for the links... The sample code in your second link, is just a bouncer for me... I think it is VC++, on which I'm 0... Do I need to try converting the whole stuff or is there an easy way to do it in C#...
The King
Yes; that's C++. I don't have time now to convert it to C#.
SLaks
Hope I have not been misunderstood... I never asked you to convert it to C#...
The King
There isn't an easy way to do it in C#; you'll need to convert that code sample.
SLaks