Link to home
Start Free TrialLog in
Avatar of Sean Bornstein
Sean Bornstein

asked on

.Net Core - Impersonation Help!

Hello EE Community,

I have been struggling trying to figure out how the heck to impersonate via .Net Core API  sending a HttpWebRequest as the Windows Identity user.   I have tried forum articles out there and posting what I tried last but this does not work.   It works fine when I run via IIS Express I have Windows Authentication and Anonymous on that I need for now.   When I run via IIS Express the "DefaultCredentials" are picking up my credentials but when I run deployed to IIS the Application Pool identity is using a service account which I understand but now I need to impersonate and with Core it seems so difficult.   This is what I am trying when deploying to IIS but it is not working and still picking up the app pool identity service account versus my Windows Identity essentially.   The Windows Identity has myself correct but the DefaultCredentials does not it uses the app pool.   I tried this via forum articles still no dice the CredentialCache.DefaultCredentials is still the app pool.  Needing help understanding how to handle this in Core.

  WindowsIdentity identity = (WindowsIdentity)_httpContextAccessor.HttpContext.User.Identity;
                WindowsIdentity.RunImpersonated(identity.AccessToken, () =>
                {
                    Uri theUri = new Uri(string.Concat(this.session.HostURL, "/refi/services"));
                    HttpWebRequest theHttpWebRequest = (HttpWebRequest)WebRequest.Create(theUri);
                    theHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
                    theHttpWebRequest.Timeout = 210000;
                    theHttpWebRequest.CookieContainer = session.CookieContainer;
                    HttpWebResponse theHttpWebResponse = (HttpWebResponse)theHttpWebRequest.GetResponse();
                    theHttpWebResponse.Close();
                });

Open in new window

Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Hi Sean,


I am doing some guess work here but did you try using DefaultNetworkCredentials instead of DefaultCredentials?


Regards,

Chinmay.

Avatar of Sean Bornstein
Sean Bornstein

ASKER

No that did not work but thanks for the idea.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.