현재 사용자가 관리자인지 확인
내 응용 프로그램에서 일부 스크립트를 실행해야하고 스크립트를 실행하는 사용자가 관리자인지 확인해야합니다. C #을 사용하여이 작업을 수행하는 가장 좋은 방법은 무엇입니까?
using System.Security.Principal;
public static bool IsAdministrator()
{
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
return new WindowsPrincipal(WindowsIdentity.GetCurrent())
.IsInRole(WindowsBuiltInRole.Administrator);
IsInRole에 대한 위의 답변 은 실제로 정확합니다. 현재 사용자에게 관리자 권한이 있는지 확인합니다. 하나,
Windows Vista부터 UAC (사용자 계정 컨트롤)는 사용자의 권한을 결정합니다. 기본 제공 관리자 그룹의 구성원 인 경우 표준 사용자 액세스 토큰과 관리자 액세스 토큰의 두 가지 런타임 액세스 토큰이 할당됩니다. 기본적으로 표준 사용자 역할에 있습니다.
(MSDN, 예 : https://msdn.microsoft.com/en-us/library/system.diagnostics.eventlogpermission(v=vs.110).aspx )
따라서 IsInRole 은 기본적으로 사용자 권한을 고려하므로 메서드는 false를 반환합니다. 소프트웨어가 관리자 권한으로 명시 적으로 실행되는 경우에만 해당됩니다.
https://ayende.com/blog/158401/are-you-an-administrator 에서 AD를 확인하는 다른 방법 은 사용자 이름이 관리자 그룹에 있는지 확인합니다.
두 가지를 결합하는 완전한 방법은 다음과 같습니다.
public static bool IsCurrentUserAdmin(bool checkCurrentRole = true)
{
bool isElevated = false;
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
if (checkCurrentRole)
{
// Even if the user is defined in the Admin group, UAC defines 2 roles: one user and one admin.
// IsInRole consider the current default role as user, thus will return false!
// Will consider the admin role only if the app is explicitly run as admin!
WindowsPrincipal principal = new WindowsPrincipal(identity);
isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
else
{
// read all roles for the current identity name, asking ActiveDirectory
isElevated = IsAdministratorNoCache(identity.Name);
}
}
return isElevated;
}
/// <summary>
/// Determines whether the specified user is an administrator.
/// </summary>
/// <param name="username">The user name.</param>
/// <returns>
/// <c>true</c> if the specified user is an administrator; otherwise, <c>false</c>.
/// </returns>
/// <seealso href="https://ayende.com/blog/158401/are-you-an-administrator"/>
private static bool IsAdministratorNoCache(string username)
{
PrincipalContext ctx;
try
{
Domain.GetComputerDomain();
try
{
ctx = new PrincipalContext(ContextType.Domain);
}
catch (PrincipalServerDownException)
{
// can't access domain, check local machine instead
ctx = new PrincipalContext(ContextType.Machine);
}
}
catch (ActiveDirectoryObjectNotFoundException)
{
// not in a domain
ctx = new PrincipalContext(ContextType.Machine);
}
var up = UserPrincipal.FindByIdentity(ctx, username);
if (up != null)
{
PrincipalSearchResult<Principal> authGroups = up.GetAuthorizationGroups();
return authGroups.Any(principal =>
principal.Sid.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid) ||
principal.Sid.IsWellKnown(WellKnownSidType.AccountDomainAdminsSid) ||
principal.Sid.IsWellKnown(WellKnownSidType.AccountAdministratorSid) ||
principal.Sid.IsWellKnown(WellKnownSidType.AccountEnterpriseAdminsSid));
}
return false;
}
상승 된 권한 (UAC 사용)이없는 관리자 그룹의 사용자에 대해이 메서드 IsCurrentUserAdmin ()은! checkCurrentRole을 반환합니다. checkCurrentRole == false이면 true, checkCurrentRole == true이면 false입니다.
관리자 권한이 필요한 코드를 실행하는 경우 checkCurrentRole == true를 고려하십시오. 그렇지 않으면 그때까지 보안 예외가 발생합니다. 따라서 올바른 IsInRole 논리입니다.
이를 위해 Windows API를 호출 할 수도 있습니다.
[DllImport("shell32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsUserAnAdmin();
더 일반적으로 사용자가 상승 된 권한으로 실행되고 있는지 여부를 알려줍니다.
다른 솔루션을 추가 할 것이라고 생각했습니다. IsInRole
항상 작동 하는 것은 아닙니다.
- 사용자가 현재 세션에서 지정된 Windows 사용자 그룹의 구성원이 아닌 경우.
- 관리자가 그룹 정책 설정을 변경했습니다.
- 역할 매개 변수는 '대소 문자 구분'방법으로 처리됩니다.
- XP 컴퓨터에 .NET Framework 버전이 설치되어 있지 않으면 작동하지 않습니다.
필요에 따라 이전 시스템을 지원해야하는 경우; 또는 클라이언트가 시스템을 물리적으로 어떻게 관리하고 있는지 확실하지 않습니다. 이것은 내가 구현 한 솔루션입니다. 유연성과 변경을 위해.
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.
So the above code has a few constructs; it will actually test to see if the User is on Vista or higher. That way if a customer is on XP without a framework or beta framework from years ago it will allow you to alter what you'd like to do.
Then it will physically test to avoid a null value for the account.
Then last of all it will provide the check to verify that the user is indeed in the proper role.
I know the question has been answered; but I thought my solution would be a great addition to the page for anyone else whom is searching Stack. My reasoning behind the Protected Constructor would allow you to use this class as a Derived Class that you could control the state of when the class is instantiated.
I must be sure that the user running them is an administrator
If your application must be run with admin rights, it would be right to update its manifest.
Set requestedExecutionlevel
to requireAdminstrator
.
참고URL : https://stackoverflow.com/questions/3600322/check-if-the-current-user-is-administrator
'IT박스' 카테고리의 다른 글
TestFlight 용 iTunes Connect에 빌드를 어떻게 업로드합니까? (0) | 2020.10.24 |
---|---|
Mercurial "서버" (0) | 2020.10.24 |
JQuery / JavaScript로 링크 할 메일 호출 / 클릭 (0) | 2020.10.24 |
추가 된 여러 항목이 동일한 기본 키를 가질 수 있습니다. (0) | 2020.10.24 |
Firefox에 의해 표시되지 않는 파비콘 (0) | 2020.10.24 |