我有一个需要身份验证算法的基本抽象类。我有两个实现,一个将散列密码并将其与存储的散列进行比较,另一个将使用 Windows 活动目录。
但在实际进行哈希检查或 Windows 身份验证之前,我有绝对必须实现的额外工作流逻辑。因此,无论身份验证的算法如何,都必须始终以相同的方式修改或使用诸如 failedPasswordCount、lastAuthenticationAttemptDate、IsUserApproved 等内容。
在我看来,这个问题似乎可以使用模板方法模式来解决,只是实现我的两种身份验证方法所需的信息因使用的一种而不同。
public abstract class User
{
public bool Authenticate() // my template method
{
lastAuthenticationAttemptDate = DateTime.UtcNow();
if(IsUserApproved)
{
if(DoAuthenticate()) // implemented by childs
{
return true;
}
else
{
failedPasswordCount++;
return false;
}
}
}
public abstract DoAuthenticate();
}
public UserWindowsLogon : User
{
public override bool DoAuthenticate(string windowsDomain, string password)
{
...
}
}
public UserApplicationLogon : User
{
public override bool DoAuthenticate(string password)
{
...
}
}
解决此问题的最佳方法是什么?是否有另一种已知模式已经解决了这个问题?或者任何人有一个好主意?