ApplicationSignInManager是否保留成功登录的列表?

时间:2018-01-10 14:17:54

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

我正在使用ApplicationSignInManager来验证用户登录(请参阅下面的代码)。 我接下来要做的是防止重复用户同时登录,同时防止超过10个用户同时登录。

我可以维护我自己的用户当前登录的集合并参考这个,但我想知道ApplicationSignInManager是否自动提供这样的任何功能?

// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
    ViewBag.ReturnUrl = returnUrl;
    return View();
}


public ApplicationSignInManager SignInManager
{
    get
    {
        return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
    }
    private set { _signInManager = value; }
}

//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    // This doesn't count login failures towards account lockout
    // To enable password failures to trigger account lockout, change to shouldLockout: true
    var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
    switch (result)
    {
        case SignInStatus.Success:
            return RedirectToLocal(returnUrl);
        case SignInStatus.LockedOut:
            return View("Lockout");
        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
        case SignInStatus.Failure:
        default:
            ModelState.AddModelError("", "Invalid login attempt.");
            return View(model);
    }
}

1 个答案:

答案 0 :(得分:1)

ApplicationSignInManager不进行任何计数,也不会在内部跟踪已登录的用户。它只是为用户创建身份验证cookie。

您可以在创建Cookie之前为用户重置安全戳,以防止并发登录:

var loggedinUser = await UserManager.FindAsync(model.UserName, model.Password);
if (loggedinUser != null)
{
    // Now user have entered correct username and password.
    // Time to change the security stamp
    await UserManager.UpdateSecurityStampAsync(loggedinUser.Id);
}

// do sign-in AFTER we have done the update of the security stamp, so the new stamp goes into the cookie
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);

我的blog

中有关于此的更多信息

要让有限数量的用户登录,您必须实施自己的计数。但是你决定登录&#34;登录&#34;实际意味着。即有一个打开的浏览器选项卡计入登录?如果该标签未使用5分钟怎么办? 10? 2小时?如果用户在没有按下&#34;退出&#34; - cookie仍然存在,它可能没有过期,但您的应用程序从未收到用户下线的消息。

您需要自己做出这些决定,因为框架中没有任何内容可以为您提供此功能。