C
C#2y 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_OP2y ago
app.UseHttpsRedirection(); is already added in program.cs
Jimmacle
Jimmacle2y ago
if you don't have HTTPS set up then you shouldn't be redirecting HTTP requests to HTTPS
Ice_
Ice_OP2y ago
So how do I setup the HTTPS? Those things are like... default in the web api being generated by the wizard..
Jimmacle
Jimmacle2y ago
for testing it should set up a self signed cert for you
Ice_
Ice_OP2y 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
Jimmacle2y ago
when you start the program what ports does the log say it's listening on?
Ice_
Ice_OP2y ago
The release? 5000 info: Microsoft.Hosting.Lifetime[14] Now listening on: http://localhost:5000
Jimmacle
Jimmacle2y 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_OP2y ago
Development?
Jimmacle
Jimmacle2y ago
no, the urls
Ice_
Ice_OP2y 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_OP2y ago
This is my entire launcshettings.json It's default Where would I add that?
Jimmacle
Jimmacle2y ago
apparently pastebin is blocked on my work network
Ice_
Ice_OP2y ago
Suggest another paste site
Jimmacle
Jimmacle2y ago
$paste
MODiX
MODiX2y 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_OP2y ago
BlazeBin - dktfcidpbjov
A tool for sharing your source code with the world!
Ice_
Ice_OP2y ago
Does that work?
Jimmacle
Jimmacle2y ago
so you already have a launch profile that's set up for https are you using that one?
Ice_
Ice_OP2y 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
Jimmacle2y ago
yeah launchsettings are only for the IDE
Jimmacle
Jimmacle2y 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
Jimmacle2y 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_OP2y 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
Jimmacle2y ago
if you're running it on your dev machine, probably
Henkypenky
Henkypenky2y ago
where are u publishing? also what executable?
Ice_
Ice_OP2y 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
Henkypenky2y 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_OP2y 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
Henkypenky2y 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_OP2y ago
Can't I just compile the whole thing and it will run the webserver for me?
Henkypenky
Henkypenky2y ago
locally, yes
Ice_
Ice_OP2y 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
Henkypenky2y ago
what executable?
Ice_
Ice_OP2y ago
The one that I build?
Henkypenky
Henkypenky2y 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
Jimmacle2y 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_OP2y 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
Jimmacle2y ago
lets you set the endpoints and SSL cert information in appsettings.json
Ice_
Ice_OP2y 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
Jimmacle2y ago
what's the difference between the "http server" and the web api the latter is hosted on the former
Ice_
Ice_OP2y ago
They are two entirely different projects Why do Visual Studio insist on having everything listening on port 5000?
Jimmacle
Jimmacle2y ago
so one is the actual web project and one is just a library? or do you actually have 2 web projects
Ice_
Ice_OP2y ago
Project 1: A website using a HTTP client to connect to API (Project 2) Project 2: Running a web api
Jimmacle
Jimmacle2y ago
it doesn't insist, it's configured in the launch settings file okay so you have 2 web projects
Ice_
Ice_OP2y 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
Jimmacle2y 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_OP2y 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
Jimmacle2y ago
dumb question, does the appsettings in your release output directory actually match what you want it to be?
Ice_
Ice_OP2y ago
It does and I meant launchSettings.json, my bad 😅
Want results from more Discord servers?
Add your server