C
C#14mo ago
Ice_

✅ 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
Ice_
Ice_14mo ago
app.UseHttpsRedirection(); is already added in program.cs
Jimmacle
Jimmacle14mo ago
if you don't have HTTPS set up then you shouldn't be redirecting HTTP requests to HTTPS
Ice_
Ice_14mo ago
So how do I setup the HTTPS? Those things are like... default in the web api being generated by the wizard..
Jimmacle
Jimmacle14mo ago
for testing it should set up a self signed cert for you
Ice_
Ice_14mo ago
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?
Jimmacle
Jimmacle14mo ago
when you start the program what ports does the log say it's listening on?
Ice_
Ice_14mo ago
The release? 5000 info: Microsoft.Hosting.Lifetime[14] Now listening on: http://localhost:5000
Jimmacle
Jimmacle14mo ago
based on the current project i have open you may need to add another endpoint to bind to in launchSettings.json like
Ice_
Ice_14mo ago
Development?
Jimmacle
Jimmacle14mo ago
no, the urls
Ice_
Ice_14mo ago
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Ice_
Ice_14mo ago
This is my entire launcshettings.json It's default Where would I add that?
Jimmacle
Jimmacle14mo ago
apparently pastebin is blocked on my work network
Ice_
Ice_14mo ago
Suggest another paste site
Jimmacle
Jimmacle14mo ago
$paste
MODiX
MODiX14mo ago
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!
Ice_
Ice_14mo ago
BlazeBin - dktfcidpbjov
A tool for sharing your source code with the world!
Ice_
Ice_14mo ago
Does that work?
Jimmacle
Jimmacle14mo ago
so you already have a launch profile that's set up for https are you using that one?
Ice_
Ice_14mo ago
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
Jimmacle
Jimmacle14mo ago
yeah launchsettings are only for the IDE
Jimmacle
Jimmacle14mo ago
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.
Jimmacle
Jimmacle14mo ago
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
Ice_
Ice_14mo ago
Thanks for your help. It didn't solve my issue though There is already a self cert, wouldn't it just use that?
Jimmacle
Jimmacle14mo ago
if you're running it on your dev machine, probably
Henkypenky
Henkypenky14mo ago
where are u publishing? also what executable?
Ice_
Ice_14mo ago
@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 😅
Henkypenky
Henkypenky14mo ago
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
Ice_
Ice_14mo ago
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*
Henkypenky
Henkypenky14mo ago
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
Ice_
Ice_14mo ago
Can't I just compile the whole thing and it will run the webserver for me?
Henkypenky
Henkypenky14mo ago
locally, yes
Ice_
Ice_14mo ago
Why shouldn't I just be able to compile the executable and upload it and the necessary data to a server and run it?
Henkypenky
Henkypenky14mo ago
what executable?
Ice_
Ice_14mo ago
The one that I build?
Henkypenky
Henkypenky14mo ago
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.
Jimmacle
Jimmacle14mo ago
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
Ice_
Ice_14mo ago
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
Jimmacle
Jimmacle14mo ago
lets you set the endpoints and SSL cert information in appsettings.json
Ice_
Ice_14mo ago
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
Jimmacle
Jimmacle14mo ago
what's the difference between the "http server" and the web api the latter is hosted on the former
Ice_
Ice_14mo ago
They are two entirely different projects Why do Visual Studio insist on having everything listening on port 5000?
Jimmacle
Jimmacle14mo ago
so one is the actual web project and one is just a library? or do you actually have 2 web projects
Ice_
Ice_14mo ago
Project 1: A website using a HTTP client to connect to API (Project 2) Project 2: Running a web api
Jimmacle
Jimmacle14mo ago
it doesn't insist, it's configured in the launch settings file okay so you have 2 web projects
Ice_
Ice_14mo ago
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"
Jimmacle
Jimmacle14mo ago
all i can think of is 5000 is just the default port that kestrel binds to in the absence of any other configuration
Ice_
Ice_14mo ago
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?
Jimmacle
Jimmacle14mo ago
dumb question, does the appsettings in your release output directory actually match what you want it to be?
Ice_
Ice_14mo ago
It does and I meant launchSettings.json, my bad 😅