简体   繁体   English

Url.Action()导致System.AccessViolationException

[英]Url.Action() causes System.AccessViolationException

I added e-mail verification to ASP.NET identity model within a standard ASP.NET MVC project. 我在标准的ASP.NET MVC项目中向ASP.NET身份模型添加了电子邮件验证。 The following line causes an AccessViolationException: 以下行导致AccessViolationException:

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

UPDATE: As unexplainable as the problem was it disappeared. 更新: 由于问题无法解释,它就消失了。 I will try to figure out what made it go away. 我会试着弄清楚是什么让它消失了。 To my concern I am not aware of any dramatic changes to the solution. 令我担心的是,我不知道解决方案有任何重大变化。

The complete account controller method for registering users looks like that: 用于注册用户的完整帐户控制器方法如下所示:

// 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);
}

Unfortunately there is no inner exception or anything useful. 不幸的是,没有内部异常或任何有用的东西。

The catch() block solves the problem as a workaround. catch()块作为一种解决方法解决了这个问题。
But I am really curious what is going wrong here. 但我真的很好奇这里出了什么问题。

而不是设置它对象confirmModel = ...,使用“dynamic confirmModel = ...”,看看是否修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM