get*_*ory 5 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层来包装这些凭据的访问权限,并允许存储库在需要时使用它们.你可以有服务层得到他们,并通过他们失望,但你有太多层理解的数据将如何被检索/不退,需要什么样的授权访问尝试层和单独的担忧时,这是不好的.
大.这是有道理的.但我不知道从哪里开始.案例研究的代码:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我没有使用会员提供商,我如何连接到数据库以检查用户名和密码是否匹配,以及其他可能的检查?
在您的基础设施层中创建一个实现 ILocalAuthenticationService 的类并进行您需要的调用。
或者,如果 ILocalAuthenticationService 太 ASP.NET-y(及其 User 返回类型),您可能必须推出自己的 ILocalAuthenticationService 变体并实现它。
然后在需要时使用 IoC 容器来解析 ILocalAuthenticationService。
| 归档时间: |
|
| 查看次数: |
2085 次 |
| 最近记录: |