aspnet core Identity not working after deployment on server
I cannot login to my web application service after deploying it to a server, it is running inside a docker container, it is working properly on my local machine, with docker or without docker
On the server, the login seams to be successful, it is generating the AspNetCore.Identity.Application cookies, but for some reason they are not being picked up after the application ,
my nav bar stays in the sign in state which means User.Identity.IsAuthenticated is always false,
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?}");
});
}
}
My 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"
}
}
}
}
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"]
Known Workarounds
i have discovered a weird hack that gets it to work
first i need to login in (https)
then i change the url to (http) it change back to https , at this stage it is not working
if i refresh the page then it works , i can see my username in the nav bar insead of the signin button
Configuration
i'm using .net core 5,
Server config :
Kubernetes Version 1.23
hosting provider (Linode)
docker for containerization
i tried to edit cookie settings in my startup file as mentionned in other forums but it is not working, the change i made on the startup file does not seam to be affect the new generated cookies,
samesite is always Lax ,secure is alway false, httponly is always true
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.Cookie.SameSite = SameSiteMode.None;
options.Cookie.SecurePolicy = CookieSecurePolicy.None;
options.Cookie.Domain = "*.mydomain.com";
});