Reputation: 95
I have a problem to my .net core application. This problem is user secret json file can not read. Application work fine on development but application gives me error on release version in hosting server.
Error code some bellow
Application startup exception: System.ArgumentNullException: Value cannot be null. Parameter name: Password at System.Data.SqlClient.SqlConnectionStringBuilder.set_Password(String value) at myApplicationCore.WebUI.Startup.ConfigureServices(IServiceCollection services) in C:\Users\myComp\source\repos\myApplicationCore\myApplicationCore.WebUI\Startup.cs:line 42 --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() crit: Microsoft.AspNetCore.Hosting.Internal.WebHost[6] Application startup exception System.ArgumentNullException: Value cannot be null. Parameter name: Password at System.Data.SqlClient.SqlConnectionStringBuilder.set_Password(String value) at myApplicationCore.WebUI.Startup.ConfigureServices(IServiceCollection services) in C:\Users\myComp\source\repos\myApplicationCore\myApplicationCore.WebUI\Startup.cs:line 42 --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() Hosting environment: Production Content root path: D:\vhosts\mydomainexample.com\httpdocs Now listening on: http://127.0.0.1:25696 Application started. Press Ctrl+C to shut down. Application is shutting down...
My Startup.cs file
public class Startup
{
public IConfiguration Configuration;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
var builder = new SqlConnectionStringBuilder(Configuration.GetConnectionString("DefaultConnection"));
builder.Password = Configuration["DbPassword"];
services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(builder.ConnectionString,
b =>
{
b.MigrationsAssembly("myApplicationCore.WebUI");
b.ServerVersion(new Version(5, 6, 44), ServerType.MySql); // replace with your Server Version and Type
}
));
services.AddIdentity<ApplicationUser, IdentityRole>(opts => {
opts.User.RequireUniqueEmail = true;
opts.Password.RequiredLength = 3;
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequireLowercase = false;
opts.Password.RequireUppercase = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddTransient<IUnitOfWork, EfUnitOfWork>();
services.AddTransient<IEmailSender, EmailSender>();
services.Configure<AuthMessageSenderOptions>(Configuration);
services.ConfigureApplicationCookie(options => options.LoginPath = "/Account/Sign");
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSignalR();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStatusCodePages();
app.UseStaticFiles();
app.UseAuthentication();
app.UseSignalR(routes =>
{
routes.MapHub<HubSignal>("/hubCon");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}"
);
routes.MapSpaFallbackRoute(
name: "spa-fallback",
templatePrefix: "Portal",
defaults: new { controller = "Portal", action = "Index" });
});
SeedData.EnsurePopulated(app);
SeedData.CreateRoles(app.ApplicationServices).Wait();
SeedData.CreateIdentityUsers(app.ApplicationServices).Wait();
}
}
This is my Program.cs file
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
It gives me this error when I enter in to the web address.
An error occurred while starting the application. .NET Core 4.6.27817.03 X86 v4.0.0.0 | Microsoft.AspNetCore.Hosting version 2.2.0-rtm-35687 | Microsoft Windows 10.0.14393 | Need help?
I think server can not find secret.json file but I don't know how can I find file.
How can i solve this problem ?
Thanks for helping.
Upvotes: 1
Views: 539
Reputation: 239440
User secrets is not supported outside of Visual Studio. It's specifically and exclusively for development.
Despite the name, it's not actually secret anyways. It's still just plain old JSON and everything is stored plain-text, unencrypted. The only benefit to it is that it gives you place to store configuration in development outside of the project, so it doesn't accidentally end up in source control. That's it. As a production config option, it would be worse than useless.
Upvotes: 1