✅ Building a web api release. No HTTPS and how do I change ports?
I am trying to do a release build for a small web api I've made. I can run it but it defaults on port 5000 and it's using HTTP. I eventually get an error message when I try to use a POST on an API endpoint that is protected by a JWT. I get a http error 401 (unauthorized) and I get this in the console window:
Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.
I suspect the 401 error is because it's being sent over HTTP and not HTTPS (getting this idea from a couple of threads after googling).
I've googled a lot and all the results comes down to launchSettings.json under properties or appsettings.json but none of them even have the HTTP port 5000 in them. A few forums suggested go into project settings and go into the web tab and change the information there but there is no such tab for me.
I'm not really sure how to progress here. Help!
51 Replies
app.UseHttpsRedirection(); is already added in program.cs
if you don't have HTTPS set up then you shouldn't be redirecting HTTP requests to HTTPS
So how do I setup the HTTPS? Those things are like... default in the web api being generated by the wizard..
for testing it should set up a self signed cert for you
I mean that app.UseHttpsRedirection(); is being added automatically.
Okay that rings a bell. I've noticed the self signed certificate.
Can you explain more?
when you start the program what ports does the log say it's listening on?
The release? 5000
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5000
based on the current project i have open you may need to add another endpoint to bind to in
launchSettings.json
likeDevelopment?
no, the urls
This is my entire launcshettings.json
It's default
Where would I add that?
apparently pastebin is blocked on my work network
Suggest another paste site
$paste
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
BlazeBin - dktfcidpbjov
A tool for sharing your source code with the world!
Does that work?
so you already have a launch profile that's set up for https
are you using that one?
Yeah it's by default
When running through Visual Studio - YES
It works
But when I build the whole project and execute the executable in the release folder, that's when I get the problems
yeah launchsettings are only for the IDE
Andrew Lock | .NET Escapades
5 ways to set the URLs for an ASP.NET Core app
In this post I describe 5 different ways to set which URLs your ASP.NET Core application listens on.
it will still blow up if you try to deploy it like this, you'd have to add a SSL cert in there and i don't know how to do that directly in ASP.NET Core
i host everything behind reverse proxies that handle the HTTPS end of things
Thanks for your help. It didn't solve my issue though
There is already a self cert, wouldn't it just use that?
if you're running it on your dev machine, probably
where are u publishing? also what executable?
@Henkypenky I am just trying to create an executable for this machine who will run the API. I assumed building it for "release" was the way to do it. I'm still learning this web api stuff and mvc/blazor/whatever. Our teacher recently posted a video of how to make a "release" and "publish" the project but when it comes to api at least, there seems to be a lot more to do than just building 😅
i see
but this is not an executable program
it's a web api
a bunch of files
that are controlled by a web server
locally visual studio handles everything for you
Yes of course
So how do I modify the settings for that said web server? Or is it more complicated than that?
Should I just skip JWT bearer?
Because http works just ifne
fine*
there are 2 things to tackle here
first is JWT
and second is publishing
as in deploying to a web server
i guess
we can go from; share all the code
and see where we go from there
Can't I just compile the whole thing and it will run the webserver for me?
locally, yes
Why shouldn't I just be able to compile the executable and upload it and the necessary data to a server and run it?
what executable?
The one that I build?
Host ASP.NET Core on Linux with Nginx
Learn how to set up Nginx as a reverse proxy on Ubuntu, RHEL and SUSE to forward HTTP traffic to an ASP.NET Core web app running on Kestrel.
an asp.net core web api does produce an executable that you can just run, it hosts everything through kestrel
the way you actually deploy it will depend on what exactly you're trying to deploy it on
Exactly. I am trying to configure and use that executable for a small project. It defaults on port 5000 and uses no HTTPS from what I can see because of the HTTPS error.
I am simply using "release" and then build ProjectName
this may be what you want https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0#replace-the-default-certificate-from-configuration-1
Configure endpoints for the ASP.NET Core Kestrel web server
Learn about configuring endpoints with Kestrel, the cross-platform web server for ASP.NET Core.
lets you set the endpoints and SSL cert information in appsettings.json
Thank you @fixed(void* x = &Jimmacle) !
We are not in there yet when it comes to generating certificates so I'll just pause now.
I'll ask my teacher tomorrow about it
Thank you all
Even the "http server" automatically set's it port to 5000 conflicting with the webapi
what's the difference between the "http server" and the web api
the latter is hosted on the former
They are two entirely different projects
Why do Visual Studio insist on having everything listening on port 5000?
so one is the actual web project and one is just a library?
or do you actually have 2 web projects
Project 1: A website using a HTTP client to connect to API (Project 2)
Project 2: Running a web api
it doesn't insist, it's configured in the launch settings file
okay so you have 2 web projects
But there are NO port 5000 anywhere to be found
In the settings file at least
Yes, you have a few DEBUGGER ports
That's about it
Not even those are used in "release"
all i can think of is 5000 is just the default port that kestrel binds to
in the absence of any other configuration
Debugging works, it's using the ports in the appsettings.development.json file. When I switch from Debug to Release and build it, it will simply use port 5000.
There are no settings where this is defined. So it's most automatically defined. I've tried several things to include ssl etc. but it doesn't work. At one point it didn't even accept my HTTP anymore. There has to be a simple fix to this rater than adding 10 lines of code?
dumb question, does the appsettings in your release output directory actually match what you want it to be?
It does and I meant launchSettings.json, my bad 😅