我敢肯定这个问题已经被问过很多次了;但是我遇到了问题。所以我创建了一个单独的类;专门用于验证是否存在正确的用户级别。
下面是测试这些权限级别的代码:
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.
}
因此该部分按预期工作;但是,当我将此类继承到另一个类以利用 protected 构造函数时,我遇到了麻烦。
class Default_Configuration : Elevated_Rights
{
#region Constructor:
public Default_Configuration() : base()
{
Elevate();
}
#endregion
}
但是当我调用那个新类时;该方法指出:“由于构造函数许可而导致访问无效”。理论上应该可行;有什么我想念的吗?任何帮助将不胜感激。
最佳答案
我认为您的问题出在其他地方:我将这两个类定义粘贴到一个项目中,构建得很好。实例化了一个新的 Default_Configuration
,调用了 Elevate()
,没有错误。
如果您对混合使用 public
和 protected
方法有其他问题,这里有一个 blog post by Peter Hallam描述问题。
基本上,您不能从派生类中的public
实例方法调用基类中的protected
实例方法;这样做将允许“恶意”派生类通过为它们编写包装器来使基类 public
中的所有 protected
方法成为可能。
关于c# - protected 构造函数派生类问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13960017/