sharepoint2010 基于AD的表单验证 用户登录后显示登录帐户不显示中文姓名
上一个文章我讲了sharepoint2010 基于AD的表单验证设置方式
这里我遇到新的问题就是用户登录后显示的名字是验证连接名称+帐号组成的名字如:i:0#.f|ADMembership|liu.han
而不是中文名 刘寒
原因是登录后没有将登录人信息添加进站点中
所以我的思路是:
1、在登录时去AD服务器查找验证成功后的登录人信息
2、添加此登录人至网站的某个权限组(portal访问者)(这里可以判断改组里是否已经存在此人)
3、判断此人信息用户名是否为中文,如果是否,修改为中文名称(中文名称是在AD服务器中查询得到)
4、跳转至站点首页
我们修改思路已经明确剩下的就要动手解决它,下面是关键部分源码,我这里重新创建了
C:\inetpub\wwwroot\wss\VirtualDirectories\80\_forms\Default.aspx 文件
/// <summary> /// 从AD中获取用户信息 /// </summary> /// <param name="domain"></param> /// <param name="username"></param> /// <param name="pwd"></param> /// <returns>[loginName][email][name][notes]</returns> public string[] IsAuthenticated(string domain, string username, string pwd) { string[] UserInformation = new string[] { "", "", "", "" }; string fullname = string.Empty; string domainAndUsername = domain + @"\" + username; string _path = LDAP://bj-dc-14.myAD.ad/DC=myAD,DC=ad; DirectoryEntry entry = new DirectoryEntry(_path, username, pwd); try { //Bind to the native AdsObject to force authentication. object obj = entry.NativeObject; DirectorySearcher search = new DirectorySearcher(entry); search.Filter = "(SAMAccountName=" + username + ")"; search.PropertiesToLoad.Add("cn"); SearchResult result = search.FindOne(); if (null == result) { return UserInformation; } else { DirectoryEntry user = result.GetDirectoryEntry(); //if (user.Properties.Contains("sAMAccountName")) //UserInformation[0] = user.Properties["sAMAccountName"][0].ToString();//loginName //表单登录后不能直接用AD登录人帐号信息拼接的, //这里可能根据自己情况来拼接字符串了,希望谁有好的方法改进它 UserInformation[0] = "i:0#.f|ADMembership|" + username; if (user.Properties.Contains("mail")) UserInformation[1] = user.Properties["mail"][0].ToString();//email //获取用户名称(通常是中文名) if (user.Properties.Contains("displayName")) UserInformation[2] = user.Properties["displayName"][0].ToString();//name UserInformation[4] = "";//notes } } catch (Exception ex) { return UserInformation; } return UserInformation; }
/// <summary> /// 更新指定人员信息 /// </summary> /// <param name="loginName"></param> /// <param name="UserInformation"></param> public void UpdateUserChineseName(string loginName, string[] UserInformation) { try { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite oSite = new SPSite(SPContext.Current.Site.ID)) { using (SPWeb oWeb = oSite.OpenWeb()) { oWeb.AllowUnsafeUpdates = true; SPUser user = null; user = oWeb.EnsureUser(UserInformation[0]); if (user != null) { if (!HaveChineseString(user.Name)) { user.Name = UserInformation[2]; user.Update(); } } try { SPGroup oGroup = oWeb.Groups["portal访问者"]; oGroup.AddUser(user); oGroup.Update(); } catch { } oWeb.AllowUnsafeUpdates = false; } } }); } catch (Exception ex) { this.Page.Response.Write(ex.Message); } }
/// <summary> /// 校验是否是中文 /// </summary> /// <param name="str"></param> /// <returns></returns> public bool HaveChineseString(string str) { //如果值为空,通过校验 System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("([\u4E00-\u9FA5])+"); if (!regex.IsMatch(str)) { return false; } return true; }
protected void signInControl_Authenticate(object sender, AuthenticateEventArgs e) { SecurityToken token = null; LoginControl formsLoginControl = sender as LoginControl; if (null != (token = GetSecurityToken(formsLoginControl))) { EstablishSessionWithToken(token); e.Authenticated = true; //验证登录成功后执行以下代码 //获取登录人信息 string[] UserInformation = IsAuthenticated("myAD", formsLoginControl.UserName, formsLoginControl.Password); //更新当前登录人信息 UpdateUserChineseName(formsLoginControl.UserName, UserInformation); //跳转至站点首页 base.RedirectToSuccessUrl(); } }
解决方案编译完成后把DLL文件更新至
C:\inetpub\wwwroot\wss\VirtualDirectories\80\BIN\
页面文件(Default.aspx)替换
C:\inetpub\wwwroot\wss\VirtualDirectories\80\_forms\Default.aspx
在web.config中修改添加以下节点
<SafeControl Assembly="FormsSignInPage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=72d2bbe72853b8eb" Namespace="FormsSignInPage" TypeName="*" Safe="True" SafeAgainstScript="False" />
最终效果

但很奇怪的是我无法断点调试。。。。。只能靠扎实的编程功底无调试完成以上代码。。。。= =#
欢迎大家来Moss技术交流群来做技术交流:群号:69022156
浙公网安备 33010602011771号