Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6887601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:54:17+00:00 2026-05-27T05:54:17+00:00

I am playing with some code and was needing an opinion on a few

  • 0

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.

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.

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

Good, bad ugly??? thoughts???

Controller 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");
    }
}

Service 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<Users>().Where(u => u.UserName == userName).Select(u => 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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:54:18+00:00Added an answer on May 27, 2026 at 5:54 am

    I’d just lookup the User by name and check the password with bool AuthenticateUser(User user, string password). Or, if you like out parameters do it like bool AuthenticateUser(string username, string password, out User user). I think the UserExists method is useless for this scenario, since you want to to do more with the object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some code for playing a .wav through my application. Now I
Here is some C++ code I'm playing around with: #include <iostream> #include <vector> #define
Does anyone know of some mp3 playing code for the pocket PC. I have
Does anyone have some useful beginner tutorials and code snippets for playing with basic
I was playing with some code to make a "closure like" construct ( not
I was just playing around with some code in LINQPad and managed to crash
I'm just playing around with some code. I create an Activity and simply do
I was playing around with some JavaScript code and found that when I took
I'm playing with writing some C code to speed up an inner loop in
I was playing around with some code when I noticed something strange: [~] main%

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.