You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core MVC Identity注册后点击确认邮件链接无法完成邮箱验证的问题排查求助

ASP.NET Core MVC Identity注册后点击确认邮件链接无法完成邮箱验证的问题排查求助

我现在遇到一个Identity邮箱确认的棘手问题,折腾了好一阵没找到根源,想请教下大家:怎么在不脚手架展开所有Identity页面的情况下排查这个问题?

先说说我的场景和已做的配置:
我开发了一个ASP.NET Core MVC网站,采用带Microsoft Identity的Individual Accounts模板,全程没有脚手架展开或自定义任何Identity的网页/控制器,完全依赖默认的密封DLL实现,注册、密码修改这些基础功能都正常工作。

为了添加邮箱确认,我按照官方思路做了这些配置:

  1. 因为新版IEmailSender需要指定模板类型,所以在Program.cs里注册了邮件服务:
builder.Services.AddTransient<IEmailSender<AppUser>, EmailSender>();
builder.Services.Configure<SmtpAccount>(builder.Configuration.GetSection("Smtp"));
  1. 实现了自定义的AppUser类继承IdentityUser:
public class AppUser : IdentityUser
{
    public AppUser() { }
    public AppUser(string userName) : base(userName) { }

    public virtual ICollection<IdentityUserRole<string>> Roles { get; set; }
    public virtual ICollection<IdentityUserClaim<string>> Claims { get; set; }
}
  1. 在Program.cs里配置了Identity核心服务,要求必须确认账号才能登录:
builder.Services.AddDefaultIdentity<AppUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddRoles<AppRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

现在遇到的问题:

  • 注册新用户后,系统能正常发送带确认链接的邮件,后端AspNetUsers表里新用户的EmailConfirmed字段确实是False
  • 我核对过邮件里的链接,userId是正确的,比如:https://localhost:7091/Identity/Account/ConfirmEmail?userId=c03411ce-dee8-4e89-a3a0-35003fc541e4&code=Q2ZESjhOQm1qYjNLUFl4SXBsMVZKMWRiOFVjKzFa
  • 但点击这个链接后,直接跳转到网站首页(https://localhost:7091/?page=%2FIndex),用户的EmailConfirmed字段依然是False,也无法登录系统

我现在的核心疑问:

  1. 我不想为了排查这个问题就把所有Identity的页面和控制器都脚手架出来,有没有轻量的排查方法?
  2. 官方文档提到:"非脚手架的RegisterConfirmation会自动检测DI容器里是否注册了IEmailSender",我怎么验证它确实正确识别到了我的IEmailSender<AppUser>实现?

附Program.cs完整关键代码:

public static bool isDevelopment = false;
public static void Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);

    // 配置数据库连接
    string connectionString = builder.Configuration.GetConnectionString("LocalAuthConnection");
    if (String.IsNullOrEmpty(connectionString))
    {
        throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
    }

    builder.Services.AddDbContext<ApplicationDbContext>(options =>
        options.UseNpgsql(connectionString));

    // 注册连接字符串单例服务
    ConnectionStringContainer connContainer = new ConnectionStringContainer(connectionString);
    builder.Services.AddSingleton<IDbConnectionString> (connContainer);

    // 配置Identity服务
    builder.Services.AddDefaultIdentity<AppUser>(options => options.SignIn.RequireConfirmedAccount = true)
        .AddRoles<AppRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>();

    // 注册邮件服务
    builder.Services.AddTransient<IEmailSender<AppUser>, EmailSender>();
    builder.Services.Configure<SmtpAccount>(builder.Configuration.GetSection("Smtp"));

    builder.Services.AddControllersWithViews();

    var app = builder.Build();

    // 配置HTTP请求管道
    if (app.Environment.IsDevelopment())
    {
        isDevelopment = true;
        app.UseDeveloperExceptionPage();
    }
    else
    {
        isDevelopment = false;
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseAuthorization();

    // 配置路由
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Manage}/{action=ManageUsers}/{id?}");

    app.MapRazorPages();

    app.Run();
}

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 06:53:00