C# 在MVC中使用GenerateEmailConfirmationTokenAsync外部控制器的令牌无效
这件事我已经坚持了好几天了 我正在使用C# 在MVC中使用GenerateEmailConfirmationTokenAsync外部控制器的令牌无效,c#,asp.net,asp.net-mvc,asp.net-identity,C#,Asp.net,Asp.net Mvc,Asp.net Identity,这件事我已经坚持了好几天了 我正在使用generateemailconfimationtokenasync在控制器外创建令牌(工作正常),但不知何故,我的令牌比使用generateemailconfimationtokenasync在控制器内创建的令牌长,因此ConfirmEmail操作拒绝该令牌。(错误:无效令牌)。 我已经在web.config,HttpUtility.UrlEncode上尝试了Machinekey,但我仍然被卡住了 如何对控制器上的无效令牌错误进行排序ConfirmEmai
generateemailconfimationtokenasync
在控制器外创建令牌(工作正常),但不知何故,我的令牌比使用generateemailconfimationtokenasync
在控制器内创建的令牌长,因此ConfirmEmail
操作拒绝该令牌。(错误:无效令牌
)。
我已经在web.config
,HttpUtility.UrlEncode
上尝试了Machinekey
,但我仍然被卡住了
如何对控制器上的无效令牌错误进行排序ConfirmEmail
这是我的密码:
注册服务器(外部控制器)
ConfirmEmail
在身份控制器(帐户控制器)中-我创建了帐户而不是帐户控制器,但它工作正常
//
// GET: /Account/ConfirmEmail
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(string userId, string code)
{
if (userId == null || code == null)
{
return View("Error");
}
var confirmed = await UserManager.IsEmailConfirmedAsync(userId);
if (confirmed)
{
return RedirectToLocal(userId);
}
var result = await UserManager.ConfirmEmailAsync(userId, code); //Here I get the error (Token Invlaid, despite the token and userId being displayed)
if (result.Succeeded)
{
ViewBag.userId = userId;
ViewBag.code = code;
}
return View(result.Succeeded ? "ConfirmEmail" : "Error");
}
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(SetPasswordViewModel model, string userId, string code)
{
if (userId == null || code == null)
{
return View("Error");
}
if (!ModelState.IsValid)
{
return View(model);
}
var result = await UserManager.AddPasswordAsync(userId, model.NewPassword);
if (result.Succeeded)
{
var user = await UserManager.FindByIdAsync(userId);
if (user != null)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
}
return RedirectToLocal(userId);
}
ViewBag.userId = userId;
ViewBag.code = code;
AddErrors(result);
return View(model);
}
//
//获取:/Account/ConfirmEmail
[异名]
公共异步任务确认邮件(字符串用户ID、字符串代码)
{
if(userId==null | | code==null)
{
返回视图(“错误”);
}
var confirm=wait UserManager.IsEmailConfirmedAsync(userId);
如果(已确认)
{
返回重定向到本地(userId);
}
var result=await UserManager.confirmemailsync(userId,code);//这里我得到了错误(尽管显示了令牌和userId,但仍显示了令牌invlay)
if(result.successed)
{
ViewBag.userId=用户ID;
ViewBag.code=代码;
}
返回视图(result.successed?“确认邮件”:“错误”);
}
[HttpPost]
[ValidateAntiForgeryToken]
[异名]
公共异步任务确认邮件(SetPasswordViewModel模型、字符串用户ID、字符串代码)
{
if(userId==null | | code==null)
{
返回视图(“错误”);
}
如果(!ModelState.IsValid)
{
返回视图(模型);
}
var result=await UserManager.AddPasswordAsync(userId,model.NewPassword);
if(result.successed)
{
var user=await UserManager.FindByIdAsync(userId);
如果(用户!=null)
{
等待SignInManager.SignInAsync(用户,isPersistent:false,rememberBrowser:false);
}
返回重定向到本地(userId);
}
ViewBag.userId=用户ID;
ViewBag.code=代码;
加法器(结果);
返回视图(模型);
}
我已经在这个代码中工作了好几个小时,但直到现在我还不能整理它。
感谢您的任何意见或解决方案。采用这种方法的原因是我必须使用任务调度器(我使用的是fluentscheduler,工作正常)。您的问题在这一行:
var provider = new DpapiDataProtectionProvider("MyApp");
UserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(
provider.Create("EmailConfirmation"));
然后在UserManager中访问该引用,如下所示:
public class UserManager : UserManager<ApplicationUser>
{
public UserManager() : base(new UserStore<ApplicationUser>(new MyDbContext()))
{
var dataProtectionProvider = Startup.DataProtectionProvider;
this.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
// do other configuration
}
}
公共类UserManager:UserManager
{
public UserManager():base(新的UserStore(新的MyDbContext()))
{
var dataProtectionProvider=Startup.dataProtectionProvider;
this.UserTokenProvider=
新的DataProtectorTokenProvider(dataProtectionProvider.Create(“ASP.NET标识”);
//执行其他配置
}
}
但是,我不熟悉FluentScheduler的详细信息,如果它在单独的AppDomain中启动进程,它可能不允许您访问此静态变量。但请尝试一下,看看它是如何工作的。谢谢trailmax,您的回答对我有一定帮助,因为我能够创建一个更像在controller中创建的令牌(长度相同),这很好,但不幸的是,在验证EmailConfirm action(get)上的电子邮件时,我仍然收到“无效令牌”错误。我获得Id和令牌,但令牌无效。我尝试了HttpUtility.UrlEncode,但仍然卡住了。你对此有什么想法吗。Thanks@prezequias检查数据库中的用户记录-在
SecurityStamp
字段中是否有任何值?是的,它有Security Stamp Valuetrailmax,您的帮助非常棒。它正在工作。你救了我的命,谢谢
//
// GET: /Account/ConfirmEmail
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(string userId, string code)
{
if (userId == null || code == null)
{
return View("Error");
}
var confirmed = await UserManager.IsEmailConfirmedAsync(userId);
if (confirmed)
{
return RedirectToLocal(userId);
}
var result = await UserManager.ConfirmEmailAsync(userId, code); //Here I get the error (Token Invlaid, despite the token and userId being displayed)
if (result.Succeeded)
{
ViewBag.userId = userId;
ViewBag.code = code;
}
return View(result.Succeeded ? "ConfirmEmail" : "Error");
}
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(SetPasswordViewModel model, string userId, string code)
{
if (userId == null || code == null)
{
return View("Error");
}
if (!ModelState.IsValid)
{
return View(model);
}
var result = await UserManager.AddPasswordAsync(userId, model.NewPassword);
if (result.Succeeded)
{
var user = await UserManager.FindByIdAsync(userId);
if (user != null)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
}
return RedirectToLocal(userId);
}
ViewBag.userId = userId;
ViewBag.code = code;
AddErrors(result);
return View(model);
}
var provider = new DpapiDataProtectionProvider("MyApp");
UserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(
provider.Create("EmailConfirmation"));
public partial class Startup
{
internal static IDataProtectionProvider DataProtectionProvider { get; private set; }
public void ConfigureAuth(IAppBuilder app)
{
DataProtectionProvider = app.GetDataProtectionProvider();
// other stuff.
}
}
public class UserManager : UserManager<ApplicationUser>
{
public UserManager() : base(new UserStore<ApplicationUser>(new MyDbContext()))
{
var dataProtectionProvider = Startup.DataProtectionProvider;
this.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
// do other configuration
}
}