【发布时间】:2012-12-07 06:08:15
【问题描述】:
我相信这个问题已经被问过很多次了;但是我遇到了问题。所以我创建了一个单独的类;专门用于验证是否存在正确的用户级别。
以下是测试这些权限级别的代码:
class Elevated_Rights
{
// Token Bool:
private bool _level = false;
#region Constructor:
protected Elevated_Rights()
{
// Invoke Method On Creation:
Elevate();
}
#endregion
public void Elevate()
{
// Get Identity:
WindowsIdentity user = WindowsIdentity.GetCurrent();
// Set Principal
WindowsPrincipal role = new WindowsPrincipal(user);
#region Test Operating System for UAC:
if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6)
{
// False:
_level = false;
// Todo: Exception/ Exception Log
}
#endregion
else
{
#region Test Identity Not Null:
if (user == null)
{
// False:
_level = false;
// Todo: "Exception Log / Exception"
}
#endregion
else
{
#region Ensure Security Role:
if (!(role.IsInRole(WindowsBuiltInRole.Administrator)))
{
// False:
_level = false;
// Todo: "Exception Log / Exception"
}
else
{
// True:
_level = true;
}
#endregion
} // Nested Else 'Close'
} // Initial Else 'Close'
} // End of Class.
}
因此该部分按预期工作;但是,当我将这个类继承到另一个类中以利用受保护的构造函数时,我遇到了障碍。
class Default_Configuration : Elevated_Rights
{
#region Constructor:
public Default_Configuration() : base()
{
Elevate();
}
#endregion
}
但是当我调用那个新类时;该方法声明:“由于构造函数权限而导致无效访问”。它理论上应该有效;有什么我想念的吗?任何帮助将不胜感激。
【问题讨论】:
-
“方法状态......”?这听起来像是一个运行时问题,而不是编译器问题......您使用的是 Lazy
还是类似在运行时构造实例的东西? -
你知道你要给
Elevate()打两次电话吗? -
能贴出调用代码吗?
-
虽然我没有意识到这个错误,但类(嵌套类除外)默认为
internal,因此除非您在同一个程序集中使用该类,否则公共构造函数毫无意义。跨度> -
另外请仔细检查您的错误消息,除了这个问题之外,谷歌中没有出现“由于构造函数权限而导致无效访问”的短语,所以我怀疑您没有准确地复制和粘贴它。
标签: c# class methods constructor protected