执行当前Web请求时出现未处理的异常,无效的列名称' UserId'

时间:2015-12-15 10:41:14

标签: c# .net asp.net-mvc-4

在我根据我的EF模型创建新数据库之后,使用标准的微软帐户控制器和模型,我的MVC 4项目中的授权存在问题。注册通过正常,但如果我尝试使用我的凭据登录,我总是会收到此错误(请参阅下面的内容)。怎么了?现在我的模型中有UserIdRoleId等字段。错误始终在线:

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

错误: enter image description here

1 个答案:

答案 0 :(得分:0)

[使用AllowAnonymous]

    public ActionResult Login(string ReturnUrl)

    {
        var viewmodel = new LoginModel { strReturnUrl = ReturnUrl };
        return View();
    }
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginModel viewmodel, bool remember = false)
    {
        if (ModelState.IsValid)
        {
            var user = _user.GetAll(a => a.Username == viewmodel.UserName && a.Password == viewmodel.Password && a.IsActive == true && a.IsDeleted != true).FirstOrDefault();
            if (user != null)
            {
                await SignInAsync(user, remember);
                return RedirectToLocal(viewmodel.strReturnUrl, user);
            }
        }
        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "The email or password provided is incorrect.");
        return View(viewmodel);
    }

    private async Task SignInAsync(User user, bool isPersistent)
    {
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
        AccountHelper.SignIn(user, isPersistent);
    }

    private IAuthenticationManager AuthenticationManager
    {
        get
        {
            return HttpContext.GetOwinContext().Authentication;
        }
    }

    private ActionResult RedirectToLocal(string returnUrl, Dental_testData.User user)
    {
        if (Url.IsLocalUrl(returnUrl))
        {
            return Redirect(returnUrl);
        }
        else
        {
            if (user.RoleId == (int)common.Role.Customer)
            {
                return RedirectToAction("Index", "Home", new { @area = "Customer" });
            }
            return RedirectToAction("Index", "Home");
        }
    }