Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3 / C# General Design opinions
    primarykey
    data
    text
    <p>I am playing with some code and was needing an opinion on a few items. I need to return a User object back to my controller from the Authentication service which is injected in the controller via Ninject. So everyone is on the same page. Here is the controller code along with some of the service code.</p> <p>In the Login ActionResult, I check to see if the user exists and if they do I will authenticate them using the authentication service. Ok, easy enough it returns true|false. I also want to so some more things with the User, I already hit the database why go back and hit it again. As you can see in the authentication service I have setup a nice user object.</p> <p>The big question now!!! Should I return User or IUser. My thoughts.... i don't want my controller depending on a concrete User so I was thinking wire up IUser to User via ninject and ctor Inject Iuser. Then I could set _user =_authenticationService.AuthenticateUser(userName,password);</p> <p>Good, bad ugly??? thoughts???</p> <p>Controller Code:</p> <pre><code> public class UsersController : Controller { private readonly IUserService _userService; private readonly IAuthenticationService _authenticationService; public UsersController(IUserService userService,IAuthenticationService authenticationService) { _userService = userService; _authenticationService = authenticationService; } public ActionResult Login() { return View(); } [HttpPost] public ActionResult Login(UserLoginViewModel userLoginViewModel) { string userName = userLoginViewModel.UserName; string password = userLoginViewModel.Password; if (_userService.UserExists(userName)) { //This will change if (_authenticationService.AuthenticateUser(userName, password)) { } } return PartialView("_Login"); } public ActionResult Register() { return PartialView("_Register"); } [HttpPost] public ActionResult Register(UserRegisterViewModel userRegisterViewModel) { ModelState.AddModelError("UserName", "Testing"); return PartialView("_Register"); } } </code></pre> <p>Service Code:</p> <pre><code>public class AuthenticationService:IAuthenticationService { private readonly IRepository _repository; private readonly IEncryption _encryption; public AuthenticationService(IRepository repository,IEncryption encryption) { _repository = repository; _encryption = encryption; } // HMM! Ok I need to get the User object back to the controller, so instead of returning bool should I return User or IUser. public bool AuthenticateUser(string userName, string password) { try { var user = _repository.Select&lt;Users&gt;().Where(u =&gt; u.UserName == userName).Select(u =&gt; new User { UserId = u.UserID, UserTypeId = u.UserTypeID, UserName = u.UserName, Password = u.Password, Salt = u.Salt, ActivationCode = u.ActivationCode, InvalidLoginAttempts = u.InvalidLoginAttempts, IsLockedOut = u.IsLockedOut, LastLoginDate = u.LastLoginDate, Active = u.Active, DateCreated = u.DateCreated, LastUpdated = u.LastUpdated }).Single(); // Check the users password hash if(_encryption.VerifyHashString(password,user.Password,user.Salt)) { return true; } } catch (Exception) { return false; } // get the user from the database return false; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload