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 7680721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:10:49+00:00 2026-05-31T18:10:49+00:00

My setup is a little complicated so let me go over that first. We

  • 0

My setup is a little complicated so let me go over that first.

We have a WCF web service that gets data from various sources through several different API’s and returns that data to the client. The requested security is that it be done over HTTPS(working)
The IIS standard is that the app pool must be set to use the basic IIS network service account and that .net impersonation should be used.

My problem is that the web service should always run under an AD process ID regardless of who calls it, but that it should also check what AD groups the caller is in to determine what functions are accessible. Now I can setup my web.config to use and this kind of works for making it always run as Blah but then I don’t know how to also make it impersonate/check the calling user to see what functions they have access too.

**edit: forgot to mention the calling client should be able to pass a UN/PASS instead of just its windows token. The wcf service should verfiy its a valid AD UN and PASS as well as poll what groups it in.

  • 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-31T18:10:50+00:00Added an answer on May 31, 2026 at 6:10 pm

    It sounds like you want HostingEnvironment.Impersonate

    Example:

    using (var imp = HostingEnvironment.Impersonate())
    {
        // code now executes in the context of the authenticated user, 
        // rather than the service account
    }
    

    That works fantastically, unfortunately the standard here is to not use app pools as password management is easier for them if its on each team to keep them up to date by putting it in the web.config

    Well, that seems counter-intuitive, but I’ve run into worse policies in my day, so I’m hardly one to judge. 😉

    As I mentioned in my comment, there are overloads of Impersonate that will allow you to impersonate an arbitrary account. In order to do this, you must obtain the windows identity token for that user, and this is non-trivial, and not, to my knowledge, something that you can do 100% in managed code. You’ll have to use unmanaged code, and you’ll have to know the username and password of the impersonated account within your application. This is far less secure than simply setting the account as the App Pool ID, if you ever want to argue the point with your network architect, BTW. Just some ammo for ya.

    Anyway, here’s some example code I adapted from the internets:

    #region native imports. 
    public const int Logon_LogonTypeInteractive = 2;
    public const int Logon_ProviderDefault = 0;
    public const int Duplicate_ImpersonationLevelImpersonate = 2;
    
    [DllImport("advapi32.dll")]
    public static extern bool LogonUser(string lpszUserName, string lpszDomain, string lpszPassword,
        int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
    [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
    public static extern bool DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
    
    [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
    public static extern bool RevertToSelf();
    
    [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
    public static extern  bool CloseHandle(IntPtr handle);
    #endregion native imports. 
    
    #region elsewhere...
    public IntPtr GetWindowsTokenForImpersonation(string username, string password, string domain)
    {
        IntPtr loginToken = IntPtr.Zero;
        IntPtr workingToken = IntPtr.Zero;
        bool success
        if(!RevertToSelf()) 
        {
            return IntPtr.Zero;
            // failed to eliminate any existing impersonations. This block may not be necessary depending on your code
        } 
        if(!LogonUserA(username, domain, password, Logon_LogonTypeInteractive, Logon_ProviderDefault, ref loginToken))
        {
            return IntPtr.Zero;
            // failed to log in the user
        }
    
        if(!DuplicateToken(loginToken, Duplicate_ImpersonationLevelImpersonate, ref workingToken)
        {
            if(loginToken != IntPtr.Zero)
            {
                CloseHandle(loginToken);
            }
            return IntPtr.Zero;
            // failed to get a working impersonation token
        }
    
        CloseHandle(loginToken);
        return workingToken; // NOTE: You must dispose this token manually using CloseHandle after impersonation is complete. 
    }
    #endregion elsewhere
    
    #region where you want to impersonate
    
    var token = GetWindowsTokenForImpersonation(username, password, domain);
    if(token != IntPtr.Zero)
    {
        using(var imp = HostingEnvironment.Impersonate(token))
        {
            // code here executes under impersonation
        }
        CloseHandle(token);
    }
    #endregion
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some XML data retrieved from a web service that I use to
I have this nice little MSBuild-based daily build setup that I use on my
I'm working on a web app that reads data from a set of text
Good day, i have following setup for my little service: -module(mrtask_net). -export([start/0, stop/0, listen/1]).
I have a form data from an iphone app that needs to send HTTP
Setup: I have a COM DLL that calls a method inside a managed C#
Setup : I have a Struts web application where I use displaytag elements to
Using iOS and a UISearchBar you can have a nice little cancel button setup
This will require a little setup. Trust me that this is for a good
Little background. I have a navigation setup for when you click on a certain

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.