Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:03:45+00:00 2026-05-31T10:03:45+00:00

Impersonation example I can check is user domain administrator with next lines of code:

  • 0

Impersonation example

I can check is user domain administrator with next lines of code:

using (Impersonation im = new Impersonation(UserName, Domain, Password))
{
    System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
    bool isDomainAdmin = identity.IsDomainAdmin(Domain, UserName, Password);
    if (!isDomainAdmin)
    {
        //deny access, for example
    }
}

where IsDomainAdmin – is extension method

public static bool IsDomainAdmin(this WindowsIdentity identity, string domain, string userName, string password)
{
    Domain d = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, domain, userName, password));

    using (DirectoryEntry de = d.GetDirectoryEntry())
    {
        byte[] domainSIdArray = (byte[])de.Properties["objectSid"].Value;
        SecurityIdentifier domainSId = new SecurityIdentifier(domainSIdArray, 0);
        SecurityIdentifier domainAdminsSId = new SecurityIdentifier(WellKnownSidType.AccountDomainAdminsSid, domainSId);
        WindowsPrincipal wp = new WindowsPrincipal(identity);
        return wp.IsInRole(domainAdminsSId);
    }
}

But, when method IsDomainAdmin is called, it is trying to write some files to the %LOCALAPPDATA% for impersonated user, and if program is runnig not as administrator, it throws an exception

Could not load file or assembly ‘System.DirectoryServices,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or
one of its dependencies. Either a required impersonation level was not
provided, or the provided impersonation level is invalid. (Exception
from HRESULT: 0x80070542)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:03:47+00:00Added an answer on May 31, 2026 at 10:03 am

    You certainly don’t need a user’s password to verify if the user is a member of a group. So why don’t you just query AD in a straight-forward manner using DirectoryEntry or DirectorySearcher? If you also need to verify that the password supplied is correct you can do that in an additional step using PrincipalContext.ValidateCredentials. (See PrincipalContext.ValidateCredentials Method (String, String)).

    static void Main(string[] args) {
        string userDomain = "somedomain";
        string userName = "username";
        string password = "apassword";
    
        if (IsDomainAdmin(userDomain, userName)) {
            string fullUserName = userDomain + @"\" + userName;
            PrincipalContext context = new PrincipalContext(
                ContextType.Domain, userDomain);
            if (context.ValidateCredentials(fullUserName, password)) {
                Console.WriteLine("Success!");
            }
        }
    }
    
    public static bool IsDomainAdmin(string domain, string userName) {
        string adminDn = GetAdminDn(domain);
        SearchResult result = (new DirectorySearcher(
            new DirectoryEntry("LDAP://" + domain),
            "(&(objectCategory=user)(samAccountName=" + userName + "))",
            new[] { "memberOf" })).FindOne();
        return result.Properties["memberOf"].Contains(adminDn);
    }
    
    public static string GetAdminDn(string domain) {
        return (string)(new DirectorySearcher(
            new DirectoryEntry("LDAP://" + domain),
            "(&(objectCategory=group)(cn=Domain Admins))")
            .FindOne().Properties["distinguishedname"][0]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a service using WCF and Impersonation. A user can log in to
In my app's config there is a impersonation tag containing username and password and
With ASP.NET impersonation, can one use Environment.UserName to determine if impersonation is working? That
I have found some sample code on codeproject that allows for user impersonation. This
In an impersonation scenario related to Sharepoint I need to execute some code in
I am using windows authentication without impersonation on my company's intranet website with IIS7.
I want to wrap a piece of code that uses the Windows Impersonation API
I am using impersonation is used to access file on UNC share as below.
I used the common impersonation code and it worked just fine, until I inserted
We are using impersonation by using the LogonUser function from advapi32.dll along with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.