代码之家  ›  专栏  ›  技术社区  ›  zzdhxu

.NET MVC核心发送网格电子邮件验证不工作

  •  0
  • zzdhxu  · 技术社区  · 7 年前

    但是,按照下面提供的Microsoft教程,仍然无法发送电子邮件。

    https://docs.microsoft.com/en-us/aspnet/core/security/authentication/accconfirm?view=aspnetcore-2.2&tabs=visual-studio

    我花了一些时间调试,发现问题是由于下面的代码为sendgriduser和sendgridkey返回了空值。有人知道如何解决这个问题吗?谢谢您!

    Options = optionsAccessor.Value;
    

    分泌物

    {
      "SendGridUser": "apiKeyName",
      "SendGridKey": "<ApiKey>"
    }
    

    enter image description here

    电子邮件地址

    using Microsoft.AspNetCore.Identity.UI.Services;
    using Microsoft.Extensions.Options;
    using SendGrid;
    using SendGrid.Helpers.Mail;
    using System.Threading.Tasks;
    using ProjectName.Areas.Identity.Services;
    
    namespace ProjectName.Services
    {
        public class EmailSender : IEmailSender
        {
            public EmailSender(IOptions<AuthMessageSenderOptions> optionsAccessor)
            {
                Options = optionsAccessor.Value;
            }
    
            public AuthMessageSenderOptions Options { get; } //set only via Secret Manager
    
            public Task SendEmailAsync(string email, string subject, string message)
            {
                return Execute(Options.SendGridKey, subject, message, email);
            }
    
            public Task Execute(string apiKey, string subject, string message, string email)
            {
                var client = new SendGridClient(apiKey);
                var msg = new SendGridMessage()
                {
                    From = new EmailAddress("Joe@contoso.com", "Joe Smith"),
                    Subject = subject,
                    PlainTextContent = message,
                    HtmlContent = message
                };
                msg.AddTo(new EmailAddress(email));
    
                // Disable click tracking.
                // See https://sendgrid.com/docs/User_Guide/Settings/tracking.html
                msg.SetClickTracking(false, false);
    
                return client.SendEmailAsync(msg);
            }
        }
    }
    

    授权消息发送选项.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace ProjectName.Areas.Identity.Services
    {
        public class AuthMessageSenderOptions
        {
            public string SendGridUser { get; set; }
            public string SendGridKey { get; set; }
        }
    }
    

    标识hostingstartup.cs

    using System;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Identity.UI;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using ProjectName.Data;
    
    [assembly: HostingStartup(typeof(ProjectName.Areas.Identity.IdentityHostingStartup))]
    namespace ProjectName.Areas.Identity
    {
        public class IdentityHostingStartup : IHostingStartup
        {
            public void Configure(IWebHostBuilder builder)
            {
                builder.ConfigureServices((context, services) => {
    
                    services.AddDefaultIdentity<IdentityUser>(config =>
                    {
                        config.SignIn.RequireConfirmedEmail = true;
                    })
                    .AddEntityFrameworkStores<ApplicationDbContext>();
    
                });
            }
        }
    }
    

    从startup.cs配置 已将Services.AddIdentity注释掉,因为它导致与IdentityHostingStartup冲突

    //services.AddIdentity<IdentityUser, IdentityRole>()
    //    .AddEntityFrameworkStores<ApplicationDbContext>()
    //    .AddDefaultTokenProviders();        
    
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddRazorPagesOptions(options =>
            {
                options.AllowAreas = true;
                options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
                options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
            });
    
            services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath = $"/Identity/Account/Login";
                options.LogoutPath = $"/Identity/Account/Logout";
                options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
            });
    
            // using Microsoft.AspNetCore.Identity.UI.Services;
            //services.AddSingleton<IEmailSender, EmailSender>();
    
            // requires
            services.AddSingleton<IEmailSender, EmailSender>();
            services.Configure<AuthMessageSenderOptions>(Configuration);
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   zzdhxu    7 年前

    对不起,这是我的错。一切都是正确的。

    下面的代码被注释掉并替换为从其他地方读取环境变量,因此这些值为空。非常感谢你!

    public Startup(IConfiguration configuration)
           {
               Configuration = configuration;
           }
    
    推荐文章