0

I need to create a login system, I have been able to do the registration part and the login part, but I am not able to create the part for the user to leave the session (exit the system).

Note: I have a repository with the SQL commands

Could someone help me with how to do this part?

I am using the Entity Framework and I do it by database first

Here is my controller

public class AccountUserController : Controller
{
    private clsContext cnn = new clsContext();

    public ActionResult Index()
    {
        clsUserRepository usu = new clsUserRepository(cnn);
        return View(usu.AccountList());
    }

    public ActionResult Register()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Register(clsAccountUser userObj)
    {
        if (ModelState.IsValid)
        {
            clsUserRepository usu = new clsUserRepository(cnn);
            usu.Register(userObj);
            cnn.SaveChanges();

            ModelState.Clear();
            ViewBag.Message = userObj.usuNome + " " +"Successfully registered!";
        }
        return View();
    }

    public ActionResult Login()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Login(clsAccountUser userObj)
    {
        cclsUserRepository usu = new clsUserRepository(cnn);

        if (usu.Login(userObj) == 2)
        {
            Session["Logged"] = usu.Login(userObj);

            return RedirectToAction("Logged");
        }
        else if(usu.Login(userObj) == 1)
        {
            ViewBag.Message = "Invalid username and password";
        }
        else
        {
            ViewBag.Message = "RE: " + userObj.usuRe + " Invalid" ;
        }

        return View();
    }

    public ActionResult Logged(clsAccountUser usuarioObj)
    {
       if(Convert.ToInt32(Session["Logged"]) != 0)
       {
           return View();
       }
       else 
       {
           return RedirectToAction("Login"); 
       }
    }
  }
}
4
  • 1
    You'll be much better off using ASP.NET Identity instead Commented Feb 4, 2019 at 14:40
  • I need to use the entity framework. But how can I do this using the ASP.NET Identity instead? Commented Feb 4, 2019 at 14:55
  • 1
    ASP.NET Identity uses EF for database access. asp.net/identity Commented Feb 4, 2019 at 14:55
  • Sorry, I'm new with C# Commented Feb 4, 2019 at 14:56

1 Answer 1

1

Set all session variables to NULL, abandon your session and redirect to homepage to create a Logout functionality.

In your controller, create a new action -

Public ActionResult Logout()
{
    Session["LoggedData"] = null;
    Session.Abandon();
    return RedirectToAction("Default", "Home");
}
Sign up to request clarification or add additional context in comments.

3 Comments

Do I need to create a view to the Logout?
If you want a custom message on Logout, you need another view. But, if you want it to redirect to Homepage, this above code works.
If you create Logout View, then use Return View() instead of Return RedirectToAction().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.