I have an ASP.NET 5 MVC 6 Web API project. Most of the API endpoints have the [Authorize] attribute, and Windows Authentication is enabled in both IIS and on the properties of the project in Visual Studio. This all works fine in beta 7.
In beta 8, however, this does not work. It's easy to reproduce this with a completely clean project:
Result:
An error occurred attempting to determine the process id of the DNX process hosting your application.
Result:
The project launches this time, but the web API returns a 500. Notice in the Output window:
Microsoft.AspNet.Mvc.Controllers.ControllerActionInvoker: Warning: Authorization failed for the request at filter 'Microsoft.AspNet.Mvc.Filters.AuthorizeFilter'.
The project also does not work when published to IIS.
As noted in the beta 8 announcement, the hosting model has changed such that IIS is now passing the request through to Kestrel. The Servers page doesn't give any indication that Kestrel supports Windows Authentication. Is there some trick to getting Windows Authentication working in beta 8?
a) To create a web api project in windows authentication mode, follow below steps: After choosing ASP.Net Web Application, select Web API template and from the right side click Change Authentication button and select Windows Authentication.
Windows-based authentication is manipulated between the Windows server and the client machine. The ASP.NET applications resides in Internet Information Server (IIS). Any user's web request goes directly to the IIS server and it provides the authentication process in a Windows-based authentication model.
This seems to be a known bug in the Visual Studio debugging tooling when using IIS Express. Until that is fixed, the only workaround I've found is to debug by running through WebListener instead of IIS Express. To set this up, in your Configure method in Startup.cs add:
// If we're self-hosting, enable integrated authentication (if we're using
// IIS, this will be done at the IIS configuration level).
var listener = app.ServerFeatures.Get<WebListener>();
if (listener != null)
{
listener.AuthenticationManager.AuthenticationSchemes =
AuthenticationSchemes.NTLM;
}
Then in project.json add a weblistener cmd as follows:
"commands": {
"weblistener": "Microsoft.AspNet.Server.WebListener --config hosting.ini",
"web": "Microsoft.AspNet.Server.Kestrel"
},
... or similar. Then if you debug using the weblistener profile instead of IIS Express (or web, which under Kestrel does not support NTLM), you should be able to carry on working while the IIS Express tooling bug is resolved. You'll need to add Microsoft.AspNet.Server.WebListener
to your project.json dependencies to enable WebListener, I believe.
I found that if I changed the "web" command directly in project.json, Visual Studio helpfully changes it back rather aggressively, so adding a separate command as recommended by the Microsoft team seems to keep everything happy.
There's a known tooling bug that prevents you from disabling "anonymous authentication": https://github.com/aspnet/Hosting/issues/419.
Re-enable it and the issue you're seeing should disappear.
Make sure you've also added app.UseIISPlatformHandler();
early in your Configure
method: it is needed to resolve the Windows identity corresponding to the token flowed by IIS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With