使用Windows用户登录功能在活动目录中搜索! [英] used windows user logged function for search in active directory !!
问题描述
早安,
使用此代码源,我会找到用户,该用户将通过在文本框中插入的名称(在本例中为txtusername)拥有来自活动目录的信息.
我不希望手动插入名称,但是希望返回登录到Windows的用户名的函数自动使用该名称.
我能怎么做?有人可以帮我吗?您可以修改源代码吗?
这是代码源:
Goodmorning,
with this code source i find the user that i will to have the information from the active directory by the name that i insert in the textbox(in this case txtusername).
i would like to not insert the name manually, but I would like to be taken automatically by the function that returns the name of the user logged into windows.
how can I do? someone could help me please? You can modify the source code please?
this is the code source:
string connection = ConfigurationManager.ConnectionStrings["ADConnection"].ToString();
DirectorySearcher dssearch = new DirectorySearcher(connection);
dssearch.Filter = "(sAMAccountName=" + txtusername.Text + ")";
SearchResult sresult = dssearch.FindOne();
DirectoryEntry dsresult = sresult.GetDirectoryEntry();
lblmittente_nome.Text = dsresult.Properties["givenName"][0].ToString();
lblmittente_cognome.Text = dsresult.Properties["sn"][0].ToString();
lblmail.Text = dsresult.Properties["mail"][0].ToString();
lbldata.Text = DateTime.Now.ToString("dd MMMM yyyy");
lblfirma_nome.Text = dsresult.Properties["givenName"][0].ToString();
lblfirma_cognome.Text = dsresult.Properties["sn"][0].ToString();
lblfunzione.Text = dsresult.Properties["department"][0].ToString();
谢谢
推荐答案
使用
Environment.UserName
来获取记录的用户名.
我正在使用以下代码:
I am using following code :
public class UserIdentity : WindowsIdentity
{
private int userId = -1;
internal FrakoEntitiesConnection context;
private UserIdentity(WindowsIdentity windowsIdentity)
: base(windowsIdentity.Token)
{
}
public new static UserIdentity GetCurrent()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
return new UserIdentity(identity);
}
public string UserName
{
get
{
string result = this.Name;
if (result.LastIndexOf('\\') == -1) return result;
else return result.Remove(0, result.LastIndexOf('\\') + 1);
}
}
public int UserId
{
get
{
if (userId != -1) return userId;
return 0;
}
}
}
这是我使用的身份验证/安全系统的一部分.
It''s part of an authentication/security system i use.
这篇关于使用Windows用户登录功能在活动目录中搜索!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!