AD域验证

1.手动获取域账号和密码进行验证

public class VerifyADDomain {

@SuppressWarnings("unchecked")
public int verifyADbyUser(String name, String password) {
int isLogin = -2;//isLogin=0 验证通过
// 读取资源文件


// String host = "10.2.1.17"; // AD服务器
// String port = "389"; // 端口


String url = PropertyManager.getValue("ad_ldap_url").trim();
// String adminName = "hn.net\\" + name; // 注意用户名的写法:domain\\User
String adminName = name + "@"
+ PropertyManager.getValue("ad_domain_name");
// 域节点
// String searchBase = "DC=hn,DC=net";
// LDAP搜索过滤器类
// String searchFilter = "(&(mail=" + name + "@cbhb.com.cn))";
String enable = PropertyManager.getValue("ad_enable");
if (enable == null || !enable.trim().toLowerCase().equals("true")) {
isLogin = -2;
return isLogin;
}
// searchBase = PropertyManager.getValue("ad_search_base_node");
// searchFilter = PropertyManager.getValue("ad_search_filter");
// searchFilter = searchFilter.replaceAll("username", name);


// 定义返回数据的属性
// String returnedAtts[] = { "cn", "department" };
// String returnedAtts[] = { "name", "departmentNumber", "department",
// "mail" };
Hashtable<String, String> HashEnv = new Hashtable<String, String>();
HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别
HashEnv.put(Context.SECURITY_PRINCIPAL, adminName); // AD User
HashEnv.put(Context.SECURITY_CREDENTIALS, password); // AD Password
HashEnv.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工厂类
HashEnv.put(Context.PROVIDER_URL, url);
LdapContext ctx = null;
try {
ctx = new InitialLdapContext(HashEnv, null);
System.out.println("验证通过");
isLogin = 0;
// // 搜索控制器
// SearchControls searchCtls = new SearchControls(); // Create the
// searchCtls.setTimeLimit(1000);
// // 创建搜索控制器
// searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); //
// Specify
// // Specify the search scope 设置搜索范围
// searchCtls.setReturningAttributes(returnedAtts); // 设置返回属性集
// // 根据设置的域节点、过滤器类和搜索控制器搜索LDAP得到结果
// searchFilter = null;
// NamingEnumeration answer = ctx.search(searchBase,
// searchFilter,searchCtls);// Search for objects using the filter
// //创建用户对象
// icUser=new IcUser();
// //icUser.setLoginName(name);
// //icUser.setPwd(password);
// while (answer.hasMoreElements()) {// 遍历结果集
// SearchResult sr = (SearchResult) answer.next();// 得到符合搜索条件的DN
// Attributes Attrs = sr.getAttributes();// 得到符合条件的属性集
// if (Attrs != null) {
// for (NamingEnumeration ne = Attrs.getAll(); ne.hasMore();) {
// Attribute Attr = (Attribute) ne.next();// 得到下一个属性
// // 读取属性值
// if (Attr.getID().toString().equals("name")) {
// for (NamingEnumeration e = Attr.getAll(); e
// .hasMore();) {
// //icUser.setDisplayName(e.next().toString());
// }
// }
// if (Attr.getID().toString().equals("mail")) {
// for (NamingEnumeration e = Attr.getAll(); e
// .hasMore(); ) {
// //icUser.setEmail(e.next().toString());
// }
// }
// if (Attr.getID().toString().equals("department")) {
// for (NamingEnumeration e = Attr.getAll(); e
// .hasMore(); ) {
// //icUser.setDepartmentName(e.next().toString());
// }
// }
// if (Attr.getID().toString().equals("departmentNumber")) {
// for (NamingEnumeration e = Attr.getAll(); e
// .hasMore(); ) {
// //icUser.setDepartmentCode(e.next().toString());
// }
// }
// }
// }
// }
} catch (NamingException e) {
e.printStackTrace();
System.err.println("AD验证失败 : 用户名:" + name);
isLogin = -2;
} finally {
try {
if (ctx != null) {
ctx.close();
}
} catch (NamingException e) {
e.printStackTrace();
}
}
return isLogin;
}

配置参数:
ad_enable=true
ad_ldap_url=ldap://10.2.1.17:389
ad_domain_name=hn.net
ad_search_base_node=DC\=hn,DC\=net
ad_search_filter=(&(mail\=username@cbhb.com.cn))

2.利用jcifs-1.3.17.jar,配置过滤器,自动验证

<filter> 
<filter-name>NtlmHttpFilter</filter-name> 
<filter-class>jcifs.http.NtlmHttpFilter</filter-class> 
<init-param> 
<param-name>jcifs.http.domainController</param-name> 
<param-value>10.2.1.17</param-value> 
</init-param>  
  
<init-param>   
<param-name>jcifs.smb.client.domain</param-name> 
<param-value>hn.net</param-value> 
</init-param>  


<init-param> 
<param-name>jcifs.smb.lmCompatibility</param-name> 
<param-value>0</param-value> 
</init-param> 

<init-param> 
 <param-name>jcifs.smb.client.useExtendedSecurity</param-name> 
<param-value>false</param-value> 
</init-param> 
<init-param> 
  <param-name>jcifs.smb.client.soTimeout</param-name> 
 <param-value>500</param-value> 
</init-param>
</filter>
<filter-mapping>
<filter-name>NtlmHttpFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

此方法是不支持NTLM2协议,而当客户端是WIN7系统时,默认采用的是NTLM2协议,因此需要手动进行设置。在 管理工具->本地安全策略->本地策略->安全选项,将 网络安全:LAN Manager 身份验证级别 属性改为LM & NTLM - 如果已协商,使用NTLMv2 会话安全。

3. 使用IIS服务器进行验证

安装IIS 6.0及.NET Framework 3.5,在网站->属性->目录安全性,取消 启用匿名访问,勾选 Windows域服务器的摘要式身份验证,再下面的 领域 中选择计算机所在域名。然后将下面的aspx文件放入网站,通过该aspx页面获取登录用户的用户名,再跳转到需要访问的系统页面。

Default.aspx页面

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Redirect Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>


Default.aspx.cs页面

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("正在跳转 ....");
        if ((System.Web.HttpContext.Current.Session["Account"] == null) | (System.Web.HttpContext.Current.Session["dtSec"] == null) | (System.Web.HttpContext.Current.Session["dtJiYao"] == null))
        {
            //取得用户姓名并删除域信息
            //string strAcctEname = String.Empty;
            string strUserEname = System.Web.HttpContext.Current.User.Identity.Name;
            int ilen = strUserEname.IndexOf('\\', 0);
            strUserEname = strUserEname.Substring(ilen + 1, strUserEname.Length - ilen - 1);
            Response.Redirect("需要访问的系统地址");
        }
    }
}


转载于:https://my.oschina.net/u/994081/blog/129494

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值