WPF / C#Active Directory显示用户输入的名称 [英] WPF/C# Active Directory Display Name for user input
问题描述
大家好......我有一个Windows身份验证屏幕(WinLogin.xaml),它使用ADHelper.cs验证帐户。这很好用,但由于某种原因,我无法让系统将经过身份验证的用户的DISPLAY NAME传递回上一个屏幕。
基本上......
WinLogin.xaml - 输入用户名,密码并按登录 - 如果好的,转到form1 else msgbox。
Form1.xaml - 从Winlogin.xaml输入用户名的DISPLAY NAME到TBoxPharmSign.Text;
ADHelper .cs
Hello everyone... I have a Windows Authentication screen (WinLogin.xaml) and it uses an ADHelper.cs the validate an account. That works just great, but for some reason, I cannot get the the system to pass the authenticated user's DISPLAY NAME back to the previous screen.
Basically...
WinLogin.xaml - enter username, password and press Login -- if OK, go to form1 else msgbox.
Form1.xaml - enter username's DISPLAY NAME from Winlogin.xaml to TBoxPharmSign.Text;
ADHelper.cs
using System.DirectoryServices;
namespace BizLogic
{
public class AdHelper
{
#region AuthenticateUser Method
public bool AuthenticateUser(string domainName, string userName, string password)
{
var ret = false;
try
{
var de = new DirectoryEntry("LDAP://" + domainName, userName, password);
var dsearch = new DirectorySearcher(de);
dsearch.FindOne();
ret = true;
}
catch
{
ret = false;
}
return ret;
}
#endregion
}
}
WinLogin.cs
WinLogin.cs
using System.Windows;
using BizLogic;
namespace Forms
{
public partial class WinLogin
{
private void Window_Loaded(object sender, RoutedEventArgs e)
{
TxtDomain.Text = Environment.UserDomainName.ToLower();
}
private void WinLoginLogin_Click(object sender, RoutedEventArgs e)
{
var ad = new AdHelper();
if (ad.AuthenticateUser(TxtDomain.Text,
TxtUserName.Text, TxtPassword.Password))
{
DialogResult = true;
}
else
MessageBox.Show("Unable to Authenticate Using the Supplied Credentials");
}
}
}
最后,在Form1上需要帮助的点击操作.xaml
Finally, the click action where the help is needed on Form1.xaml
private void eSign_Click(object sender, RoutedEventArgs e)
{
//added for winlogin instance
var win = new WinLogin {Owner = this};
win.ShowDialog();
if (!win.DialogResult.HasValue || !win.DialogResult.Value)
{
Close();
}
else
{
TBoxPharmSign.Text = "AD Display Name From WinLogin.xaml - "
}
Log.Info("Cor551240415V1.EmployeeSearch_Click() called into existence for employee:");
}
推荐答案
我回答了我自己的问题。我向WinLogin.cs添加了一个公共空白
I answered my own question. I added a public void to WinLogin.cs
public void ReturnNames()
{
FullName = TxtUserName.Text;
Domain = TxtDomain.Text;
}
和Form1
and in Form1
//added for winlogin instance
var win = new WinLogin { Owner = this };
win.ShowDialog();
if (!win.DialogResult.HasValue || !win.DialogResult.Value)
Close();
else
win.ReturnNames();
TBoxPharmSign.Text = GetUserFullName(win.Domain,win.FullName);
}
还有Form1
And also in Form1
public static string GetUserFullName(string domain, string userName)
{
DirectoryEntry userEntry = new DirectoryEntry("WinNT://" + domain + "/" + userName + ",User");
return (string)userEntry.Properties["fullname"].Value;
}
简而言之,WinLogin.cs - ReturnNames()设置FullName和Domain等于用户在屏幕上输入的值。
Form1使用GetUserFullName()构建对AD的查询
Form1中的eSign_Click运行WinLogin - ReturnNames()并将文本框设置为GetUserFullName()的返回值。 。:)
in a nutshell, WinLogin.cs - ReturnNames() sets FullName and Domain equal to the values the user inputs on the screen.
Form1 builds the query to AD using GetUserFullName()
The eSign_Click in Form1 runs WinLogin - ReturnNames() and sets the textbox to the returned values from GetUserFullName()... :)
您好,
以下是帮助您从Active Directory获取详细信息的文章。
http://stackoverflow.com/questions/9845444/how-to-get-a-username-in-active-directory-from-a-display-name-in-c 一> [ ^ ]
http://myjeeva.com/querying-active-directory-using-csharp.html [ ^ ]
Hi,
Here are the articles that help you to get details form Active Directory.
http://stackoverflow.com/questions/9845444/how-to-get-a-username-in-active-directory-from-a-display-name-in-c[^]
http://myjeeva.com/querying-active-directory-using-csharp.html[^]
这篇关于WPF / C#Active Directory显示用户输入的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!