【发布时间】:2018-10-11 10:13:17
【问题描述】:
我有一个使用 Windows Active Directory 获取当前 Windows 帐户名称的 Winform 客户端。
有什么方法可以知道此解决方案是否可以在不进行设置的情况下与新的 Windows Server 2016 Active Directory 一起使用?
客户端代码
public string GetCurrentActiveDirectoryAccountName()
{
var windowsName = WindowsIdentity.GetCurrent().Name;
var index = windowsName.LastIndexOf("\\");
if (index > 0)
windowsName = windowsName.Substring(index + 1);
return windowsName;
}
public void AuthenticateActiveDirectoryAccount(string username, string password)
{
//Hidden code to setup variables
if (ADUserName.Length > 0)
context = new PrincipalContext(ContextType.Domain, ADServer, ADUserName, ADUserPassword);
else
context = new PrincipalContext(ContextType.Domain, ADServer);
using (context)
{
if (!context.ValidateCredentials(account, password))
//Hidden code to throw exception
}
}
public string CheckActiveDirectoryAccount(string account)
{
///Hidden code to setup variables
if (ADUserName.Length > 0)
context = new PrincipalContext(ContextType.Domain, ADServer, null, ADUserName, ADUserPassword);
else
context = new PrincipalContext(ContextType.Domain, ADServer);
using (context)
{
if ((user = UserPrincipal.FindByIdentity(context, account)) == null)
{
if (account.Contains("\\"))
{
userPrincipalNameList = user.UserPrincipalName.Split('\\').ToList();
if (userPrincipalNameList.Count > 0)
user = UserPrincipal.FindByIdentity(context, userPrincipalNameList[0]);
}
}
if (user != null)
{
using (user)
{
userAccount = user.SamAccountName;
return userAccount.ToLower();
}
}
}
return string.Empty;
}
【问题讨论】:
标签: c# authentication active-directory principalcontext