【问题标题】:Url.Action() causes System.AccessViolationExceptionUrl.Action() 导致 System.AccessViolationException
【发布时间】:2015-11-19 05:54:03
【问题描述】:

我在标准 ASP.NET MVC 项目中向 ASP.NET 身份模型添加了电子邮件验证。以下行导致 AccessViolationException:

callbackUrl = Url.Action("ConfirmEmail", "Account", confirmModel, Request.Url.Scheme);

更新: 问题消失了,但无法解释。我会试着弄清楚是什么让它消失了。令我担心的是,我不知道解决方案有任何重大变化。

注册用户的完整账户控制器方法如下:

// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        ApplicationUser user = new ApplicationUser { UserName = model.Email, Email = model.Email };
        IdentityResult result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

            string callbackUrl;
            try
            {
                string requestScheme = Request.Url.Scheme;
                object confirmModel = new { userId = user.Id, code = code };
                callbackUrl = Url.Action("ConfirmEmail", "Account", confirmModel, Request.Url.Scheme); // TODO: Fails somehow!
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                callbackUrl = "https://localhost:43000/Account/ConfirmEmail?userid=" + user.Id + "&code=" + code;
            }

            await
                UserManager.SendEmailAsync(
                        userId: user.Id,
                        subject: "Verify it's you",
                        body: "Please confirm your email address by clicking <a href=\"" + callbackUrl + "\">here</a>");

            return View("CheckYourEmail");
        }

        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

不幸的是,没有内部异常或任何有用的东西。

catch() 块作为一种解决方法解决了这个问题。
但我真的很好奇这里出了什么问题。

【问题讨论】:

  • 您能否将ex.ToString() 的整个输出的确切文本复制并粘贴到您的问题中?
  • 异常中的堆栈跟踪是什么样的?

标签: c# asp.net-mvc asp.net-identity-2


【解决方案1】:

不要设置对象confirmModel = ...,而是使用“动态confirmModel = ...”,看看是否可以修复它。

【讨论】:

    猜你喜欢
    • 2013-07-18
    • 2017-05-31
    • 1970-01-01
    • 2011-09-05
    • 2019-09-08
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 2022-08-03
    相关资源
    最近更新 更多