Windows auth in aspnet core is unpredictable using IISExpress or HttpSys - authentication

In my asp.net core web app I need to find a login name for the user making a request. This is done in the middleware using a name claim from HttpContext.
public static string GetUserName(HttpContext ctx)
{
var claim = ctx.User.FindFirst(ClaimTypes.NameIdentifier);
if (claim != null)
return claim.Value;
claim = ctx.User.FindFirst(ClaimTypes.Name);
var arr = claim?.Value.Split('\\');
return arr?[arr.Length - 1];
}
But sometimes Context.User.UserClaims is empty. It looks like this mostly happens when multiple requests come almost at the same time. For some of those requests claims are empty and for some they are not.
It never happens to me under IIS but does happen under IISExpress and HttpSys. I hope someone has an idea why it behaves this way.
Here's my launch.json
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5000/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"use64Bit": true
},
"HttpSys": {
"commandName": "Project",
"launchBrowser": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

Related

.net 7 Unable to connect to web server 'Local'. The web server is no longer running

There is a lot of info on this problem, but none of them match what I'm seeing.
Here is the relevant info:
Visual Studio 2022, net 7
I'm working on setting up specific profiles that match our pre-existing environments. I believe I have a fundamental misunderstanding of "launchsettings.json" perhaps. Something is going wrong and I have been unable to figure it out.
Launch settings:
{
"profiles": {
"Local": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Local"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:9059;"
}
//"Green": {
// "commandName": "Project",
// "launchBrowser": true,
// "launchUrl": "swagger",
// "environmentVariables": {
// "ASPNETCORE_ENVIRONMENT": "Green"
// },
// "dotnetRunMessages": true,
// "applicationUrl": "https://localhost:9053;http://localhost:9053;"
//},
//"Yellow": {
// "commandName": "Project",
// "launchBrowser": true,
// "launchUrl": "swagger",
// "environmentVariables": {
// "ASPNETCORE_ENVIRONMENT": "Yellow"
// },
// "dotnetRunMessages": true,
// "applicationUrl": "https://localhost:9053;http://localhost:9053;"
//},
//"YellowVariant": {
// "commandName": "Project",
// "launchBrowser": true,
// "launchUrl": "swagger",
// "environmentVariables": {
// "ASPNETCORE_ENVIRONMENT": "YellowVariant"
// },
// "dotnetRunMessages": true,
// "applicationUrl": "https://localhost:9053;http://localhost:9053;"
//},
//"Umber": {
// "commandName": "Project",
// "launchBrowser": true,
// "launchUrl": "swagger",
// "environmentVariables": {
// "ASPNETCORE_ENVIRONMENT": "Umber"
// },
// "dotnetRunMessages": true,
// "applicationUrl": "https://localhost:9053;http://localhost:9053;"
//},
//"White": {
// "commandName": "Project",
// "launchBrowser": true,
// "launchUrl": "swagger",
// "environmentVariables": {
// "ASPNETCORE_ENVIRONMENT": "White"
// },
// "dotnetRunMessages": true,
// "applicationUrl": "https://localhost:9053;http://localhost:9053;"
//}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5709",
"sslPort": 44372
}
}
}
You'll see the following environments: Local, Green, Yellow, YellowVariant, Umber, White (local, test server, qa, alternate qa, uat, prod)
They are all set as commandName: "Project" because - that's what they were before under the "Development" setting. My understanding is this uses Kestrel instead of IIS Express.
It does load when I change commandName to "IISExpress" but loads as SSL and we do not currently use SSL for our service layer. Though the browser screen sits there on a blank screen and nothing ever loads.
Here are the steps I followed (taken from another stack overflow article):
>Clean and Rebuild Projects dependencies
>Clean and Rebuild the primary project
>Verify launchsettings and project build properties
>Close VS
>Clear bin and obj folders from the project
>Go to C:\Users\username\Documents\IISExpress\config and delete all files from this folder
>Remove the.VS folder from the solution directory
>Change port numbers (tried many different, but none of these are in use according to netstat/netsh)
>Reboot
It continues to occur. As you can see, I have everything commented out except local, to reduce complexity. If I change it back to "Development" it does not work either.
I appreciate any assistance. We're moving from net45 to net7 and the confusion is extensive. So there are quite possibly fundamentals I'm not understanding here.
I did read through https://dotnettutorials.net/lesson/asp-net-core-launchsettings-json-file/ to see if there was anything I could glean and nothing really seemed to help me solve this issue.
Thank you!

VSCode webapi default implementation does not run Swagger interface by default, 404 error occurs

