【发布时间】:2021-12-05 06:53:24
【问题描述】:
我想在我的应用程序(Asp.Net Core 2.2)中创建一个忘记密码系统。我成功生成了一个令牌。但问题是当我生成 passwordResetLink 时它为空。
var passwordResetLink = Url.Action("ResetPassword", "Account",
new { email = model.Email, token = token }, Request.Scheme);
我不明白我的email 和token 不为空,但为什么passwordResetLink 为空?
这是我的所有代码:-
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await userManager.FindByEmailAsync(model.Email);
if (user != null)
{
// Generate the reset password token
var token = await userManager.GeneratePasswordResetTokenAsync(user);
var passwordResetLink = Url.Action("ResetPassword", "Account",
new { email = model.Email, token = token }, Request.Scheme); //here is the main problem.why it's null?
return View("ForgotPasswordConfirmation");
}
return View("ForgotPasswordConfirmation");
}
return View(model);
}
Startup.cs 文件(ConfigureServices):-
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IBraintreeService, BraintreeService>();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddSession(options =>
{
// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromMinutes(10);
//options.Cookie.HttpOnly = true;
// Make the session cookie essential
options.Cookie.IsEssential = true;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultUI(UIFramework.Bootstrap4).AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
这是我的输出:-
【问题讨论】:
-
Url.Action()如果找不到与提供的参数匹配的路由,将返回 null。 你有一个AccountController和一个ResetPassword操作方法,接受一个email和token参数吗? -
@CodeCaster 感谢您的回复。我有 AccountController 和 ResetPassword 方法。我也在我的项目中使用了
Area。 -
AccountController在哪个区域?
-
@CodeCaster 我的区域名称是
Display1 -
然后使用
new { email = model.Email, token = token , area = "Display1" }。
标签: c# asp.net-mvc asp.net-core asp.net-core-mvc asp.net-core-identity