[英].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.