Skip to content

Instantly share code, notes, and snippets.

@rushfrisby
Created June 15, 2015 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rushfrisby/e8e795ca24df5ba3e21a to your computer and use it in GitHub Desktop.
Save rushfrisby/e8e795ca24df5ba3e21a to your computer and use it in GitHub Desktop.
Intentionally bad code...
public interface IUserService
{
User GetUser(string userName);
}
public static class UserService : IUserService
{
public User GetUser(string userName)
{
using(var context = new SomeDbContext())
{
return context.Users.SqlQuery("SELECT * FROM [dbo].[User] WHERE [UserName]='" + userName + "'").First();
}
}
}
public class UserController
{
public static int CurrentUserId { get; set; }
private readonly IUserService _userService;
public UserController(IUserService userService)
{
_userService = userService;
}
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(string userName, string password)
{
var user = _userService.GetUser(userName);
if(user == null)
{
ViewBag.ErrorMessage = "User name does not exist";
}
else
{
if(user.Password == password)
{
CurrentUserId = user.Id;
return RedirectToAction("Index");
}
else
{
ViewBag.ErrorMessage = "Incorrect password";
}
}
return View();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment