MOSS ad组的获取及Hashtable作缓存总结

问题一:
在MOSS项目中,通过web.SiteGroups[i].ContainsCurrentUser可以判断到当前登录的用户是否是AD域中的用户
即使当前用户在MOSS站点中并不存在。
而且通过web.AllUsers或web.SiteUserInfoList都可以判断到AD域中的用户。
但是,如果使用web.SiteGroups[i].Users["域用户帐号"] 时,却会报错。这个时候该怎么弄呢?
 我的解决方案是通过模拟用户来操作,代码如下:
 1 ExpandedBlockStart.gif ContractedBlock.gif   /**/ /// <summary>
 2InBlock.gif        /// 获取用户所属于的组
 3InBlock.gif        /// </summary>
 4InBlock.gif        /// <param name="user"></param>
 5ExpandedBlockEnd.gif        /// <returns></returns>

 6 None.gif          private  IList < SPGroup >  GetUserExistInGroups(SPUser user)
 7 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 8InBlock.gif            try
 9ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
10InBlock.gif                IList<SPGroup> tempGroups = new List<SPGroup>();
11InBlock.gif
12InBlock.gif                SPSecurity.RunWithElevatedPrivileges(delegate()
13ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
14InBlock.gif                    SPUser tempUser = web.AllUsers[user.LoginName];
15InBlock.gif                    using (SPSite tempSite = new SPSite(site.Url, tempUser.UserToken))
16ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
17InBlock.gif                        using (SPWeb tempWeb = tempSite.OpenWeb())
18ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
19InBlock.gif                            SPGroupCollection groups = tempWeb.SiteGroups;
20InBlock.gif                            foreach (SPGroup group in groups)
21ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
22InBlock.gif                                if (group.ContainsCurrentUser)
23ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
24InBlock.gif                                    tempGroups.Add(group);
25ExpandedSubBlockEnd.gif                                }

26ExpandedSubBlockEnd.gif                            }

27InBlock.gif
28ExpandedSubBlockEnd.gif                        }

29ExpandedSubBlockEnd.gif                    }

30ExpandedSubBlockEnd.gif                }
);
31InBlock.gif                return tempGroups;
32ExpandedSubBlockEnd.gif            }

33InBlock.gif            catch
34ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
35InBlock.gif                return null;
36ExpandedSubBlockEnd.gif            }

37ExpandedBlockEnd.gif        }

如上可以获取无论是AD域中的用户还是MOSS站点的用户所属的所有组。。

另一个Hashtable缓存,在不方便使用System.Web.Caching.Cache或HttpRuntime.Cache,或HttpContext.Current.Cache的时候,可以试试,代码如下:
 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif    /// 哈希表缓存
 3InBlock.gif    /// 作者:徐志泽
 4InBlock.gif    /// 创建日期:2008-12-26
 5ExpandedBlockEnd.gif    /// </summary>

 6 None.gif      public   class  HashCacheTool
 7 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 8InBlock.gif        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
 9InBlock.gif
10ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
11InBlock.gif        /// 保存缓存值
12InBlock.gif        /// </summary>
13InBlock.gif        /// <param name="cacheKey"></param>
14ExpandedSubBlockEnd.gif        /// <param name="cmdParms"></param>

15InBlock.gif        public static void SetCache(string cacheKey, object cacheValue)
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            parmCache[cacheKey] = cacheValue;
18ExpandedSubBlockEnd.gif        }

19ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
20InBlock.gif        /// 获取缓存值 
21InBlock.gif        /// </summary>
22InBlock.gif        /// <param name="cacheKey"></param>
23ExpandedSubBlockEnd.gif        /// <returns></returns>

24InBlock.gif        public static object GetCached(string cacheKey)
25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
26InBlock.gif            return parmCache[cacheKey];
27ExpandedSubBlockEnd.gif        }

28ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
29InBlock.gif        /// 清空所有
30ExpandedSubBlockEnd.gif        /// </summary>

31InBlock.gif        public static void Clear()
32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
33InBlock.gif            if (parmCache.Count <= 0)
34InBlock.gif                return;
35InBlock.gif            parmCache.Clear();
36ExpandedSubBlockEnd.gif        }

37ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
38InBlock.gif        /// 判断关键字是否存在
39InBlock.gif        /// </summary>
40InBlock.gif        /// <param name="key"></param>
41ExpandedSubBlockEnd.gif        /// <returns></returns>

42InBlock.gif        public static bool IsKeyExist(string cacheKey)
43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
44InBlock.gif            if (parmCache.Count <= 0)
45InBlock.gif                return false;
46InBlock.gif            return parmCache.ContainsKey(cacheKey);
47ExpandedSubBlockEnd.gif        }

48ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
49InBlock.gif        /// 如果关键字不存在的话,则清空所有
50InBlock.gif        /// </summary>
51ExpandedSubBlockEnd.gif        /// <param name="key"></param>

52InBlock.gif        public static void ClearWhileKeyNotExist(string cacheKey)
53ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
54InBlock.gif            if (parmCache.Count <= 0)
55InBlock.gif                return;
56InBlock.gif            if (IsKeyExist(cacheKey))
57InBlock.gif                return;
58InBlock.gif            else
59InBlock.gif                Clear();
60ExpandedSubBlockEnd.gif        }

61ExpandedBlockEnd.gif    }
其中的
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
是同步封装。
我就曾因为同步问题,导致在Loadrunner中多并发测试时,几乎90%的用户会报500错误。

转载于:https://www.cnblogs.com/McJeremy/archive/2008/12/26/1363088.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值