我相信这个问题已经被问过很多次了,但是我遇到了一个问题。所以我创建了一个单独的类,专门用于验证是否存在正确的用户级别。
以下是测试权限级别的代码:
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
}
但是当我调用这个新类时,方法会显示:“由于构造函数权限而导致无效访问”。理论上应该可以工作;我是否漏掉了什么?任何帮助将不胜感激。
Elevate()
被调用两次了吗? - Bobson