Can we use the ASP.NET identifier in a Driven Driven project?

Our team decided to use Driven Driven architecture architecture for our project. Now the discussion continues, " can I use the ASP.NET identifier in DDD ?"

Are there any flaws in using the ASP.NET identifier in the DDD design.

I hesitate to make a decision.

I was looking for him, but I had no idea.

Any help would be noticeable.

+7
c # asp.net-mvc domain-driven-design
source share
3 answers

Questions reveal several misconceptions:

It seems that you perceive the domain model as some monolithic model in which you put each part of the application. Instead, focus on strategic templates to distinguish between limited contexts. Consider the region as a composition of several weakly interconnected components. Then determine what your primary domain is and apply tactical DDD models. Not every component requires DDD. Some of them should not even use DDD. Especially common domains such as authentication.

DDD is a technological agnostic (to some extent), so yes, you can use the ASP.NET identifier or any other library you like.

Authentication usually refers to the application tier, not the domain.

However, if a user / client / person concept exists in your domain, you may need to use the identifier provided by the identity component. But you must understand that the value of User in your limited context is different from the value of the User in Identity component. This is not the same concept. Although they both refer to the same individual who sits somewhere and clicks on your GUI application, they are two different models (or forecasts) that serve different purposes. Therefore, you should not just reuse the ASP.NET user class in a limited context.

Instead, individual contexts should interact through an anti-corruption layer. Basically you have to do some service (interface only) in a limited context that creates context-specific user objects. Implementation of this interface, done at the infrastructure level, will become an ASP.NET Identity wrapper that will receive the ASP.NET Identity user and create the corresponding limited context user.

+14
source share

You can use whatever you like. But be careful with the pollution that will be made by a specific decision. If your domain model is messed up with hundreds of lines of the technical type asp.net, which makes your domain logic hard to read, and you miss the DDD point.

In an ideal situation, your domain model should depend only on the programming language.

Also, you might find something useful from my long-standing implementation of code related to a user session .

+1
source share

I am new to DDD. But I have achieved integration with Identity and DDD. Despite the fact that I doubt that this really applies to DDD members.

I started with Entity:

public partial class User : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager) { var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here return userIdentity; } /// <summary> /// The Date the User Registered for general information purposes /// </summary> public DateTime DateRegistered { get; set; } } 

