现在的位置: 首页 > 综合 > 正文

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

2014年11月12日 ⁄ 综合 ⁄ 共 3507字 ⁄ 字号 评论关闭

问题一:
在MOSS项目中,通过web.SiteGroups[i].ContainsCurrentUser可以判断到当前登录的用户是否是AD域中的用户
即使当前用户在MOSS站点中并不存在。
而且通过web.AllUsers或web.SiteUserInfoList都可以判断到AD域中的用户。
但是,如果使用web.SiteGroups[i].Users["域用户帐号"] 时,却会报错。这个时候该怎么弄呢?
 我的解决方案是通过模拟用户来操作,代码如下:

 1
 
///
 
<summary>


 2

        

///
 获取用户所属于的组

 3

        

///
 
</summary>


 4

        

///
 
<param name="user"></param>


 5

        

///
 
<returns></returns>


 6
        
private
 IList
<
SPGroup
>
 GetUserExistInGroups(SPUser user)

 7

        

{

 8

            

try


 9

            

{

10

                IList

<
SPGroup
>
 tempGroups 
=
 
new
 List
<
SPGroup
>
();

11


12

                SPSecurity.RunWithElevatedPrivileges(

delegate
()

13

                

{

14

                    SPUser tempUser 

=
 web.AllUsers[user.LoginName];

15

                    

using
 (SPSite tempSite 
=
 
new
 SPSite(site.Url, tempUser.UserToken))

16

                    

{

17

                        

using
 (SPWeb tempWeb 
=
 tempSite.OpenWeb())

18

                        

{

19

                            SPGroupCollection groups 

=
 tempWeb.SiteGroups;

20

                            

foreach
 (SPGroup group 
in
 groups)

21

                            

{

22

                                

if
 (group.ContainsCurrentUser)

23

                                

{

24

                                    tempGroups.Add(group);

25

                                }




26

                            }




27


28

                        }




29

                    }




30

                }


);

31

                

return
 tempGroups;

32

            }




33

            

catch


34

            

{

35

                

return
 
null
;

36

            }




37

        }

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

另一个Hashtable缓存,在不方便使用System.Web.Caching.Cache或HttpRuntime.Cache,或HttpContext.Current.Cache的时候,可以试试,代码如下:

 1
///
 
<summary>


 2

    

///
 哈希表缓存

 3

    

///
 作者:徐志泽

 4

    

///
 创建日期:2008-12-26

 5

    

///
 
</summary>


 6
    
public
 
class
 HashCacheTool

 7

    

{

 8

        

private
 
static
 Hashtable parmCache 
=
 Hashtable.Synchronized(
new
 Hashtable());

 9


10

        

///
 
<summary>


11

        

///
 保存缓存值

12

        

///
 
</summary>


13

        

///
 
<param name="cacheKey"></param>


14

        

///
 
<param name="cmdParms"></param>


15
        
public
 
static
 
void
 SetCache(
string
 cacheKey, 
object
 cacheValue)

16

        

{

17

            parmCache[cacheKey] 

=
 cacheValue;

18

        }




19

        

///
 
<summary>


20

        

///
 获取缓存值 

21

        

///
 
</summary>


22

        

///
 
<param name="cacheKey"></param>


23

        

///
 
<returns></returns>


24
        
public
 
static
 
object
 GetCached(
string
 cacheKey)

25

        

{

26

            

return
 parmCache[cacheKey];

27

        }




28

        

///
 
<summary>


29

        

///
 清空所有

30

        

///
 
</summary>


31
        
public
 
static
 
void
 Clear()

32

        

{

33

            

if
 (parmCache.Count 
<=
 
0
)

34

                

return
;

35

            parmCache.Clear();

36

        }




37

        

///
 
<summary>


38

        

///
 判断关键字是否存在

39

        

///
 
</summary>


40

        

///
 
<param name="key"></param>


41

        

///
 
<returns></returns>


42
        
public
 
static
 
bool
 IsKeyExist(
string
 cacheKey)

43

        

{

44

            

if
 (parmCache.Count 
<=
 
0
)

45

                

return
 
false
;

46

            

return
 parmCache.ContainsKey(cacheKey);

47

        }




48

        

///
 
<summary>


49

        

///
 如果关键字不存在的话,则清空所有

50

        

///
 
</summary>


51

        

///
 
<param name="key"></param>


52
        
public
 
static
 
void
 ClearWhileKeyNotExist(
string
 cacheKey)

53

        

{

54

            

if
 (parmCache.Count 
<=
 
0
)

55

                

return
;

56

            

if
 (IsKeyExist(cacheKey))

57

                

return
;

58

            

else


59

                Clear();

60

        }




61

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


抱歉!评论已关闭.