gpt4 book ai didi

c# - 用户认证应该使用哪一层

转载 作者:行者123 更新时间:2023-12-04 12:52:36 25 4
gpt4 key购买 nike

我打算在我的一个应用程序中使用域驱动设计,并且对用户身份验证有一些疑问。

我有一个名为 User 的聚合根,它具有 UserCredentialsPasswordActivationToken 等值对象.我也很少有用于管理用户的域服务。例如 UserRegistration 服务如下所示:

public interface IUserRegistrationService
{
IEnumerable<string> Register(NewUserRequest request);
}

它检查分配给用户注册过程的业务规则,并将用户保存在数据库中。

现在我想对用户进行身份验证,所以我创建了 UserAuthentication 域服务:

public interface UserAuthenticationService
{
IEnumerable<string> Authenticate(AuthRequest request);
}

它从存储库中获取用户、检查业务规则、更新并持久保存用户数据更改,例如 LastLoginDate。

但是我有些怀疑认证过程是属于域本身还是应该属于应用程序服务,至于我的域,用户如何认证并不重要。但另一方面,在此服务中检查的身份验证规则属于我的域规则,因此它们是我域的组成部分。

那么,在基于 DDD 的应用程序中,您将身份验证放在哪里?您对此问题的解决方案是什么?

最佳答案

1.一般情况下,认证和授权是一个应用中的su域。你最好在应用层/核心领域建立一个抽象来隔离它们。

public interface OrderingService// application layer
{
void PlaceOder(Order order) {
//delegate to identity subdomain to validate user request
UserAuthenticationService.Authenticate(ExtractFrom(order));

//delegate to booking core domain to handle core business
BookingService.placeOrder(order);
}
}

2.在Identity子域中,认证算法可以放在基础设施层:

public class OathUserAuthenticationService:UserAuthenticationService //infrastructure layer
{
IEnumerable<string> Authenticate(AuthRequest request) {
......
}
}

Implementing Domain Driven Design 中有很好的讨论和示例.作者单独对身份子域进行身份验证。

关于c# - 用户认证应该使用哪一层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21730464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com