Then the interface:

 public interface IUserRepository:IBaseRepository<User> { /// <summary> /// Register User to Identity Database /// </summary> /// <param name="userManager">User Manager to Handle Registration</param> /// <param name="user">User to add to database</param> /// <param name="password">User password</param> /// <returns>Identity Result</returns> Task<IdentityResult> Register(UserManager<User, string> userManager, User user, string password); /// <summary> /// Login User /// </summary> /// <param name="signinManager">Signin Manager to handle login</param> /// <param name="email">Email of user</param> /// <param name="password">Password of user</param> /// <param name="rememberMe">Boolean if the user wants to be remembered</param> /// <returns>SignIn Status</returns> Task<SignInStatus> Login(SignInManager<User, string> signinManager, string email, string password, bool rememberMe); /// <summary> /// Verify that code sent to User is valid /// </summary> /// <param name="signinManager">Signin Manager to handle verification</param> /// <param name="provider">Provider of the code</param> /// <param name="code">The code</param> /// <param name="rememberMe">Boolean if user wants to be remembered</param> /// <param name="rememberBrowser">Boolean if browser should be remembered</param> /// <returns>SignIn Status</returns> Task<SignInStatus> VerifyCode(SignInManager<User, string> signinManager, string provider, string code, bool rememberMe, bool rememberBrowser); /// <summary> /// Confirm email of User /// </summary> /// <param name="userManager">User Manager to handle confirmation</param> /// <param name="userId">String user Id of the User</param> /// <param name="code">User code sent in Email</param> /// <returns>Identity Result</returns> Task<IdentityResult> ConfirmEmail(UserManager<User, string> userManager, string userId, string code); void ForgotPassword(); void ForgotPasswordConfirmation(); void ResetPassword(); void ResetPasswordConfirmation(); void ExternalLogin(); void SendCode(); void ExternalLoginCallback(); void ExternalLoginConfirmation(); /// <summary> /// Log off user from the Application /// </summary> /// <param name="AuthenticationManager">Application Manager to handle Sign out</param> void Logoff(IAuthenticationManager AuthenticationManager); /// <summary> /// Get user based on their Email /// </summary> /// <param name="Email">Email of user</param> /// <returns>User</returns> User GetUser(string Email); /// <summary> /// Get User by their GUID /// </summary> /// <param name="ID">GUID</param> /// <returns>User</returns> User GetUserById(string ID); } 

Then the repository:

 public class UserRepository : BaseRepository<User>, IUserRepository { /// <summary> /// Confirm email of User /// </summary> /// <param name="userManager">User Manager to handle confirmation</param> /// <param name="userId">String user Id of the User</param> /// <param name="code">User code sent in Email</param> /// <returns>Identity Result</returns> public async Task<IdentityResult> ConfirmEmail(UserManager<User, string> userManager, string userId, string code) => await userManager.ConfirmEmailAsync(userId, code); public void ExternalLogin() { throw new NotImplementedException(); } public void ExternalLoginCallback() { throw new NotImplementedException(); } public void ExternalLoginConfirmation() { throw new NotImplementedException(); } public void ForgotPassword() { throw new NotImplementedException(); } public void ForgotPasswordConfirmation() { throw new NotImplementedException(); } /// <summary> /// Get user based on their Email /// </summary> /// <param name="Email">Email of user</param> /// <returns>User</returns> public User GetUser(string Email) => _context.Users.Where(p => p.Email == Email).FirstOrDefault(); /// <summary> /// Get User by their GUID /// </summary> /// <param name="ID">GUID</param> /// <returns>User</returns> public User GetUserById(string ID) => _context.Users.Find(ID); /// <summary> /// Login User /// </summary> /// <param name="signinManager">Signin Manager to handle login</param> /// <param name="email">Email of user</param> /// <param name="password">Password of user</param> /// <param name="rememberMe">Boolean if the user wants to be remembered</param> /// <returns>SignIn Status</returns> public async Task<SignInStatus> Login(SignInManager<User, string> signinManager, string email, string password, bool rememberMe) => await signinManager.PasswordSignInAsync(email, password, rememberMe, shouldLockout: false); /// <summary> /// Log off user from the Application /// </summary> /// <param name="AuthenticationManager">Application Manager to handle Sign out</param> public void Logoff(IAuthenticationManager AuthenticationManager) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); } /// <summary> /// Register User to Identity Database /// </summary> /// <param name="userManager">User Manager to Handle Registration</param> /// <param name="user">User to add to database</param> /// <param name="password">User password</param> /// <returns>Identity Result</returns> public async Task<IdentityResult> Register(UserManager<User, string> userManager, User user, string password) => await userManager.CreateAsync(user, password); public void ResetPassword() { throw new NotImplementedException(); } public void ResetPasswordConfirmation() { throw new NotImplementedException(); } public void SendCode() { throw new NotImplementedException(); } /// <summary> /// Verify that code sent to User is valid /// </summary> /// <param name="signinManager">Signin Manager to handle verification</param> /// <param name="provider">Provider of the code</param> /// <param name="code">The code</param> /// <param name="rememberMe">Boolean if user wants to be remembered</param> /// <param name="rememberBrowser">Boolean if browser should be remembered</param> /// <returns>SignIn Status</returns> public async Task<SignInStatus> VerifyCode(SignInManager<User, string> signinManager, string provider, string code, bool rememberMe, bool rememberBrowser) => await signinManager.TwoFactorSignInAsync(provider, code, isPersistent: rememberMe, rememberBrowser: rememberBrowser); } 

IService:

 public interface IUserService { /// <summary> /// Register User to Identity Database /// </summary> /// <param name="userManager">User Manager to Handle Registration</param> /// <param name="user">User to add to database</param> /// <param name="password">User password</param> /// <returns></returns> Task<IdentityResult> Register(UserManager<User, string> userManager, User user, string password); /// <summary> /// Login User /// </summary> /// <param name="signinManager">Signin Manager to handle login</param> /// <param name="email">Email of user</param> /// <param name="password">Password of user</param> /// <param name="rememberMe">Boolean if the user wants to be remembered</param> /// <returns></returns> Task<SignInStatus> Login(SignInManager<User, string> signinManager, string email, string password, bool rememberMe); /// <summary> /// Verify that code sent to User is valid /// </summary> /// <param name="signinManager">Signin Manager to handle verification</param> /// <param name="provider">Provider of the code</param> /// <param name="code">The code</param> /// <param name="rememberMe">Boolean if user wants to be remembered</param> /// <param name="rememberBrowser">Boolean if browser should be remembered</param> /// <returns></returns> Task<SignInStatus> VerifyCode(SignInManager<User, string> signinManager, string provider, string code, bool rememberMe, bool rememberBrowser); /// <summary> /// Confirm email of User /// </summary> /// <param name="userManager">User Manager to handle confirmation</param> /// <param name="userId">String user Id of the User</param> /// <param name="code">User code sent in Email</param> /// <returns></returns> Task<IdentityResult> ConfirmEmail(UserManager<User, string> userManager, string userId, string code); void ForgotPassword(); void ForgotPasswordConfirmation(); void ResetPassword(); void ResetPasswordConfirmation(); void ExternalLogin(); void SendCode(); void ExternalLoginCallback(); void ExternalLoginConfirmation(); /// <summary> /// Log off user from the Application /// </summary> /// <param name="AuthenticationManager">Application Manager to handle Sign out</param> void Logoff(IAuthenticationManager AuthenticationManager); /// <summary> /// Get user based on their Email /// </summary> /// <param name="Email">Email of user</param> /// <returns>User</returns> User GetUser(string Email); /// <summary> /// Get User by their GUID /// </summary> /// <param name="ID">GUID</param> /// <returns>User</returns> User GetUserById(string ID); } 

Services:

 public class UserService : ServiceBase, IUserService { #region Private Field private IUserRepository _userRepository; #endregion #region Constructor /// <summary> /// Constructor to initialise User Repository /// </summary> /// <param name="userRepository"></param> public UserService(IUserRepository userRepository) { _userRepository = userRepository; } #endregion #region Methods /// <summary> /// Confirm email of User /// </summary> /// <param name="userManager">User Manager to handle confirmation</param> /// <param name="userId">String user Id of the User</param> /// <param name="code">User code sent in Email</param> /// <returns>Identity Result</returns> public Task<IdentityResult> ConfirmEmail(UserManager<User, string> userManager, string userId, string code) => _userRepository.ConfirmEmail(userManager, userId, code); public void ExternalLogin() { throw new NotImplementedException(); } public void ExternalLoginCallback() { throw new NotImplementedException(); } public void ExternalLoginConfirmation() { throw new NotImplementedException(); } public void ForgotPassword() { throw new NotImplementedException(); } public void ForgotPasswordConfirmation() { throw new NotImplementedException(); } /// <summary> /// Get user based on their Email /// </summary> /// <param name="Email">Email of user</param> /// <returns>User</returns> public User GetUser(string Email) { throw new NotImplementedException(); } /// <summary> /// Get User by their GUID /// </summary> /// <param name="ID">GUID</param> /// <returns>User</returns> public User GetUserById(string ID) { throw new NotImplementedException(); } /// <summary> /// Login User /// </summary> /// <param name="signinManager">Signin Manager to handle login</param> /// <param name="email">Email of user</param> /// <param name="password">Password of user</param> /// <param name="rememberMe">Boolean if the user wants to be remembered</param> /// <returns>SignIn Status</returns> public Task<SignInStatus> Login(SignInManager<User, string> signinManager, string email, string password, bool rememberMe) => _userRepository.Login(signinManager, email, password, rememberMe); /// <summary> /// Log off user from the Application /// </summary> /// <param name="AuthenticationManager">Application Manager to handle Sign out</param> public void Logoff(IAuthenticationManager AuthenticationManager) { _userRepository.Logoff(AuthenticationManager); } /// <summary> /// Register User to Identity Database /// </summary> /// <param name="userManager">User Manager to Handle Registration</param> /// <param name="user">User to add to database</param> /// <param name="password">User password</param> public Task<IdentityResult> Register(UserManager<User, string> userManager, User user, string password) => _userRepository.Register(userManager, user, password); public void ResetPassword() { throw new NotImplementedException(); } public void ResetPasswordConfirmation() { throw new NotImplementedException(); } public void SendCode() { throw new NotImplementedException(); } /// <summary> /// Verify that code sent to User is valid /// </summary> /// <param name="signinManager">Signin Manager to handle verification</param> /// <param name="provider">Provider of the code</param> /// <param name="code">The code</param> /// <param name="rememberMe">Boolean if user wants to be remembered</param> /// <param name="rememberBrowser">Boolean if browser should be remembered</param> /// <returns>SignIn Status</returns> public Task<SignInStatus> VerifyCode(SignInManager<User, string> signinManager, string provider, string code, bool rememberMe, bool rememberBrowser) => _userRepository.VerifyCode(signinManager, provider, code, rememberMe, rememberBrowser); #endregion } 
0
source share

All Articles