Jason

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

判断当前用户是否属于网站管理员(或者其他角色)

判断当前用户的代码很简单:
1SPSite site = SPControl.GetContextSite(this.Context);
2site.AllowUnsafeUpdates = true;
3SPWeb web = site.RootWeb;
4SPUser user = web.CurrentUser;
但是有个问题:普通用户是没有权限取到web.CurrentUser的,如果用Impersonate()取到的则是提升的管理员的用户名。所以当前用户只能用WindowsIdentity user= WindowsIdentity.GetCurrent()来得到。
完整代码:
 1public static bool IsSiteAdmin(HttpContext context,string siteName)
 2        {
 3            WindowsImpersonationContext wic = null;
 4            bool isAdmin = false;
 5            try
 6            {
 7                WindowsIdentity old = WindowsIdentity.GetCurrent();
 8                //提升管理员权限
 9                wic = CreateIdentity("administrator""iswind""xxxxxxx").Impersonate();
10                SPSite site = SPControl.GetContextSite(context);
11                site.AllowUnsafeUpdates = true;
12                SPWeb web = site.OpenWeb();
13                SPUser user = web.AllUsers[old.Name];
14                if (user.Roles.Count > 0)
15                {
16                    foreach (SPRole role in user.Roles)
17                    {
18                        if (role.Name == "管理员")
19                            isAdmin = true;
20                    }

21                }

22                else
23                {
24                    isAdmin = false;
25                }

26                return isAdmin;
27            }

28            catch (Exception ex)
29            {
30                throw ex;
31            }

32            finally
33            {
34                wic.Undo();
35            }
    
36        }

37
38        protected static WindowsIdentity CreateIdentity(string User, string Domain, string Password) 
39        {
40            // The Windows NT user token. 
41            IntPtr tokenHandle = new IntPtr(0); 
42            const int LOGON32_PROVIDER_DEFAULT = 0
43            const int LOGON32_LOGON_NETWORK = 3
44
45            // Initialize token object
46            tokenHandle = IntPtr.Zero; 
47
48            // Call LogonUser to obtain a handle to an access token. 
49            bool returnValue = LogonUser(User, Domain, Password, 
50              LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, 
51                ref tokenHandle); 
52
53            // Check for failure
54            if (false == returnValue) 
55            {
56                int ret = Marshal.GetLastWin32Error(); 
57                throw new Exception("LogonUser failed with error code: " + ret); 
58            }
 
59
60            System.Diagnostics.Debug.WriteLine("Created user token: " + tokenHandle); 
61            //The WindowsIdentity class makes a new copy of the token. 
62            //It also handles calling CloseHandle for the copy. 
63            WindowsIdentity id = new WindowsIdentity(tokenHandle); 
64            CloseHandle(tokenHandle); 
65            return id; 
66        }
 
67
68        [DllImport("advapi32.dll", SetLastError=true)] 
69        private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, 
70            int dwLogonType, int dwLogonProvider, ref IntPtr phToken); 
71
72        [DllImport("kernel32.dll", CharSet=CharSet.Auto)] 
73        private extern static bool CloseHandle(IntPtr handle);
注意:获取Roles信息是需要管理员权限的,如果不想用提升权限的方法,可以考虑使用DoesUserHavePermission的方法,通过一些特定权限进行判断。DoesUserHavePermission是不需要管理员权限运行的,SharePoint的layouts下面的一些页面里对当前用户的区分也是使用的这个方法。


posted on 2005-12-31 13:21  Jsang  阅读(747)  评论(0)    收藏  举报