When installing and running a new webapi template in Visual Studio Code:
dotnet new webapi -n weatherapi
the framework is set up to default to launch the Swagger interface, but subsequently executing dotnet run or launching with F5 will attempt to browse to the applicationUrl property specified in launchSettings.json. This subsequently throws a 404 error since this is not the Swagger definition url.
Also within launchSettings.json, launchBrowser defaults to true, and launchUrl defaults to swagger.
According to the documentation, the first entry in the profiles section of launchSettings.json will be the profile used unless specified otherwise in launch.json (which it is not).
So the question is, why does the default implementation throw a 404 when it looks like it should be set up by default to land on the Swagger interface?
The out-of-the-box launchSettings.json implementation is:
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:21895",
"sslPort": 44320
}
},
"profiles": {
"weatherapi": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7205;http://localhost:5111",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

How to add HTTP endpoint to ASP.NET Core 6 app?

I created an ASP.NET Core 6 app with "Configure for HTTPS" checked during project creation. For testing purposes I now need a plain HTTP endpoint. How can I add such an endpoint?
My code currently looks like this:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World! - use /graphql");
app.Run();
Serhii and Serge pointed me to the launchSettings.json (note that it's inside the Properties folder!). That's how it looks like:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:20380",
"sslPort": 44329
}
},
"profiles": {
"TestProject": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7090;http://localhost:5090",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Note the line
"applicationUrl": "https://localhost:7090;http://localhost:5090",
and note that the second URL is plain HTTP. I could call this URL without any issues (started project normally using F5, HTTPS link is called automatically, but one can just switch manually to the HTTP link in the browser).
So, nothing to do for me, no configuration change was needed etc :-)

asp.net Core 3.1 Webapp unable to bind to localhost

i have an issue starting my asp.net core webapp with the profile "WebApp". i allways get:
"System.IO.IOException" in System.Private.CoreLib.dll
Ein Ausnahmefehler des Typs "System.IO.IOException" ist in System.Private.CoreLib.dll aufgetreten.
Failed to bind to address https://localhost:5000.
The port is not used by any other programm. I tried lots of different ports as well.
Does anybody has an idea what i can try?
On my colleagues Pc it works just fine.
Seems it does not have the right to bind to any ports.
Startup:
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
launchsettings.
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:23736",
"sslPort": 44369
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApp": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}```
IIS Profile works fine for me..
Would be very glad if one could help. trying for few days now.
I solved it. It was because the project was not located on my C-Drive but on a network drive from my company.
After moving it to the C drive it worked.

Asp.NETCore 3.1 With Swagger launchUrl does not work after Publish

ASP.NET Core Web API project is configured with Swagger. When I run the project on my local machine the launchUrl works correctly and automatically redirects to my Swagger apidocs location (https://localhost:44373/apidocs/index.html)
But as soon as I publish the project on Azure the launchUrl no longer works correctly.
(https://myapiurl.com) => Should'nd this redirect automatically to (https://myapiurl.com/apidocs/index.html) ?
What am I missing for the published environment?
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60837",
"sslPort": 44373
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "apidocs",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
"Web.Apis.Organization": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "apidocs",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
My COnfigure Method
app.UseStaticFiles();
// Enable middle-ware to serve generated Swagger as a JSON endpoint. https://github.com/domaindrivendev/Swashbuckle.AspNetCore#generate-multiple-swagger-documents
app.UseSwagger(options =>
{
options.RouteTemplate = "apidocs/{documentName}/apispec.json";
});
//Enable middle-ware to serve swagger - ui(HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
// https://dejanstojanovic.net/aspnet/2018/november/setting-up-swagger-to-support-versioned-api-endpoints-in-aspnet-core/
// https://github.com/microsoft/aspnet-api-versioning/issues/516
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "apidocs";
//Build a swagger endpoint for each discovered API version
foreach (var apiDescription in apiVersionProvider.ApiVersionDescriptions)
{
c.SwaggerEndpoint($"/{c.RoutePrefix}/{apiDescription.GroupName}/apispec.json", $"Version { apiDescription.ApiVersion.ToString()}");
}
c.DisplayRequestDuration();
c.DocumentTitle = GlobalConstants.OrganizationFullName;
// Custom Index
c.IndexStream = () => GetType().Assembly.GetManifestResourceStream("Web.Apis.Organization.wwwroot.swagger_ui.index.html");
// Custom style
//c.InjectStylesheet("/swagger-ui/custom.css");
//c.InjectJavascript("/swagger-ui/custom.js");
});