System.ObjectDisposedException只有occuers有时候其他人工作正常

时间:2018-04-27 09:03:36

标签: c# asp.net-mvc entity-framework

在我的aproch中,为了跟踪用户在网络应用中的操作,我只是在输入了错误的密码并且我无法发现如何解决这个问题时陷入了这个错误。

代码在语句_context.UsersActivity.Add(this);之后中断,并且只有当用户输入错误的密码并且仅在我将方法“SaveUserActivity”从控制器移动到类之后才会附加,之后我将其写入控制器并工作细

任何帮助都将被解雇。

我的控制器代码:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
    ViewData["ReturnUrl"] = returnUrl;
    if (ModelState.IsValid)
    {
        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, set lockoutOnFailure: true
        var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);
        if (result.Succeeded)
        {
            await SaveUserActivity(model);
            return RedirectToLocal("~/Users/Index");
        }
        if (result.RequiresTwoFactor)
        {
            return RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe });
        }
        if (result.IsLockedOut)
        {
            _logger.LogWarning("User account locked out.");
            return RedirectToAction(nameof(Lockout));
        }
        else
        {
            if (await IfUserExist(model))
                PasswordFailure(model);

            ModelState.AddModelError(string.Empty, "Log In Failure.");
            return View(model);
        }
    }

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

private async Task SaveUserActivity(LoginViewModel model, [CallerMemberName] string callerName = "")
{
    string Action = null;
    var RegActivity = new UserActivity();

    switch (callerName)
        {
        case "Login":
            Action = "Log In";
            break;
        case "IfUserExist":
            Action = "Log In attempt from no exist user.";
            break;
        case "PasswordFailure":
            Action = "Log In attempt with wrong password.";
            break;
    }

    await RegActivity.SaveUserActivity(_context, _userManager, model.UserName, Action);
}

private async Task<bool> IfUserExist(LoginViewModel model)
{
    var LoginAttemp = await _userManager.FindByNameAsync(model.UserName);
    if (LoginAttemp == null)
    {
        ViewData["Page"] = new PageSettings("AccountControllerLoginPostInvalidUser");
        await SaveUserActivity(model);
        return false;
    }
    return true;
}

private async void PasswordFailure(LoginViewModel model)
{
    ViewData["Page"] = new PageSettings("AccountControllerLoginPostInvalidPass");
    await SaveUserActivity(model);
}

我班上保存用户活动的代码就是这个:

public async Task SaveUserActivity(SiteDbContext _context, UserManager<SiteUser> _userManager, string _UserName, string _Action)
{

    var user = await _userManager.FindByNameAsync(_UserName);
    var status = (user == null || _Action.Contains("password"));
    var roles = (status) ? null : await _userManager.GetRolesAsync(user);

    UserName = _UserName;
    Roles = (status) ? null : roles;
    TimeStamp = DateTime.Now;
    Action = _Action;
    Role = (status) ? null : TransformListOfRolesToString();

    _context.UsersActivity.Add(this);
    _context.SaveChanges();

}

0 个答案:

没有答案