SharePoint發行版本有SharePoint2003、SharePoint2007、Sharepoint 2010、SharePoint2013和SharePoint2016。SharePoint提供了功能強大的團隊協作環境,使得組織能夠在整個組織內部實現整合、組織、查找和提供 SharePoint站點。 SharePoint 開啟了基于FBA的身份認證,經常會遇到用戶組用戶的問題,當我加入一個AD賬號,無法同時加入Form認證的用戶,這時,只能手動添加,比較麻煩;所以,寫了一個服務,用來每天晚上同步一下用戶組中的AD賬號和Form賬號。 原理 原理比較簡單,就是遍歷用戶組的所有用戶,同步的時候首先刪掉所有的Form賬號,然后根據所有的AD賬號去查找Form賬號重新添加;如果碰到AD安全組,就去安全組中遍歷所有的用戶,然后查找Form賬號添加,每天晚上定時執行。 優點 免去添加賬號的時候添加2次,而且服務可以手動執行,定時執行等; 缺點 并非實時同步,而且無法單獨在用戶組中加入Form賬號,無法滿足斷開權限,無法滿足單獨按照人授權的情況。 總結 在自己的需求中,用戶權限控制比較簡單,均按照用戶組授權,沒有唯一權限設置,所以用起來還是挺好的,而且用戶對于Form賬號沒有實時的要求。如果復雜的權限控制,還需進一步增強代碼,呵呵。 效果 執行前,只有AD賬號和AD安全組,如下圖: 執行后,多出了所有Form認證的賬號,如下圖: 代碼片段 遍歷所有用戶組 ![]() 1 using (SPSite site = new SPSite("http://SPServer")) 2 { 3 using (SPWeb web = site.RootWeb) 4 { 5 foreach (SPGroup group in web.Groups) 6 { 7 foreach (SPUser user in group.Users) 8 { 9 if (user.LoginName.IndexOf("custommembership") > 0)10 {11 group.RemoveUser(user);12 }13 }14 15 foreach (SPUser user in group.Users)16 {17 if (user.LoginName.IndexOf("domain") > 0)18 {19 group.Users.Add("i:0#.f|custommembership|" + user.Email, user.Email, user.LoginName, user.Notes);20 }21 22 if (user.IsDomainGroup)23 {24 DomainGroup(group, user.Name);25 }26 }27 }28 }29 } ![]() 去AD目錄中查找AD賬號 ![]() ![]() 1 public static string DomainGroup(SPGroup group, string DomainGroupName) 2 { 3 string returnStr = string.Empty; 4 SearchResultCollection results = null; 5 6 string filter = "(&(objectClass=group)(cn=" + DomainGroupName + "))"; 7 string connectionPrefix = "LDAP://linyu.ad.com.cn"; 8 using (DirectoryEntry root = new DirectoryEntry(connectionPrefix)) 9 {10 using (DirectorySearcher searcher = new DirectorySearcher(root))11 {12 searcher.ReferralChasing = ReferralChasingOption.All;13 searcher.SearchScope = SearchScope.Subtree;14 searcher.Filter = filter;15 results = searcher.FindAll();16 }17 }18 foreach (SearchResult sr in results)19 {20 21 DirectoryEntry deGroup = new DirectoryEntry(sr.Path);22 System.DirectoryServices.PropertyCollection pcoll = deGroup.Properties;23 int n = pcoll["member"].Count;24 Console.WriteLine(n.ToString());25 26 for (int i = 0; i < n;="" i++)27="" {28="" directoryentry="" deuser="new" directoryentry(connectionprefix="" +="" "/"="" +="" pcoll["member"][i].tostring());29="" 30="" string="" username="deUser.Name.ToString();31" if="" (username.indexof("=") > 0)32 {33 username = username.Split('=')[1];34 }35 36 string email = GetProperty(deUser, " mail");37="" if="" (email.indexof("@")=""> 0)38 {39 Console.WriteLine(username);40 group.AddUser("i:0#.f|custommembership|" + email, email, username, "");41 }42 }43 }44 return returnStr;45 }46 47 public static string GetProperty(DirectoryEntry oDE, string PropertyName)48 {49 try50 {51 if (oDE.Properties.Contains(PropertyName))52 {53 return oDE.Properties[PropertyName][0].ToString();54 }55 else56 {57 return string.Empty;58 }59 }60 catch (Exception ee)61 {62 throw ee;63 }64 } ![]() 后記 思路、代碼比較簡單,希望給大家一個參考吧;運行過程中,可以封裝成TimerJob、控制臺和Windows計劃任務、Windows服務等均可,看大家需要和熟悉程度吧。 Sharepoint 可以幫助企業用戶輕松完成日常工作。 |
溫馨提示:喜歡本站的話,請收藏一下本站!