简体   繁体   English

无法读取托管服务器上的用户机密

[英]Can not read user secrets on hosting server

I have a problem to my .net core application. 我的.net核心应用程序有问题。 This problem is user secret json file can not read. 此问题是用户机密json文件无法读取。 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. 应用程序启动异常:System.ArgumentNullException:值不能为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. 参数名称:C:\\ Users \\ myComp \\ source \\ repos \\ myApplicationCore \\ myApplicationCore.WebUI \\ Startup中myApplicationCore.WebUI.Startup.ConfigureServices(IServiceCollection services)处System.Data.SqlClient.SqlConnectionStringBuilder.set_Password(字符串值)处的密码。 cs:第42行---从之前抛出异常的位置开始的堆栈跟踪---在Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()在Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()---从上一个引发异常的位置开始的堆栈跟踪---在Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()处:Microsoft.AspNetCore。 Hosting.Internal.WebHost [6]应用程序启动异常System.ArgumentNullException:值不能为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. 参数名称:C:\\ Users \\ myComp \\ source \\ repos \\ myApplicationCore \\ myApplicationCore.WebUI \\ Startup中myApplicationCore.WebUI.Startup.ConfigureServices(IServiceCollection services)处System.Data.SqlClient.SqlConnectionStringBuilder.set_Password(字符串值)处的密码。 cs:第42行---从之前抛出异常的位置开始的堆栈跟踪---在Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()在Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()---在Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()处引发异常的先前位置的堆栈结束跟踪-托管环境:生产内容根路径:D:\\ vhosts \\ mydomainexample.com \\ httpdocs现在正在侦听: http : //127.0.0.1 : 25696应用程序已启动。 Press Ctrl+C to shut down. 按Ctrl + C关闭。 Application is shutting down... 应用程序正在关闭...

My Startup.cs file 我的Startup.cs文件

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 这是我的Program.cs文件

    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 | .NET Core 4.6.27817.03 X86 v4.0.0.0 | Microsoft.AspNetCore.Hosting version 2.2.0-rtm-35687 | Microsoft.AspNetCore.Hosting版本2.2.0-rtm-35687 | Microsoft Windows 10.0.14393 | 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. 我认为服务器无法找到secret.json文件,但是我不知道如何找到文件。

How can i solve this problem ? 我怎么解决这个问题 ?

Thanks for helping. 感谢您的帮助。

User secrets is not supported outside of Visual Studio. 在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. 它仍然只是普通的旧JSON,所有内容均以纯文本形式存储,未加密。 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. 作为生产配置选项,它会比没用还糟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM