'.net core authentication not working after deployment on server
I can't login to my web application after deploying it to a server, it's running inside a docker container, everything works properly on my local machine, with docker or without docker
On the server, the login seams to be successful, it's generating the AspNetCore.Identity.Application cookies on my browser, but for some reason they are not being picked up by the application, User.Identity.IsAuthenticated is always false,
Workaround.
i have discovered a weird hack that gets that to work but i still don't understand how to solve the problem.
first i need to login in (https)
then i change the url to (http) it change back automatically to https , at this stage it is not working
if i refresh the page then it works, User.Identity.IsAuthenticated is returning true.
any help is really appretiated.
Configuration.
- .net core 5
- Kubernetes Version 1.23
- hosting provider (Linode)
- docker for containerization
my startup file.
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<DBContext>(options =>
options.UseMySql(Configuration["ConnectionStrings.CloudDB"]
, ServerVersion.AutoDetect(Configuration["ConnectionStrings.CloudDB"]))
);
services.AddIdentity<AdminModel, IdentityRole>(options =>
{
options.Password.RequiredLength = 5;
options.SignIn.RequireConfirmedAccount = false;
options.SignIn.RequireConfirmedEmail = false;
options.SignIn.RequireConfirmedPhoneNumber = false;
}).AddEntityFrameworkStores<DBContext>()
.AddDefaultTokenProviders();
services.AddHttpClient<CustomHttpClient>()
.ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (m, crt, chn, e) => true
};
});
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseJwtAuthHandler();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
Launch settings.
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:29877",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Mvc": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
web app docker file.
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["Solutions/Mvc/Mvc.csproj", "Solutions/Mvc/"]
COPY ["Solutions/API/API_Editor/API_Editor.csproj", "Solutions/API/API_Editor/"]
COPY ["Solutions/API/API_Shared/API_Shared.csproj", "Solutions/API/API_Shared/"]
COPY ["Solutions/Shared/Shared.csproj", "Solutions/Shared/"]
RUN dotnet restore "Solutions/Mvc/Mvc.csproj"
COPY . .
WORKDIR "/src/Solutions/Mvc"
RUN dotnet build "Mvc.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Mvc.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Mvc.dll"]
Solution 1:[1]
Finally i got it to work,
The problem was ASP.NET Core Data Protection that needs to be setup when dealing with multiple replicates.
I had 3 replicats running, i cut that down to 1 then the problem is gone, i can authenticate like in my local machine.
To get it to work with multiple replicates you need to setup your data protection keys, you can find an explanation on how to get that to work here
https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-storage-providers?view=aspnetcore-5.0&tabs=visual-studio
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Badr Douah |
