0
I’m implementing a login system, but I can’t send an error message to when I try to create a duplicate login. I declared the login as follows:
[Index("User_UserName_Index", IsUnique = true)]
public string Login { get; set; }
But I can’t return an error by saying that the login already exists
I thought I’d use a try
, but I don’t know how I could do it.
For login verification I did as follows
public class LoginController : Controller
{
Contexto db = new Contexto();
// GET: Login
public ActionResult Index()
{
if (TempData["ViewData"] != null)
{
ViewData = (ViewDataDictionary)TempData["ViewData"];
}
return View();
}
public ActionResult Autenticar(string login, string senha)
{
try
{
var usuario = db.Usuarios.FirstOrDefault(p => p.Login == login && p.Senha == senha);
if (usuario.Admim == true)
{
Session["admLogado"] = usuario;
return RedirectToAction("Index", "Usuarios");
}
else
{
Session["usuarioLogado"] = usuario;
return RedirectToAction("Login", "Home");
}
}
catch(Exception ex)
{
ModelState.AddModelError(string.Empty, "Usuario ou senha invalidos");
TempData["ViewData"] = ViewData;
return RedirectToAction("Index");
}
}
Why would Login be duplicated? It does not validate if there is already a user with the same data at the time of registration?
– Victor Laio