java域验证_如何验证域凭据?

我使用以下代码来验证凭据 . 下面显示的方法将确认凭据是否正确,如果密码已过期或需要更改 .

我一直在寻找这样的东西...所以我希望这有助于某人!

using System;

using System.DirectoryServices;

using System.DirectoryServices.AccountManagement;

using System.Runtime.InteropServices;

namespace User

{

public static class UserValidation

{

[DllImport("advapi32.dll", SetLastError = true)]

static extern bool LogonUser(string principal, string authority, string password, LogonTypes logonType, LogonProviders logonProvider, out IntPtr token);

[DllImport("kernel32.dll", SetLastError = true)]

static extern bool CloseHandle(IntPtr handle);

enum LogonProviders : uint

{

Default = 0, // default for platform (use this!)

WinNT35, // sends smoke signals to authority

WinNT40, // uses NTLM

WinNT50 // negotiates Kerb or NTLM

}

enum LogonTypes : uint

{

Interactive = 2,

Network = 3,

Batch = 4,

Service = 5,

Unlock = 7,

NetworkCleartext = 8,

NewCredentials = 9

}

public const int ERROR_PASSWORD_MUST_CHANGE = 1907;

public const int ERROR_LOGON_FAILURE = 1326;

public const int ERROR_ACCOUNT_RESTRICTION = 1327;

public const int ERROR_ACCOUNT_DISABLED = 1331;

public const int ERROR_INVALID_LOGON_HOURS = 1328;

public const int ERROR_NO_LOGON_SERVERS = 1311;

public const int ERROR_INVALID_WORKSTATION = 1329;

public const int ERROR_ACCOUNT_LOCKED_OUT = 1909; //It gives this error if the account is locked, REGARDLESS OF WHETHER VALID CREDENTIALS WERE PROVIDED!!!

public const int ERROR_ACCOUNT_EXPIRED = 1793;

public const int ERROR_PASSWORD_EXPIRED = 1330;

public static int CheckUserLogon(string username, string password, string domain_fqdn)

{

int errorCode = 0;

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain_fqdn, "ADMIN_USER", "PASSWORD"))

{

if (!pc.ValidateCredentials(username, password))

{

IntPtr token = new IntPtr();

try

{

if (!LogonUser(username, domain_fqdn, password, LogonTypes.Network, LogonProviders.Default, out token))

{

errorCode = Marshal.GetLastWin32Error();

}

}

catch (Exception)

{

throw;

}

finally

{

CloseHandle(token);

}

}

}

return errorCode;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值