ServiceStack API Explorer: Login form not working when deployed to IIS as a "sub-application" - servicestack

I have a aspnet core (dotnet 6) application that uses ServiceStack authentication.
The built-in login form is displayed when the browser tries to access protected services, as expected.
One can login successfully, when the app is installed as a stand-alone site in IIS.
However, when the same app is installed as an "application" inside an existing IIS site, that same login form doesn't work.
In the browser's "developer tools/console" one can see the following javascript error:
Uncaught ReferenceError: Authenticate is not defined
at Proxy.login (ui:866:10)
at login (ui:862:137)
at Proxy.onsubmit (ui:1025:1)
at eval (eval at We (petite-vue.js?vfx=6,2:1:8737), <anonymous>:3:26)
at HTMLFormElement.i (petite-vue.js?vfx=6,2:1:8122)
login # ui:866
login # ui:862
onsubmit # ui:1025
eval # VM102:3
i # petite-vue.js?vfx=6,2:1
The exact line of the error is:
let auth=new Authenticate()
The web.config is for this sub-aplication:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\MyApp.exe" arguments=".\MyApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
</system.webServer>
</location>
</configuration>
What can be done to troubleshoot this error?

Related

Hosting a net6.0 web api in Azure App Service

I have created a demo Web Api in net6 using the Visual Studio 2022 template for that. The Web Api runs fine on my machine but when trying to host it in an Azure App Service it fails with the following error:
After some research I managed to have it working on Azure by changing the module "AspNetCoreModuleV2" to "AspNetCoreModule" in the web.config.
Here it is the web.config that gets deployed to Azure and it doesn't work:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Web6Test.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
Is it possible to host the web api in Azure using the "AspNetCoreModuleV2" module?
There can be multiple reasons causing HTTP 500.30 error: ASP .Net core app failed to start like App Initialization, App Configuration and Key vault permission issues.
Thanks to 130nk3r5, I figured out how to find the root cause of the error:
Go to Azure Portal
Select App Services
Search and click on your App service
Go to development tools and open console.
Run the application through this console and then the real error that is causing the application not to load will be displayed.
You can refer to this SO thread for multiple solutions.
Hosting the web api can also be achieved by using AspNetCoreModuleV2 in the web.config.
I have reproduced the same by creating a .Net 6.0 Core web API project in Visual Studio.
Web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\XXXXXX.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
<!--ProjectGuid: 09e5e23c-101e-41d1-bd27-60c6f5c08db0-->
Output when running the project locally:
Deployed the application to Azure App Service:
Output after hosting the application in Azure App Service:
Result:

IIS windows authentication works for website but not for web application under website

I have a website hosted in IIS using windows authentication. It works fine.
But if I host it as a web application unter a website, windows authentication doesn't work and the authentication window shows and then doesn't go away even if I put in the correct user name and password.
The SPN, delegation and IIS settings should be correct, otherwise the website won't work. Only if I deploy it as web application, windows authentication stoped working. Any idea?
Thanks for the reply, the sub status code is 401.1 and here the web.config file on the application level
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\DataService.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</location>
<system.web>
<authentication mode="Windows" />
</system.web>
</configuration>
I finally found the solution.After enabling ASP.Net Impersonation on the web application, it works.Seems like, the web application needs impersonation to get the kerberos ticket from the website.

Azure multiple sites in virtual directories

I have 3 asp.net core sites I need to deploy on the same domain (subdomains will not work with my SSL certificate).
https://my-site.com (Single page app)
https://my-site.com/api (Web api)
https://my-site.com/identity (Identity server)
When I deploy the api and identity projects, they work fine. However, when deploying the single page app, api and identity stop working.
The page cannot be displayed because an internal server error has occurred.
The error appears instantly so it's probably failing early on from startup I guess.
The single page app is working ok.
It seems the spa is interfering, I tried solutions from here to ignore the routes, but I get the same error.
I have tried the solution here to get a more descriptive error but to no avail.
Not sure where to go from here
The cause of the problem is that both the root application and the child app add the aspNetCore handler, causing the config system to blow up. You can see this by turning on Detailed error messages in the Azure Portal, and then finding the error page under D:\home\LogFiles\DetailedErrors. You'll see this error:
Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'aspNetCore'
There are two approaches to solving this.
The first is to use a location tag to prevent inheritance. Specifically, change your root app's web.config from something like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\myapp.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" />
</system.webServer>
</configuration>
to something like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\myapp.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" />
</system.webServer>
</location>
</configuration>
The second approach is to remove the <handlers> section from the sub-application to avoid the duplication (as suggested in the doc under Configuration of sub-applications):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<aspNetCore processPath="dotnet" arguments=".\mySubApp.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" />
</system.webServer>
</configuration>

Asp.Net core MVC application Windows Authentication in IIS

My Asp.Net Core mvc web application requires Windows Authentication. In developpement, on IIS Express, everything works fine thanks to this setting
launchSettings.json
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:61545/",
"sslPort": 0
}
}
When deploying to IIS, I get a blank page. The Request to my site get a 500 error code.
I tried to add this configuration to Startup.cs, as explained here , without success.
services.Configure<IISOptions>(options => {
options.ForwardWindowsAuthentication = true;
});
When I look into the authentication parameters directly in IIS, Windows Authentication is activated.
I found some post talking about a package called Microsoft.AspNetCore.Server.WebListener, others about implementing a custom Middleware. I can't imagine this basic feature needs that much effort to work. Am I missing something ?
launchSettings.json file is only used by VS. When you publish your app (or run without VS) launchSettings.json is not being used. When you run with IIS/IISExpress you just need to make sure that your web.config has correct settings. In your case the forwardWindowsAuthToken attribute in the web.config is missing or is set to false. It must be set to true for Windows Authentication to work. A sample web.config before publishing would look like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true"/>
</system.webServer>
</configuration>
For me I had to add the line
services.AddAuthentication(IISDefaults.AuthenticationScheme);
in the method ConfigureServices in Startup.cs
My app allows both Windows and Anonymous users.
https://learn.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x#windows-authentication-httpsys--iisintegration
You need check web.config in your project dir. This settings was help me.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true"/>
<security>
<authentication>
<anonymousAuthentication enabled="false" />
<windowsAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
</configuration>

ServiceStack REST service custom path error

I am having trouble configuring my ServiceStack REST service to work on my production IIS 7.5 box. It works fine running localhost, and it also works fine if I deploy in the root of "Default Web Site" - /EmployeeSvc. I have a virtual directory structure under Default Web Site for organizational purposes (note custom path in web.config). I can browse successfully to the default.htm at this location, but when I try to execute the built-in /hello example I receive:
Server Error 404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.
Like I said, the built-in /hello example works perfectly from localhost and from a root iis folder under default web site, but not from within a directory structure. What am I doing wrong with the location-path?
Thanks in advance! Here is web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="Employees-NoAuth/WebServices/EmployeeSvc">
<system.web>
<httpHandlers>
<add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
</system.web>
<!-- Required for IIS 7.0 -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" verb="*" />
</handlers>
</system.webServer>
</location>
<system.web>
<customErrors mode="RemoteOnly"/>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
I'd try the following:
1. Remove location node from web.config. Move SS configuration out of it.
2. Register base URL path in your class derived from AppHostBase:
public override void Configure(Container container)
{
this.SetConfig(new EndpointHostConfig { ServiceStackHandlerFactoryPath = "base/path" });
}

Resources