首页 > 解决方案 > 在 ApplicationUser 中保持用户锁定状态的最佳方法是什么?

问题描述

我想在 Asp.Net Mvc(Core 或 NetFramework)中的 ApplicationUser(继承自 IdentityUser)中保存用户的锁定状态,那么从设计角度来看,哪种实现更好?

1. 分别具有 IsActive 属性和 LockoutReason 枚举属性的用户

class ApplicationUser: IdentityUser
{
    // ...
    public bool IsAcitve {get;  set;}
    public UserLockoutReason LockoutReason {get; set; }
}

enum UserLockoutReason 
{
    NotLocked,
    LockedByAdmin,
    MaximumWrongPasswordAttemptsReached,
    ...
}

2. 具有 ActiveStatus 枚举的用户

class ApplicationUser: IdentityUser
{
    // ...
    public UserActiveStatus ActiveStatus {get; set; }
}

enum UserActiveStatus
{
    Active,
    LockedByAdmin,
    MaximumWrongPasswordAttemptsReached,        
    ...
}

ps:这个设计将成为我们 IdentityProvider 的 UserManagement 的一部分,所以对我们来说实现最好的方式非常重要

标签: c#asp.net-mvcasp.net-core-mvcasp.net-identity

解决方案


我会朝这个方向走:

class ApplicationUser: IdentityUser
{
    // ...
    public UserActiveStatus ActiveStatus {get; set; }
}

enum UserActiveStatus
{
    Active,
    LockedByAdmin,
    MaximumWrongPasswordAttemptsReached,        
    ...
}

由于这ActiveStatus是您需要为用户提供的最终信息,因此需要user.IsActive在 if-else 语句中进行分支,然后获取LockoutReason以防万一它被锁定的帐户将是不必要的


推荐阅读