Unautorized 401 While I'm not using any Authentication

Abdurhman Alsobhi 21 Reputation points
2022-03-28T10:35:14.54+00:00

Im using ASP.NET Core 6 WebAPI,

As you can see in my Program.cs im not using any kind of auth, In development (dotnet run) everything works,
but when I deploy to IIS with dotnet publish -r win-x64 --self-contained -o release12, I get 401.2 unauthorized on all routes
web.config on IIS looks like this

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\MaintBader.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>

This is my Program.cs

using Microsoft.EntityFrameworkCore;
using MaintBader.Data;
using MaintBader.Settings;

var  MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
    options.AddPolicy(name: MyAllowSpecificOrigins,
                      builder =>
                      {
                          builder.WithOrigins("http://localhost:4040",
                                              "http://127.0.0.1:5500")
                                                .AllowAnyHeader()
                                                .AllowAnyMethod();
                      });
});


// Add services to the container.


builder.Services.AddDbContext<MaintBaderContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("SB-STTN-Dev-Connection")));


builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.

    app.UseSwagger();
    app.UseSwaggerUI();


using (var scope = app.Services.CreateScope())
{
    var services = scope.ServiceProvider;

    var context = services.GetRequiredService<MaintBaderContext>();
    context.Database.EnsureDeleted();
    context.Database.EnsureCreated();
    DbInitializer.Initialize(context);

}


app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

app.UseCors(MyAllowSpecificOrigins);



app.MapControllers();

app.Run();
Internet Information Services
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,080 questions
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 25,866 Reputation points
    2022-03-28T13:59:02.833+00:00

    The full error is 401.2 - Logon failed due to server configuration.

    It is, and Anonymous is Enabled

    If Anonymous Authentication and only Anonymous Authentication is enabled in IIS then the Anonymous account has not been granted access to the application directory.

    Also, I think --self-contained needs to be followed by true/false.

    https://learn.microsoft.com/en-us/dotnet/core/deploying/deploy-with-cli#self-contained-deployment

    0 comments No comments

  2. Zaid Ashraf 1 Reputation point
    2022-03-29T01:20:43.75+00:00

    I'm just checking in to see if the information provided above was of assistance. Please let us know if you have any other questions.

    Thank you very much.

    https://batteriadipentole.com/

    0 comments No comments