1
\$\begingroup\$

I'm fairly new to ASP.NET Core, I've used webforms for years so I naturally want to cling to things I did with webforms. The following code works, but I'm wondering if it can be cleaned up with existing .NET Core functionality I'm unaware of?

Tech stack Asp.net Core 2.0, EntityFramework core with Postgres database

Razor File for login:

@{
ViewData["Title"] = "My apps login title";
// Making this nullable makes it so I don't 
// have to pass a false boolean when I'm not postbacking
bool? failedLogin = (bool?)ViewData["failedLogin"];
}
<h2>My Application's name goes here</h2>
<form method="post">
    <div class="form-group">
        <div class="form-group">
            <label for="email">Username:</label>
            <input type="text" class="form-control" id="username" name="username">
        </div>
        <div class="form-group">
            <label for="pwd">Password:</label>
            <input type="password" class="form-control" id="password" name="password">
        </div>
        <div class="checkbox">
            <label><input type="checkbox"> Remember me</label>
        </div>
        @if (failedLogin == true)
        {
            <div class="text-danger form-group">
                Invalid Username and/or password
            </div>
        }
        <button type="submit" class="btn btn-default">Submit</button>
</form>

My Controller Postback(Is that the correct terminology in MVC?) method

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Index(string username, string password)
{  
    // I don't like seeing (using() {} everywhere in my code. I like to hide the ugliness)
    var matchedUser = ProgramWideFunctions.getUserByUserNameAndPassword(username, password);
    if (matchedUser != null)
    {
        HttpContext.Session.SetString("username", matchedUser.username);
        return RedirectToAction("ControlCentre");
    }
    else
    {
        ViewData["failedLogin"] = true;
        return View();
    }
}

You might've noticed I hid the ugliness of the using statements for the database check in an external file. I'm considering making these two methods overloaded (two getUser() methods, one for UN, and another for UN/PW

public static KAPUsers getUserByUserName(string username)
    {
        using (var db = new AlertsDbContext())
        {
            return db.KAPUsers.Where(b => b.username == username).FirstOrDefault();
        }

    }

    public static KAPUsers getUserByUserNameAndPassword(string username, string password)
    {
        using (var db = new AlertsDbContext())
        {
            return db.KAPUsers.Where(b => b.username == username && b.password == password).FirstOrDefault();
        }

    }
\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review! I hope you get great answers. \$\endgroup\$ – Phrancis Oct 22 '17 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.