权限提升

什么时候适合使用权限提升;一般的用户如游客(everyone),我们会赋予的read的权限,read的权限可能能够查看sitecontent,但是不能操作。如要涉及到操作可能就需要使用到权限替身了。此外,如果该网站如果读的是跟网站的sitecontent,可能游客也没有权限查看到,这时候也需要使用到权限提升。当然,究竟什么时候使用权限替身,最好的办法就是用目标用户的身份登录,看能否查看该页面,若不能,则去提升其权限。

            SPSecurity.RunWithElevatedPrivileges(delegate()//虚拟管理员,否则其他用户访问没有权限
            {
                using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb myweb = site.OpenWeb())
                    {
                        SPList list = myweb.Lists[ListNames.BBSScoreManage];
                        SPQuery spquery = new SPQuery();
                        string querystring = string.Format(@"
                                                          <Where>
                                                              <Eq>
                                                                 <FieldRef Name='Title' />
                                                                 <Value Type='Text'>{0}</Value>
                                                              </Eq>
                                                           </Where>
                                                        ", userName);
                        SPListItemCollection items = list.GetItems(spquery);
                        SPListItem item = items[0];
                        if (items.Count > 0 && Convert.ToInt32(item["Score"]) > score)
                        {
                            int resultScore = Convert.ToInt32(item["Score"]) + score;
                            item["Score"] = resultScore;
                            item.Update();
                        }
                    }
                }
            });

 

posted on 2013-12-14 17:28  木子叶  阅读(387)  评论(0编辑  收藏  举报

导航