.net 域用户登录

使用C#访问 Active Directory域服务,需引用System.DirectoryServices命名空间,该命名空间包含两个组件类,DirectoryEntry和 DirectorySearcher。

DirectoryEntry类可封装 ActiveDirectory域服务层次结构中的节点或对象,使用此类绑定到对象、读取属性和更新特性;
使用DirectorySearcher类可对 Active Directory域服务层次结构执行查询。
如果仅要对AD的组织单元,群组和用户进行查询或者更改属性,使用上述两个类即可。

下面给出操作AD域的部分代码:

1、连接到域

DirectoryEntry objDE = new DirectoryEntry(ADname, Loginname, Loginpwd);

ADname :是域名,一般格式是:"LDAP://****";****为域的名字(一般是大写的英文字符串)
Loginname: 登录域的用户名(保存在域中的名字,一般是中文名字的拼音)
Loginpwd: 用户名对应的登录密码

2、查询域中的所有用户

DirectoryEntry objDE = new DirectoryEntry(ADtxt, Logintxt, Loginpwdtxt);
string strFilter = “(&(objectCategory=person)(objectClass=user))”;
DirectorySearcher objSearcher = new DirectorySearcher(objDE, strFilter);
//排序
objSearcher.Sort = new SortOption(“name”, SortDirection.Ascending);
SearchResultCollection src = objSearcher.FindAll();
src 中保存了所有用户的所有信息

如果想查找域中所有的组织单元,可以把筛选字符串改为:
string strFilter = “(&(objectCategory=organizationalUnit)(objectClass=organizationalUnit))”;

如果想查找域中所有的群组,可以把筛选字符串改为:
string strFilter = “(&(objectCategory=group)(objectClass=group))”;

2.1 查看用户的所有属性

//获取域用户属性
DirectoryEntry objDE = new DirectoryEntry(ADtxt, Logintxt, Loginpwdtxt);
string strFilter = “(&(objectCategory=person)(objectClass=user))”;
DirectorySearcher objSearcher = new DirectorySearcher(objDE, strFilter);
//排序
objSearcher.Sort = new SortOption(“name”, SortDirection.Ascending);
SearchResultCollection src = objSearcher.FindAll()
DirectoryEntry entry = src[102].GetDirectoryEntry();
foreach (string pro in entry.Properties.PropertyNames)
{
this.tbLog.Text += entry.Properties[pro].PropertyName + “…” + entry.Properties[pro].Value.ToString() + " \r\n";
}

或者:

foreach (string strPropNamein src[102].Properties.PropertyNames)
{
this.tbLog.Text +=strPropName;
}

2.2 查看用户的属性内容

foreach (SearchResult sr in src)
{
string chinesename = sr.Properties[“name”][0].ToString();
string englishname= sr.Properties[“samaccountname”][0].ToString();}

本文出自he ivy 的博客:http://blog.csdn.net/heivy/article/details/53505916

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET 自动登录(AD自动登录)可以让用户在访问Web应用程序时无需再次输入他们的凭据。它允许用户在他们的计算机上通过Windows身份验证登录,并自动通过ASP.NET应用程序进行身份验证。 以下是实现ASP.NET自动登录(AD自动登录)的步骤: 1. 在Web.config文件中启用Windows身份验证: ``` <authentication mode="Windows" /> ``` 2. 在Global.asax文件中添加以下代码: ``` protected void Application_AuthenticateRequest(Object sender, EventArgs e) { if (HttpContext.Current.User != null) { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.User.Identity is WindowsIdentity) { // Get the Windows identity. var windowsIdentity = (WindowsIdentity)HttpContext.Current.User.Identity; // Get the user name and domain name. var userName = windowsIdentity.Name; var domainName = windowsIdentity.Name.Split('\\')[0]; // Authenticate the user against Active Directory. if (AuthenticateUser(userName, domainName)) { // Create a new identity using the user name and domain name. var identity = new GenericIdentity(userName, "Windows"); // Get the roles for the user from Active Directory. var roles = GetRolesForUser(userName, domainName); // Attach the roles to the identity. HttpContext.Current.User = new GenericPrincipal(identity, roles); } else { // Redirect the user to the login page. FormsAuthentication.RedirectToLoginPage(); } } } } } private bool AuthenticateUser(string userName, string domainName) { // Authenticate the user against Active Directory. // Return true if the user is authenticated, false otherwise. } private string[] GetRolesForUser(string userName, string domainName) { // Get the roles for the user from Active Directory. // Return an array of role names. } ``` 3. 在Web应用程序中创建一个登录页面。 4. 在登录页面中添加以下代码: ``` protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.IsAuthenticated) { // Redirect the user to the default page. Response.Redirect("~/Default.aspx"); } } } protected void btnLogin_Click(object sender, EventArgs e) { // Authenticate the user against Active Directory. if (AuthenticateUser(txtUserName.Text, txtPassword.Text)) { // Redirect the user to the default page. FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false); } else { // Display an error message. lblErrorMessage.Text = "Invalid username or password."; } } private bool AuthenticateUser(string userName, string password) { // Authenticate the user against Active Directory. // Return true if the user is authenticated, false otherwise. } ``` 这些步骤将帮助您实现ASP.NET自动登录(AD自动登录)。当用户访问您的Web应用程序时,他们将通过Windows身份验证自动登录,并自动通过ASP.NET应用程序进行身份验证。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值