Skip to content
This repository was archived by the owner on Nov 1, 2018. It is now read-only.
This repository was archived by the owner on Nov 1, 2018. It is now read-only.

Question: Windows Authentication + Delegation #75

Closed

Description

@jruckert

Hi All,

Traditionally when we want to setup impersonation/delegation we would usually setup the apppool credentials with a SPN (associated with the http/) + turn on impersonation under the web.config file.

In the new world, we have done the same regarding the SPN and http/ (NOTE: This url is different from the Kestrel hosting.json server.url)

We have tried every permutation under the sun (apart from the right one that is!) to try to get a simple IIS Windows Auth vNext Website + HttpPlatformHandler to delegate through the windows credentials to SQL, it always uses the app pool. (We have modified the web.config file to pass through the windows auth token as per documentation under this site)

Note: When we render out on the MVC Webpage the IHttpContextAccessor -> Context -> Identity -> Username, this is showing the correct authenticated username, not the app pool.

My theory to make this work is as follows:

If someone can please verify if this is correct, I would really appreciate it.

Dnx.exe (which is the one connecting to the DB, doing the web requests etc), is fundamentally proxied through IIS (+ the HttpPlatformHandler) to localhost:5002 at the moment. The w3wp.exe process is actually redundant in the security side of things.

localhost:5002 does not have any SPN’s etc for this service account etc.

If we update the hosting.json file (“server.urls”) to be say http://secure-dotnet.mywebserver.com:5002 and set the SPN to this (e.g. http/secure-dotnet.mywebserver.com:5002)

Will we have a fighting chance?

Activity

  1. Tratcher commented on Jan 22, 2016

    @Tratcher
    Member

    /cc: @blowdart

    Your analysis is mostly accurate. Let me fill in some blanks.

    The authentication is happening in IIS so you do need the SPN configured there. However the impersonation there is irrelevant as we are running out of process. The identity is passed to the back-end process through a special handshake, and used to populate HttpContext.User. However there is no automatic or configurable impersonation of the user. You would need to explicitly call WindowsIdentity.Impersonate() before performing delegated operations.

    Side note: When running in IIS/Express you should not explicitly configure server.urls for the back-end process. HttpPlatformHandler tells us a random port to listen on.

  2. jruckert commented on Jan 22, 2016

    @jruckert
    Author

    Thanks for your guidance, this clarifies a lot of my thinking, so in theory though if I wanted all operations to be delegated, would a valid approach be to create some middleware to perform this impersonation operation for all requests? (Thinking about this as I write this, I'm thinking its not valid as we are interested in outbound impersonation to another service, not the inbound request?)

    I suppose we are all complacent with the old school method of the one liner

    <identity impersonate="true"/>
    

    into the web.config without having to write any additional code.

    Would the middleware approach (if its valid) have the potential to lead to cross contamination of impersonation requests (e.g. one thread having the wrong impersonation credentials?)

    If the middleware approach is not valid, then this would mean we would have to identify any services such as SQL and wrap each call (EF7 Create/Update etc), WCF/Web Services outbound calls with this impersonate request (quite a lot of code to wrap).

    Most enterprises apps would like understand this scenario from my experience (our team is trying to develop one as we speak which helps!) which requires pass through authentication to a myriad of services (SQL, web services etc).

    Thank you for your thoughts!

  3. Tratcher commented on Jan 22, 2016

    @Tratcher
    Member

    @blowdart, I'll defer to you on this one.

  4. blowdart commented on Jan 22, 2016

    @blowdart
    Member

    Assuming the identity flows async and to background tasks that would work, however it may not be what you want to be honest. Error handling would now run elevated and that's usually not a good idea, especially if threads abort and things hang around for a while.

    So if I were recommending an approach it would be to wrap only the code that requires it in an impersonation call, and ensure that you catch errors and de-elevate properly no matter what Usually this involves writing a class that implements IDisposable and using using to wrap your code.

  5. jruckert commented on Jan 22, 2016

    @jruckert
    Author

    Thanks!

  6. jruckert commented on Jan 22, 2016

    @jruckert
    Author

    Sorry last question, will we need a SPN for the dnx.exe process for the specific port that it is listening on?

  7. Tratcher commented on Jan 23, 2016

    @Tratcher
    Member

    No, there is no authentication being processed in the dnx.exe, that all happens in IIS and is handed off to us.

  8. jruckert commented on Jan 23, 2016

    @jruckert
    Author

    I've done everything that has been recommended so far, and still not delegation of rights.

    I'm running under both Anonymous and Windows Auth (due to a CORS being a requirement).

    I'll attempt to send a stack trace/wireshark etc, as this is not working.

  9. added this to the 1.0.0-rc2 milestone on Feb 2, 2016
  10. muratg commented on Feb 2, 2016

    @muratg
    Contributor

    @pakrym Could you attempt a repro for this issue?

  11. modified the milestones: 1.0.0-rc2, 1.0.0 on Mar 30, 2016
  12. modified the milestones: Discussions, 1.0.0 on May 19, 2016
  13. 10 remaining items

  14. added and removed on Jul 6, 2016
  15. Tratcher commented on Jul 7, 2016

    @Tratcher
    Member

    ImpersonationLevel and AuthenticationType work if you impersonate the user first.

    There may not be an issue with ANCM, I'm not able to get delegation working for my Asp.Net 4 site on the same server.

  16. Tratcher commented on Jul 8, 2016

    @Tratcher
    Member

    I've got it working using Asp.Net Core on .NET 4.6. Note there's a known bug with RunImpersonated in 4.6 (https://github.com/dotnet/cli/issues/1365#issuecomment-184822817), you need to use Impersonate instead.

    I'm still trying to track down the issue with .Net Core. Not sure if it's an issue with WindowsIdentity or HttpClient.

  17. Tratcher commented on Jul 8, 2016

    @Tratcher
    Member

    Theory: WinHttpHandler (HttpClient) on .NET Core is not honoring the impersonated WindowsIdentity.

  18. Tratcher commented on Jul 11, 2016

    @Tratcher
    Member
  19. Tratcher commented on Oct 14, 2016

    @Tratcher
    Member

    Closing this as it appears to be an external issue with WindowsIdentity or WinHttpHandler. See dotnet/corefx#9996. We can revisit it if they conclude otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions