English 中文(简体)
无法访问 Web 应用程序属性
原标题:No permission to access webApplication properties

当激活网站功能时, 我自动要设置 WebApplication 属性。 这是代码 :

   public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {             
                SPWeb currentWeb = ContentTypes.ValidateFeatureActivation(properties);
                using (SPSite site = new SPSite(currentWeb.Site.Url))
                {
                    SPWebApplication currentApplication = site.WebApplication;
                    if (currentApplication.MaxQueryLookupFields < 20)
                    {

                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            try
                            {
                                currentApplication.MaxQueryLookupFields = 20;
                            }

                            catch (System.Security.SecurityException ex)
                            {
                                _log.ErrorFormat("no permission");
                            }
                        });

                    }
                }                
        }

即使我有一个农场管理员激活了这个功能, 安全例外也会被抛出(“拒绝进入” ) 。

currentApplication.MaxQueryLookupFields = 20;

AFAIK SP Security. Run with the Leveled Privitees 运行作为一个网站管理者, 农场管理者是“ 强势的” / 强势的。 但如何做到这一点呢? (WITHOUT run On with the leveled Privites I gets 相同例外 ) 。

问题回答

您需要在 SPS Security. Run with levelated Privictees 内创建新的 SPSite、 SPWeb 和 SPWebApplication 对象。 否则您将使用与当前用户相同的权限运行它们。 例如 。

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
        SPSecurity.RunWithElevatedPrivileges(delegate()
             {             
                SPWeb currentWeb = ContentTypes.ValidateFeatureActivation(properties);
                using (SPSite site = new SPSite(currentWeb.Site.Url))
                {
                    SPWebApplication currentApplication = site.WebApplication;
                    if (currentApplication.MaxQueryLookupFields < 20)
                    {
                            try
                            {
                                currentApplication.MaxQueryLookupFields = 20;
                            }

                            catch (System.Security.SecurityException ex)
                            {
                                _log.ErrorFormat("no permission");
                            }


                    }
                }
            });                
        }

您应该立即在 RWEP 中安装另一个SPSite 对象, 以获得应用程序集合身份背景, 因为第一个在RWEP 区块外创建的SPSite 是用当前SPSite 用户环境创建的。 请尝试这个 :

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWeb currentWeb = ContentTypes.ValidateFeatureActivation(properties);
        using (SPSite site = new SPSite(currentWeb.Site.Url))
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite _site = new SPSite(site.ID))
                {
                    SPWebApplication currentApplication = _site.WebApplication;
                    if (currentApplication.MaxQueryLookupFields < 20)
                    {
                        try
                        {
                            currentApplication.MaxQueryLookupFields = 20;
                        }
                        catch (System.Security.SecurityException ex)
                        {
                            _log.ErrorFormat("no permission");
                        }
                    }
                }
            });
        }
    }




相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签