Exception error when authenticating login with null values - Simplemembership - ASPNET MVC

Asked

Viewed 121 times

0

I developed a project on Asp.net mvc5 of a course I’m doing, which is accusing the following error of exception when authenticating the login menu when I enter no data in the form fields and step null parameters:

"An Exception of type 'System.Argumentexception' occurred in Webmatrix.WebData.dll but was not handled in user code - Additional information: Value cannot be null or an Empty string"

I believe that the problem is with simplemembership, because in the other views of forms when I authenticate with null values does not happen this error of exception, being that the controller of the login menu was configured with simplemenbership. I would like to know how to configure simplemembership not to give this exception error when authenticating with null values. Below is the screen print and logincontroller codeprint da excessão

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace Financas.Controllers
{
    public class LoginController : Controller
    {
        // GET: Login
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Autentica(string login, string senha )
        {
               if (WebSecurity.Login(login, senha))
            {
                return RedirectToAction("Index", "Movimentacao");
            }
               else
            {

                ModelState.AddModelError("Login.Invalido", "Login ou senha incorretos");
                return View("Index");
            }
            }
        public ActionResult Logout()
        {
            WebSecurity.Logout();
            return RedirectToAction("Index");

            

        }
    }
}

1 answer

0

Well, as the message says, this method does not accept null values or empty strings. In this case you would have to directly validate your values in this way:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace Financas.Controllers
{
    public class LoginController : Controller
    {
        // GET: Login
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Autentica(string login, string senha )
        {
             if(String.IsNullOrEmpty(login) || String.IsNullOrEmpty(senha)
            {
                ModelState.AddModelError("Login.PreencherTodosCampos", "Por favor, preencha todos os campos.");
                return View("Index");
            }


             if (WebSecurity.Login(login, senha))
            {
                return RedirectToAction("Index", "Movimentacao");
            }
               else
            {

                ModelState.AddModelError("Login.Invalido", "Login ou senha incorretos");
                return View("Index");
            }
            }
        public ActionResult Logout()
        {
            WebSecurity.Logout();
            return RedirectToAction("Index");



        }
    }
}

I hope I’ve helped.

Hug.

  • I modified it and it worked right here in my project, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.