Why ASP.Net Core 7.0 Web API showing as Connection refused? - asp.net-core

I am new to asp.net core 7.0.
I am working on the Web Api. However I created one controller with [ApiController] decoration.
In the launchSettings.json file it shows port number as 14023 as shown below :-
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:14023",
"sslPort": 44381
}
Means that my Web API will be pointing to http://localhost:14023
However when I run the application and navigate to http://localhost:14023
it shows below error :
This site can’t be reached
localhost refused to connect.
ERR_CONNECTION_REFUSED
And when I hit the same API URL through Postman then below error coming :-
Error: connect ECONNREFUSED 127.0.0.1:14023
Initially I thought that it could be related to CORS so I added below code in Program.cs file :-
builder.Services.AddCors();
app.UseCors(options =>
options.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
Can anyone help me on this?
I am stuck. Even I googled but did not find any suitable answer.

Related

How to test UseHttpsRedirection setting locally in .NET Core

In my ASP.NET Core project, I have turned ON HTTPS Redirection, with this setting in my Program.cs:
app.UseHttpsRedirection();
I have referred to MS doc.
Locally, when I run my Core Web project, it uses https by default (https://localhost:7432/). Now, to test if redirection from http -> https works, I browse to http://localhost:7432/, but I get a "This page isn’t working right now" error.
So, how do I test if this redirection is working locally?
In the Properties/launchSettings.json file, you'll see a property for applicationUrl that looks like this:
"applicationUrl": "https://localhost:7250;http://localhost:5097"
This sets up the app to listen on two different ports: 7250 for HTTPS and 5097 for HTTP. In your specific project, find the HTTP URL and use that to test the HTTPS redirection locally.
As far I know, you can't share the same port for both http and https. Instead, you should define the ports in the appsettings.json or appsettings.Production.json. FYI, I never define such thing in appsettings.json, because that file is typically part of the repository. To me, the best place is in the other file.
In one of those files, you should see (or add whereas not present) something like this:
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://*:5000"
},
"Https": {
"Url": "https://*:5001",
"Certificate": {
"Path": "(your certificate file)",
"Password": "(your certificate password)"
},
}
}
},
"AllowedHosts": "*",
"AllowInvalid": true,
"https_port": 443,
...
Doing so, when you try to call your site as http, it will attempt to switch to the other port, using https.
For sure not the best explaination, but that's what I know about the https redirection.

asp.net 6 Get Windows User

I am having a devil of a time getting a simple Windows Domain User in my view in a apsnet core MVC project. Looking in google, i see a lot of people having the same question but very few suggestions past a full auth system. I was just hoping to turn on windows auth and get a username, no need for any other functions then to just display a name in the view.
In the past I have used:
#User.Identity.Name
in my launchSettings.json:
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
I did also added to Program.cs:
app.UseAuthentication();
app.UseAuthorization();
Is there no way to just grab the windows user for a display value in asp.net core 6
In my opinion,you should have authentication to get UserName,you need to Enable Windows Authentication.
Program.cs:
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
builder.Services.AddAuthorization(options =>
{
// By default, all incoming requests will be authorized according to the default policy.
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddHttpContextAccessor();
app.UseAuthentication();
app.UseAuthorization();
launchSettings.json:
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
}
Controller:
string UserName = HttpContext.User.Identity.Name;
Test Result:
For more details,please check this link.
If you do not need to automaticaly login the user with Windows Authentication, and you have already a custom login Controller to do that,you can refer to this.

How to setup a test domain which points to my Controller runs locally

I have a C# controller which is running at port 44347. And when I go to browser locally https://127.0.0.0.1:44347/myurl, it hits my Controller runs on the same machine.
I want to setup so that I can when I load https://mytest.mycom.com:44347/myurl locally, it hits my controller run locally.
I have added 'mytest.mycom.com 127.0.0.1' to my hosts file in Windows. And I verify that ping mytest.mycom.com , it has reply.
But when I go https://mytest.mycom.com:44347/myurl locally, I get message saying 'mytest.mycom.com' took too long to respond.
Can you please tell me what am I missing?
You can change the url that your application uses within launchSettings.config (under the properties folder). Following is an example -
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:39655/",
"sslPort": 44340
}
},
Changing "applicationUrl" will allow you to use your test domain locally.

ASP.NET Core 3.1 CORS error in VS for Mac

Initially, I started to develop my project on PC and now I'm trying to run it on Mac. When my front end tries to reach my web API endpoint, I'm getting the following error:
Failed to load resource: Origin https://localhost:3000 is not allowed by Access-Control-Allow-Origin
The thing is that it works perfectly on Windows, but doesn't work on Mac.
My code to enable CORS:
builder.UseCors(x => x
.WithOrigins(new string[] { "https://localhost:3000" })
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
How to solve this issue?
In "ConfigureServices" of your startup.cs add this (it is a different syntax from yours):
services.AddCors(o => o.AddPolicy("AllowSpecificOrigins", builder =>
{
builder..WithOrigins("https://localhost:3000")
.AllowAnyMethod()
.AllowAnyHeader();
}));
In "Config" change yours to this:
app.UseCors("AllowSpecificOrigins");
and place it after UseRouting but before UseAuthorization.
Also remove all Cors attributes from all controller actions.
Okay, it turned out that I had to "trust" the dev certificate created by dotnet core command. I opened my backend URL from browser (https://localhost:44372), clicked "Proceed" when the "site is unsafe" prompt appeared and only after that my front end (https://localhost:3000) was able to reach it.

Windows Authentication with asp.net core

Please provide guidance on how to implement Windows Authentication on ASP.NET Core RC2+.
I see other SO questions that describe bearer authentication like Bearer Authentication with ASP.NET Core RC2 404 instead of 403
But that is not what I am looking for.
You can do this using WebListener, like so:
Open your project.json and add WebListener to dependencies:
"dependencies" : {
...
"Microsoft.AspNetCore.Server.WebListener": "0.1.0-rc2-final"
...
}
Add WebListener to commands (again in Project.json)
"commands": {
"weblistener": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener"
},
In Startup.cs, specify the WebHostBuilder to use WebListener with NTLM
var host = new WebHostBuilder()
// Some configuration
.UseWebListener(options => options.Listener.AuthenticationManager.AuthenticationSchemes = AuthenticationSchemes.NTLM)
// Also UseUrls() is mandatory if no configuration is used
.Build();
That's it!
This doesn't appear to work any longer in the .Net Core 1.0.0 (RTM). I do the WebHostBuilder exactly as above in Ivan Prodanov's answer; it runs, don't get an error there, but the HttpContext.User is not marked with a WindowsIdentity. Following code used to work in ASP.Net 5 beta6:
in project.json:
"version": "1.0.0"
"dependencies": {
"Microsoft.AspNetCore.Owin": "1.0.0",
"Microsoft.AspNetCore.Server.WebListener": "0.1.0",
in middleware class:
public async Task Invoke(HttpContext context)
{
try
{
ClaimsPrincipal principal = context.User;
// <-- get invalidcastexception here:
WindowsIdentity winIdentity = (WindowsIdentity)principal.Identity;
....
....
Check your launchSettings.json file - change anonymousAuthentication to false
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
For deployment to iis check this Asp.Net core MVC application Windows Authentication in IIS