Link to home
Create AccountLog in
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

Avatar of Sean Bornstein
Sean Bornstein

.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

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Chinmay PatelChinmay Patel🇮🇳

Hi Sean,


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


Regards,

Chinmay.


Avatar of Sean BornsteinSean Bornstein

ASKER

No that did not work but thanks for the idea.

This question needs an answer!
Looks like this question is still being worked on. Think you can help?
Create your account and start contributing!
Create Account

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.