Create Session Authorize in Asp.Net Core

1.1k views Asked by At

I am working on a Web Application using Asp.net Core 1.1 My Login is a partial view that after Success Login set Session for UserId and UserName

Controller Code :

    [HttpGet]
    public IActionResult Login()
    {
        return PartialView();
    }

And For HttpPost:

  [HttpPost]
    [ValidateAntiForgeryToken]
    public PartialViewResult Login(UserAccount user)
    {
        var accout = _context.userAccount.Where(u => u.UserName == user.UserName && u.Password == user.Password).FirstOrDefault();

        if (accout != null)
        {
            HttpContext.Session.SetString("UserID", accout.UserID.ToString());
            HttpContext.Session.SetString("UserName", accout.UserName);
            return PartialView("_welcome");
        }
        else
        {
            ViewBag.Message = "UserName Or Password is InCorrect";
            return PartialView("_ErrorMessage");
        }

        return null;

    }

Now, My project has 2 areas. First "Admin" Area and second is "User" Area. Each area has some controllers and views. I want the user to access only some of the views and controllers after Login. How can i do it?

0

There are 0 answers