понедельник, 8 августа 2011 г.

How to validate user credentials in windows?

Validating with user name and password:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace ConsoleApplication1
{
class Program
{
[DllImport("ADVAPI32.DLL")]
public static extern bool LogonUser(String lpszUsername,
String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out int phToken);

const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_LOGON_NETWORK = 3;
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_PROVIDER_WINNT50 = 3;
const int LOGON32_PROVIDER_WINNT40 = 2;
const int LOGON32_PROVIDER_WINNT35 = 1;

private static bool isValidUser(string userName, string domainName, string passsWord) {
bool Validateuser; int token1;
Validateuser = LogonUser(userName, domainName,
passsWord, LOGON32_LOGON_NETWORK, 0, out token1);

if (Validateuser) {
return true;
} else {
return false;
}
}

static void Main(string[] args) {
if (args.Length != 2) {
Environment.Exit(1);
return;
}

String fullName = WindowsIdentity.GetCurrent().Name;
String un = fullName.Split('\\')[1]; //getting user name
String dom = fullName.Split('\\')[0];//getting domain name
String userName = args[0];

if (!userName.Equals(un)) {
Environment.Exit(1);
return;
}

bool isValid = isValidUser(userName, dom, args[1]);
if (!isValid) {
Environment.Exit(1);
return;
}
}
}
}

Комментариев нет:

Отправить комментарий