gpt4 book ai didi

c# - .NET Core 2.0 访问用户 secret

转载 作者:行者123 更新时间:2023-11-30 12:55:34 27 4
gpt4 key购买 nike

我正在尝试设置一个 .net core 2.0 网络应用程序以在用户注册时发送电子邮件并恢复密码。我遵循了本教程:https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?tabs=visual-studio .

但是,阅读评论部分后,该教程似乎并未针对 Core 2.0 进行更新。我的问题是,当我进入“将用户 secret 配置源添加到启动方法”部分时,我不知道启动文件应该是什么样子,因为我的启动文件与显示的不同那里。任何人都可以通过向我展示启动文件的外观来帮助我吗?谢谢。

这是我当前的启动文件:

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?}");
});
}
}

最佳答案

在您的代码中,我没有发现调用 ConfigurationBuilder 类的 build() 函数。这是示例代码。

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

引用:https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-2.1&tabs=windows#access-a-secret

关于c# - .NET Core 2.0 访问用户 secret ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47822821/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com