MOSS用那么久了,有人采用AD认证,有人采用FORM认证,我们公司是采用中间的替换方法,因为采用FORM认证个人觉得还是不是太方便,例如需要开两个端口等。个人有个做法把经验共享给大家,如果做得不对或者有更好的办法的话,可以联系我。
1.原因
为什么要进行表单登录,默认MOSS采用AD认证,是因为用户默认存储为AD,如果有人要把用户和以前的系统进行结合的方式,那么请采用正规的FORM认证进行开发。
我今天要讲的是AD认证,AD认证有个不好的方式就是弹出对话框,界面不友好,例如我要在界面上放一个忘记密码的方式是完全做不到的。
</P>
现在教大家一种采用表单进行提交的简单方法。其实这个办法很早的时候十一就搞过,不复杂,我只是稍微完善了一下。
1.首先采用匿名访问。
1.1IIS开启匿名访问。
</P>
1.2验证提供程序需要开启。操作方式:管理中心--》应用程序管理--》应用程序安全性--》验证提供程序--》默认
</P>
1.3开启网站匿名访问。操作为:网站操作--》高级权限--》设置--》匿名访问,如下
<BR></P>
2.设置登录界面
我们需要开发一个登录界面。输入用户。密码。
</
这个登录界面可以放在_LAYOUTS/目录底下。
登录界面代码请见附件。
需要改几个地址如下:
http://www.cnblogs.com/sharepoint/test/default.aspx 这个界面是需要验证的界面(默认网站已经开了匿名,只需要验证到其他一个非匿名访问的地址即可)
http://10.49.28.7 这个是我们要返回的界面
如果还不清楚的。可以访问http://keji.lickies.cn 查看。
3.登录后自动跳转到登录界面的方式,因为我们已经开放了匿名,默认我们是可以进去的,那么我们需要增加一段程序,判断如果AD用户为空的话则跳转到登录界面。
我们采用一个办法是在MASTERPAGE里增加一个USERCONTROL
如何做呢?打开默认的在12/TEMPLATE/GLOBRAL/DEFAULT.MASTER(如果有改过则到SPD里找)
在
<%@ Register TagPrefix="wssuc" TagName="Welcome" src="~/_controltemplates/Welcome.ascx" %>
下增加
<%@ Register TagPrefix="wssuc" TagName="login" Src="~/_controltemplates/chklogin9.ascx" %>
在<BODY scroll="yes" onload="javascript:if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();">
下增加
<wssuc:login id="chklogin" runat="server" EnableViewState="false"></wssuc:login>
可能很多人在问CHKLOGIN.ascx是啥。
就是我们的用户控件,,,这个文件如下。附件可以下载。。
<%@ Control Language="C#" Inherits="Microsoft.SharePoint.ApplicationPages.WebControls.ActionBar,Microsoft.
SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" compilationMode="Always" %>
<%@ Register Tagprefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%
if(HttpContext.Current.User.Identity.Name.ToString()=="")
{
Response.Redirect("/_layouts/web/login.aspx");
}
%>
请把这个文件放到c:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES下即可。
大概写了一下。有问题的话,大家QQ群联系。
using System.DirectoryServices;
--先应用
--添加用户
DirectoryEntry entry = new DirectoryEntry(BaseFunctions.DomainSrv,HttpContext.Current.Session["username"].ToString(),HttpContext.Current.Session["password"].ToString(), AuthenticationTypes.Secure);
DirectoryEntry subEntry = entry.Children.Find(ou.Value);
DirectoryEntry deUser = subEntry.Children.Add("cn=" +firstname.Text.ToString()+lastname.Text.ToString(), "user");
deUser.Properties["userPrincipalName"].Add((accountname.Text.ToString()+"@"+BaseFunctions.Domainname));
deUser.Properties["samAccountName"].Add(accountname.Text.ToString());//帐户
deUser.Properties["description"].Value = desc.Text.ToString();
deUser.Properties["sn"].Add(firstname.Text.ToString());
deUser.Properties["givenName"].Add(lastname.Text.ToString());
deUser.Properties["displayName"].Add(firstname.Text.ToString()+lastname.Text.ToString());
deUser.CommitChanges();
deUser.Invoke("ChangePassword",new object[]{"",password.Text.ToString()});
deUser.Properties["userAccountControl"].Value = 0x200;
deUser.CommitChanges();
deUser.Close();
lblShowInfo.Text="添加成功";
----修改密码
if(tb_newpass.Value.Trim()!=tb_newpass1.Value.Trim())
{
lblShowInfo.Text="两次输入的新密码不一样";
return;
}
try
{
DirectoryEntry de=new DirectoryEntry(BaseFunctions.DomainSrv,BaseFunctions.GetUserNameFromSession(),BaseFunctions.GetPassWord());
DirectorySearcher deSearch = new DirectorySearcher(de);
deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName="+Request.QueryString["username"]+"))";
deSearch.SearchScope = SearchScope.Subtree;
SearchResult result = deSearch.FindOne();
DirectoryEntry de1=result.GetDirectoryEntry();
object retResult=de1.Invoke("SetPassword", new Object[]{tb_newpass.Value.Trim()});
lblShowInfo.Text="成功修改密码!";
de1.Close();
de.Close();
de1.Dispose();
de.Dispose();
}
catch(Exception ex)
{
if(ex.InnerException!=null)
lblShowInfo.Text=ex.InnerException.Message;
else
lblShowInfo.Text+=ex.Message;
}
---将用户改变组织单位
DirectoryEntry de=new DirectoryEntry(BaseFunctions.DomainSrv,HttpContext.Current.Session["username"].ToString(),HttpContext.Current.Session["password"].ToString());
object o = de.NativeObject;
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(SAMAccountName=" + Server.UrlDecode(Request.QueryString["username"]) + ")";
ds.PropertiesToLoad.Add("cn");
SearchResult sr = ds.FindOne();//查找该用户
//Response.Write(sr.GetDirectoryEntry().Properties["name"][0]);
DirectoryEntry de2=sr.GetDirectoryEntry();//设置de2为用户
DirectoryEntry ou2 = de.Children.Find(Request.QueryString["ou"]);//获取新ou对象
de2.MoveTo(ou2);//移动OU
de2.CommitChanges();//提交
Response.Write("<script language=javascript>");
Response.Write("alert('移动完毕');");
Response.Write("opener.window.location.reload(true);");
Response.Write("window.close();");
Response.Write("</script>");
--用递归方式显示组织单位.还有小部分没做好,只是显示出来,没优化,有兴趣的同志可以改界面呈现方式
调用函数
GetOu("泉州移动",5);----5代表是显示5级
public void GetOu(string ouname,int amstr)
{
DirectoryEntry entry=new DirectoryEntry(BaseFunctions.DomainSrv,BaseFunctions.GetUserNameFromSession(),BaseFunctions.GetPassWord());
System.DirectoryServices.DirectoryEntry subentry=entry.Children.Find(ouname,"organizationalUnit");
string ax;
ax="";
string am;
am="";
foreach(DirectoryEntry res in subentry.Children)
{
am="";
foreach(DirectoryEntry res1 in res.Children)
{
if(res1.Name.Substring(0,3)=="OU=")
{
am=am+"1";
}
}
if(res.Name.Substring(0,3)=="OU=")//判断是组织单位还是其他类型,例如cn=用户,GROUP=组等
{
ax=ax+"1";
string []strArray;//用于存储上级目录的字符串
string ou;
ou="";
strArray=ouname.Split(',');//分割
for(int j=strArray.Length;j>0;j--)
{
if(j==1)
{
if(amstr==ax.Length)
{
HttpContext.Current.Response.Write("<img src=images/tree2.gif>");
}
else
{
HttpContext.Current.Response.Write("<img src=images/tree1.gif>");
}
}
else
{
HttpContext.Current.Response.Write("<img src=images/tree3.gif>");
}
}//根据上级目录的深度来显示目录树的层次
if(am=="")
{
Response.Write("<img src=images/tree6.gif>");
}
else
{
Response.Write("<img src=images/tree5.gif>");
}
string []strArray2;
string tem;
tem=res.Name.ToString()+","+ouname;//存储本级目录
strArray2=tem.Split(',');
for(int k=strArray2.Length;k>0;k--)
{
ou=ou+strArray2[k-1].Replace("OU=","/");
}
//OU存储本级目录的新格式,例如.泉州移动--网络部--IT中心等
if(HttpContext.Current.Request.QueryString["action"]=="move")
{
HttpContext.Current.Response.Write("<a onclick=movechk('"+Server.UrlEncode(res.Name.ToString()+","+ouname)+"','"+HttpContext.Current.Request.QueryString["username"]+"');>"+res.Name.ToString().Replace("OU=","")+"</a><br>");
}
else if(HttpContext.Current.Request.QueryString["action"]=="add")
{
HttpContext.Current.Response.Write("<a onclick=chk('"+ou+"','"+res.Name.ToString()+","+ouname+"');>"+res.Name.ToString().Replace("OU=","")+"</a><br>");
}
else
{
HttpContext.Current.Response.Write("<a onclick=magchk('"+Server.UrlEncode(res.Name.ToString()+","+ouname)+"');>"+res.Name.ToString().Replace("OU=","")+"</a><br>");
}
GetOu(res.Name.ToString()+","+ouname,am.Length);
}
else
{
}
}
----显示所有域用户的信息
DirectoryEntry de = new DirectoryEntry(BaseFunctions.DomainSrv,HttpContext.Current.Session["username"].ToString(),HttpContext.Current.Session["password"].ToString());
DirectorySearcher srch=new DirectorySearcher();
srch.Filter =("(objectclass=User)");
srch.SearchRoot=de;
srch.SearchScope = SearchScope.Subtree;
srch.PropertiesToLoad.Add("sn");
srch.PropertiesToLoad.Add("givenName");
srch.PropertiesToLoad.Add("uid");
srch.PropertiesToLoad.Add("telephoneNumber");
srch.PropertiesToLoad.Add("employeeNumber");
Response.Write("<table width=100% border=0 align=center Class=border");
Response.Write("<tr background=images/topbar_bg.gif>");
Response.Write("<td align=center height=22>帐号</td>");
Response.Write("<td align=center height=22>名字</td>");
Response.Write("<td align=center height=22>姓</td>");
Response.Write("<td align=center height=22>名</td>");
Response.Write("<td align=center height=22>显示名称</td>");
Response.Write("<td align=center height=22>手机</td>");
Response.Write("<td align=center height=22>部门</td>");
Response.Write("<td align=center height=22>状态</td>");
Response.Write("</tr>");
foreach(SearchResult res in srch.FindAll())
{
if(res.Path.IndexOf("泉州移动")>0)
{
Response.Write("<tr class=tdbg>");
Response.Write("<td align=center>"+res.GetDirectoryEntry().Properties["sAMAccountName"][0]+"</td>");
Response.Write("<td align=center>"+res.GetDirectoryEntry().Properties["Name"][0]+"</td>");
Response.Write("<td align=center>"+res.GetDirectoryEntry().Properties["Sn"][0]+"</td>");
try
{
Response.Write("<td align=center>"+res.GetDirectoryEntry().Properties["givenName"][0]+"</td>");
}
catch
{
Response.Write("<td align=center></td>");
}
Response.Write("<td align=center>"+res.GetDirectoryEntry().Properties["displayName"][0]+"</td>");
try
{
Response.Write("<td align=center>"+res.GetDirectoryEntry().Properties["telephoneNumber"][0]+"</td>");
}
catch
{
Response.Write("<td align=center></td>");
}
try
{
Response.Write("<td align=center>"+res.GetDirectoryEntry().Properties["mail"][0]+"</td>");
}
catch
{
Response.Write("<td align=center></td>");
}
string []strArray;
string str;
str="";
strArray=res.Path.Split(',');
for(int j=strArray.Length;j>0;j--)
{
if(strArray[j-1].Substring(0,3)=="OU=")
{
str=str+"-"+strArray[j-1].Replace("OU=","");
}
}
Response.Write("<td align=center>"+str+"</td>");
if(res.GetDirectoryEntry().Properties["userAccountControl"][0].ToString()=="514")
{
Response.Write("<td align=center>锁定</td>");
}
else
{
Response.Write("<td align=center>正常</td>");
}
/*
Response.Write("<td align=center>");
if(res.GetDirectoryEntry().Properties["userAccountControl"][0].ToString()=="514")
{
Response.Write("<input type=button value=解锁 onclick=\"javascript:window.open('setstate.aspx?action=1&username="+res.GetDirectoryEntry().Properties["sAMAccountName"][0]+"', 'poppage', 'toolbars=1, scrollbars=0, location=0, statusbars=1, menubars=0, resizable=1, width=50, height=50');\">");
}
else
{
Response.Write("<input type=button value=锁定 onclick=\"javascript:window.open('setstate.aspx?action=2&username="+res.GetDirectoryEntry().Properties["sAMAccountName"][0]+"', 'poppage', 'toolbars=1, scrollbars=0, location=0, statusbars=1, menubars=0, resizable=1, width=50, height=50');\">");
}
Response.Write("<input type=button value=移动部门 onclick=\"javascript:window.open('ou.aspx?action=move&username="+res.GetDirectoryEntry().Properties["sAMAccountName"][0]+"', 'poppage', 'toolbars=1, scrollbars=1, location=0, statusbars=1, menubars=0, resizable=1, width=200, height=650');\">");
Response.Write("<input type=button value=更改密码 onclick=\"javascript:window.open('changeuserpass.aspx?action=password&username="+res.GetDirectoryEntry().Properties["sAMAccountName"][0]+"', 'poppage', 'toolbars=1, scrollbars=1, location=0, statusbars=1, menubars=0, resizable=1, width=500, height=150');\">");
Response.Write("</td>");
*/
Response.Write("</tr>");
//CreateExcelFile2(res.GetDirectoryEntry().Password.ToString(),res.GetDirectoryEntry().Properties["Name"][0].ToString(),res.GetDirectoryEntry().Properties["telephoneNumber"][0].ToString(),res.GetDirectoryEntry().Properties["sn"][0].ToString(),res.GetDirectoryEntry().Properties["givenName"][0].ToString(),res.GetDirectoryEntry().Properties["displayName"][0].ToString());
}
}
Response.Write("</table>");