无法从.net core的配置管理器获取用户秘密

本文关键字:管理器 配置管理 获取 用户 秘密 配置 net core | 更新日期: 2023-09-27 18:12:18

我目前正在设置一个。net核心web应用程序,并设置了MailKit来处理它的电子邮件发送。

没有硬编码我的smtp密码,我选择了用户秘密选项。然而,由于某种原因,每次我试图检索密码时,它返回为空。错误:

处理请求时发生未处理的异常。ArgumentNullException:值不能为空。参数名称:password在MessageServices.cs中,第56行

我想知道是否有人能看到我错过了什么!

这是我的MessageService.cs

    public class AuthMessageSender : IEmailSender, ISmsSender
{
    public IConfiguration Configuration { get; set; }
    public AuthMessageSender()
    {
        var builder = new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile("appsettings.json");
        Configuration = builder.Build();
    }
    public async Task SendEmailAsync(string email, string subject, string message, string fullName)
    {
        try
        {
            var _email = "info@*******.co.uk";
            var _epass = Configuration["AdminPassword:Email"];
            var _dispName = "Mark ****";
            var myMessage = new MimeMessage();
            var builder = new BodyBuilder();
            myMessage.To.Add(new MailboxAddress(fullName ?? "User", email));
            myMessage.From.Add(new MailboxAddress(_dispName, _email));
            myMessage.Subject = subject;
            builder.HtmlBody = message;
            myMessage.Body = builder.ToMessageBody();
            using (SmtpClient smtp = new SmtpClient())
            {
                bool UseSSL = true;
                string Host = "just22.justhost.com";
                int Port = 465;
                await smtp.ConnectAsync(Host, Port, UseSSL).ConfigureAwait(true);
                smtp.AuthenticationMechanisms.Remove("XOAUTH2");
                smtp.Authenticate(_email, _epass); // Note: only needed if the SMTP server requires authentication
                await smtp.SendAsync(myMessage).ConfigureAwait(false);
                await smtp.DisconnectAsync(true).ConfigureAwait(false);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

}
    public Task SendSmsAsync(string number, string message)
    {
        // Plug in your SMS service here to send a text message.
        return Task.FromResult(0);
    }
这里是start。cs
    public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
        if (env.IsDevelopment())
        {
            // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
            builder.AddUserSecrets();
        }
        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }
    public IConfigurationRoot Configuration { get; }
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
        services.AddMvc();
        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();
        services.AddDistributedMemoryCache();
        services.AddSession();
    }
    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public async void Configure(IApplicationBuilder app, IHostingEnvironment env,
        ILoggerFactory loggerFactory, IServiceProvider serviceProvider, ApplicationDbContext context)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }
        app.UseStaticFiles();
        app.UseSession();
        app.UseIdentity();
        // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
        app.UseFacebookAuthentication(new FacebookOptions()
        {
            AppId = Configuration["Authentication:Facebook:AppId"],
            AppSecret = Configuration["Authentication:Facebook:AppSecret"]
        });
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
        await CreateRoles(context, serviceProvider);
    }
    private async Task CreateRoles(ApplicationDbContext context, IServiceProvider serviceProvider)
    {
        var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
        var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
        // Create a list of roles with both name and normalised name attributes
        List<IdentityRole> roles = new List<IdentityRole>();
        roles.Add(new IdentityRole { Name = "Admin", NormalizedName = "ADMIN" });
        roles.Add(new IdentityRole { Name = "Member", NormalizedName = "MEMBER" });
        roles.Add(new IdentityRole { Name = "Moderator", NormalizedName = "MODERATOR" });
        // Check if the role already exists
        foreach (var role in roles)
        {
            var roleExist = await RoleManager.RoleExistsAsync(role.Name);
            if (!roleExist)
            {   // Add it if it doesn't
                context.Roles.Add(role);
                context.SaveChanges();
            }
        }
        var user = await userManager.FindByEmailAsync("mark****@gmail.com");
        if (user != null)
        {
            var gotRoles = userManager.GetRolesAsync(user);
            if (!gotRoles.Equals("Admin"))
            {
                await userManager.AddToRoleAsync(user, "Admin");
            }
        }
    }
}

我已经检查了以确保秘密存在,它确实存在,以及Facebook身份验证秘密,这似乎工作得很好。

如果我硬编码密码,电子邮件被发送。当我设置断点时,我可以看到密码确实为空。我有点难倒了!

无法从.net core的配置管理器获取用户秘密

基于@Kritner提供的信息和此链接的新答案:从启动类访问配置对象

首先,我创建了一个POCO,它有我所有的SMTP属性,应该是看不见的,像这样:

        public class SmtpConfig
{
    public string EmailDisplayName { get; set; }
    public string SmtpPassworrd { get; set; }
    public string SmtpUserEmail { get; set; }
    public string SmtpHost { get; set; }
    public int SmtpPort { get; set; }
}

然后在My Startup.cs中,我将此添加到ConfigureServices:

            services.Configure<SmtpConfig>(optionsSetup =>
        {
            //get from appsetings.json file
            optionsSetup.SmtpPassworrd = Configuration["SMTP:Password"];
            optionsSetup.SmtpUserEmail = Configuration["SMTP:Email"];
            optionsSetup.SmtpHost = Configuration["SMTP:Host"];
            optionsSetup.SmtpPort = Convert.ToInt32(Configuration["SMTP:Port"]);
        });

最后我编辑了我的messageServices,看起来像这样:

        public class AuthMessageSender : IEmailSender, ISmsSender
{
    private readonly IOptions<SmtpConfig> _smtpConfig;
    public IConfiguration Configuration { get; set; }
    public AuthMessageSender( IOptions<SmtpConfig> smtpConfig)
    {
        _smtpConfig = smtpConfig;
    }
    public async Task SendEmailAsync(string email, string subject, string message, string fullName)
    {
        try
        {
            var _email = _smtpConfig.Value.SmtpUserEmail;
            string _epass = _smtpConfig.Value.SmtpPassworrd;
            var _dispName = _smtpConfig.Value.EmailDisplayName;
            var myMessage = new MimeMessage();
            var builder = new BodyBuilder();
            myMessage.To.Add(new MailboxAddress(fullName ?? "User", email));
            myMessage.From.Add(new MailboxAddress(_dispName, _email));
            myMessage.Subject = subject;
            builder.HtmlBody = message;
            myMessage.Body = builder.ToMessageBody();
            using (SmtpClient smtp = new SmtpClient())
            {
                bool UseSSL = true;
                string Host = _smtpConfig.Value.SmtpHost;
                int Port = _smtpConfig.Value.SmtpPort;
                await smtp.ConnectAsync(Host, Port, UseSSL).ConfigureAwait(true);
                smtp.AuthenticationMechanisms.Remove("XOAUTH2");
                smtp.Authenticate(_email, _epass); // Note: only needed if the SMTP server requires authentication
                await smtp.SendAsync(myMessage).ConfigureAwait(true);
                await smtp.DisconnectAsync(true).ConfigureAwait(true);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }