DDD身份验证服务

时间:2011-07-11 18:51:37

标签: c# asp.net-mvc domain-driven-design

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

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

我在其他地方问了这个,有人回答说:

  

我仍然会将功能放在Infrastructure层中,因为此图层垂直对齐其他水平图层,并且所有图层都可以访问它。由于您没有使用ASP.NET成员资格提供程序,并且可能正在使用可能仅使用加密凭据的自定义内容,因此您仍然可以使用Infrastructure层来包装这些凭据的访问权限,并允许存储库在需要时使用它们。你可以让服务层获取它们并将它们传递下来,但是你有太多的层来理解如何检索/持久化数据以及需要什么样的授权访问,这在尝试分层和分离关注点时并不好。 / p>

大。这是有道理的。但我不知道从哪里开始。案例研究的代码:

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;
    }       
}

如果我没有使用会员资格提供者,我如何连接到数据库以检查用户名和密码是否匹配,以及其他可能的检查?

1 个答案:

答案 0 :(得分:3)

在您的基础架构层中创建一个实现ILocalAuthenticationService的类,并进行所需的调用。

或者,如果ILocalAuthenticationService太ASP.NET-y(使用其用户返回类型),您可能必须滚动自己的ILocalAuthenticationService变体并实现它。

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

相关问题