简体   繁体   English

.NET Core 2.0访问用户机密

[英].NET Core 2.0 accessing user secrets

I'm trying to setup a .net core 2.0 web application to send an email when user registers and also to recover password. 我正在尝试设置.net核心2.0 Web应用程序,以便在用户注册时发送电子邮件并恢复密码。 I have followed this tutorial: https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?tabs=visual-studio . 我已经按照本教程: https//docs.microsoft.com/en-us/aspnet/core/security/app-secrets?tabs=visual-studio

However, upon reading the comments sections, it seems that the tutorial is not updated for Core 2.0. 但是,在阅读评论部分时,似乎没有为Core 2.0更新教程。 My question is, when I get to part " Add the user secrets configuration source to the Startup method ", I cannot figure out how the startup file should look like since my startup file is different from the one showed there. 我的问题是,当我开始分享“ 将用户机密配置源添加到启动方法 ”时,我无法弄清楚启动文件应该是什么样子,因为我的启动文件与那里显示的不同。 Can anyone help me by showing me how the startup file should look like? 任何人都可以通过向我展示启动文件应该是什么样子来帮助我吗? Thanks. 谢谢。

This is my current startup file: 这是我目前的启动文件:

public class Startup
{
    string _testSecret = null;

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        _testSecret = Configuration["MySecret"];
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>(config =>
        {
            config.SignIn.RequireConfirmedEmail = true;
        })
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

        // Add application services.
        services.AddTransient<IEmailSender, EmailSender>();

        services.AddMvc();
        services.Configure<AuthMessageSenderOptions>(Configuration);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
            app.UseDatabaseErrorPage();
            builder.AddUserSecrets<Startup>();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

In your code I didn't find invoking of build() function of ConfigurationBuilder class. 在您的代码中,我没有找到调用ConfigurationBuilder类的build()函数。 Here is the sample code. 这是示例代码。

var builder = new ConfigurationBuilder();
builder.AddUserSecrets<Startup>();
var config builder.Build(); //This line is missing from your code
string mySecret = config ['EmailAccount'];

Refernce: https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-2.1&tabs=windows#access-a-secret 参考: https ://docs.microsoft.com/en-us/aspnet/core/security/app-secrets ? view = aspnetcore-2.1 & tabs = windows#access-a-secret

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

相关问题 Asp.net Core 2.0 Configure.Services <model> ()绑定不适用于用户机密 - Asp.net core 2.0 Configure.Services<model>() binding isn't working with user secrets .Net Core 2.1不读取用户秘密 - .Net Core 2.1 not reading User Secrets EF核心2.0迁移以及附属程序和用户机密中的上下文 - EF Core 2.0 Migrations With Context In Satellite Assembly and User Secrets .net 核心 2.2,从 serilog 的用户机密中读取设置 - .net core 2.2, Reading setting from user secrets for serilog 用户机密不适用于Windows服务中托管的.net核心应用程序 - User secrets not available to .net core app hosted in windows service 在 asp.net core 项目中禁用用户机密 - Disabling user secrets in asp.net core project 如何在 EF 脚手架的 .NET 核心控制台应用程序中管理用户机密? - How to manage user secrets in .NET Core Console Application for EF Scaffolding? 在.NET Core 2.1控制台应用程序中配置用户密码 - Configure User Secrets in .NET Core 2.1 Console Apps ".net core Worker Service (BackgroundService) 无法加载用户机密" - .net core Worker Service (BackgroundService) cannot load user secrets 在 .NET 核心用户机密 (JSON) 中存储多行 RSA 密钥 - Storing multiline RSA Key in .NET core User Secrets (JSON)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM