gpt4 book ai didi

c# - DDD认证服务

转载 作者:太空狗 更新时间:2023-10-29 23:34:08 25 4
gpt4 key购买 nike

我正在关注 Scott Millet 在 Professional ASP.NET Design Patterns 中的案例研究。在案例研究中,身份验证是在基础设施项目中处理的。它包含类似 AspFormsAuthentication 的实现:IFormsAuthentication、AspMembershipAuthentication:ILocalAuthenticationService。

这很好用,因为他使用的是内置成员(member)提供程序,但我不是,所以我需要访问我的存储库。在我的场景中,将我的 ILocalAuthenticationService 和 AspMembershipAuthentication 实现放在服务项目中不是更好吗?

我在别处问过这个问题,有人回答:

I would still place the functionality to pull credentials in the Infrastructure layer as this layer aligns vertically to the other horizontal layers and all layers have access to it. Since you are not using the ASP.NET Membership provider and may be using something custom that might just use encrypted credentials, you can still use the Infrastructure layer to wrap the access of those credentials and allow the repository to use them when needed. You could have the Services layer get them and pass them down, but then you have too many layers understanding how the data is going to be retrieved/persisted and what authorization access is required which is not good when attempting to layer and separate concerns.

太棒了。这是有道理的。但我不知道从这里去哪里。来自案例研究的代码:

public class AspMembershipAuthentication : ILocalAuthenticationService 
{
public User Login(string email, string password)
{
User user = new User();
user.IsAuthenticated= false;

if (Membership.ValidateUser(email, password))
{
MembershipUser validatedUser = Membership.GetUser(email);
user.AuthenticationToken = validatedUser.ProviderUserKey.ToString();
user.Email = email;
user.IsAuthenticated = true;
}

return user;
}

public User RegisterUser(string email, string password)
{
MembershipCreateStatus status;
User user = new User();
user.IsAuthenticated = false;

Membership.CreateUser(email, password, email,
Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
true, out status);

if (status == MembershipCreateStatus.Success)
{
MembershipUser newlyCreatedUser = Membership.GetUser(email);
user.AuthenticationToken = newlyCreatedUser.ProviderUserKey.ToString();
user.Email = email;
user.IsAuthenticated = true;
}
else
{
switch (status)
{
case MembershipCreateStatus.DuplicateEmail:
throw new InvalidOperationException(
"There is already a user with this email address.");
case MembershipCreateStatus.DuplicateUserName:
throw new InvalidOperationException(
"There is already a user with this email address.");
case MembershipCreateStatus.InvalidEmail:
throw new InvalidOperationException(
"Your email address is invalid");
default:
throw new InvalidOperationException(
"There was a problem creating your account. Please try again.");
}
}

return user;
}
}

如果我不使用成员(member)提供程序,我如何连接到数据库以检查用户名和密码是否匹配,以及其他可能的检查?

最佳答案

在您的基础设施层创建一个实现 ILocalAuthenticationService 的类并进行您需要的调用。

或者,如果 ILocalAuthenticationService 过于 ASP.NET-y(具有其用户返回类型),您可能必须推出自己的 ILocalAuthenticationService 变体并实现它。

然后在需要时使用 IoC 容器解析 ILocalAuthenticationService。

关于c# - DDD认证服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6654966/

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