AD域賬號驗證


public partial class _Default : Page
{
[DllImport("advapi32.dll")]
private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
protected void Page_Load(object sender, EventArgs e)
{
lblLocaAdAccount.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
}

protected void btnSave_Click(object sender, EventArgs e)
{
string strDomain = txtDomain.Text.Trim();
string strUserName = txtUserName.Text.Trim();
string strPassWord = txtPassWord.Text.Trim();

lblMsg.Text = "驗證中…";
bool isAccount = ValidateUserAccount(strDomain, strUserName, strPassWord);
if (isAccount)
{
lblMsg.Text = "合法用戶";
}
else {
lblMsg.Text = "非法用戶";
}


if (isAccount)
{
//完全采用域用戶來管理
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // version
strUserName, // user name
DateTime.Now, // issue time
DateTime.Now.AddHours(1), // expires every hour
false, // don't persist cookie
"" // roles
);
FormsAuthentication.SetAuthCookie(strUserName, false);
}
}

/// <summary>
/// 域驗證
/// </summary>
/// <param name="AstrDomainName">域名稱</param>
/// <param name="AstrDomainAccount">域賬號</param>
/// <param name="AstrDomainPassword">域密碼</param>
/// <returns></returns>
public bool ValidateUserAccount(string AstrDomainName, string AstrDomainAccount, string AstrDomainPassword)
{
const int LOGON32_LOGON_INTERACTIVE = 2; //通過網絡驗證賬戶合法性
const int LOGON32_PROVIDER_DEFAULT = 0; //使用默認的Windows 2000/NT NTLM驗證方
IntPtr tokenHandle = new IntPtr(0);
tokenHandle = IntPtr.Zero;

string domainName = AstrDomainName; //域 如:officedomain
string domainAccount = AstrDomainAccount; //域帳號 如:administrator
string domainPassword = AstrDomainPassword;//密碼
bool checkok = LogonUser(domainAccount, domainName, domainPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);

return checkok;
}
}

 

 

=======================================================================

其他參考代碼 ===============================================================

=======================================================================

WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
WindowsPrincipal currentPrincipal = new WindowsPrincipal(currentIdentity);

bool isDomainUser = currentPrincipal.IsInRole("Domain Users");
isDomainUser=true; //為域用戶

string userName=System.Environment.UserName;  //獲取計算機登錄名稱

string computerName=System.Environment.MachineName;//獲取計算機名稱

string computerName=Dns.GetHostName();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM