Session Issues in MVC application using ActionFilters

0

I have a problem with my application ASP.Net MVC , about authentication. Let's go by steps:

1) I have my login screen /Login/Index simply the user logs, if found in the MySQL database, and logs in and redirects to /Dashboard/Index . This is the user verification code and if correct logs in:     [Note:] I use object SessionManager Generic% and ISessionOperation com with methods that Start , Finish , IsActive , GetSessionId , GetUsuario .     Follow the code below the Action Login HttpPost :

[HttpPost]
public ActionResult Logar(UsuarioDto Model, string Remind)
{
    try
    {
        var UsuarioLogado = UsuarioDomain.Authentication(Model);

        if (UsuarioLogado != null)
        {
            //--> Acesso
            var AcessoDomain = new SmartAdmin.Domain.Acesso();
            AcessoDomain.Save(GetUserInformation(String.Empty));

            //--> Menus & Submenus
            var CollectionMenuMain = new List<MenuModelView>();                    
            foreach (var MenuMain in UsuarioDomain.GetAllowedMenus(UsuarioLogado.ID)) //--> Para cada menu pai pega os filhos e adiciona no modelo de visão
            {
               var CollectionSubMenus = UsuarioDomain.GetSubMenuFromMenu(MenuMain.ID);
               var CurrentMenuMain = new MenuModelView() { Menu = MenuMain, CollectionSubMenu = CollectionSubMenus }; 
               CollectionMenuMain.Add(CurrentMenuMain);
            }

            //--> Session
            var Session = new SessionManager();
            Session.Start(new UsuarioModelView() { Usuario = UsuarioLogado, CollectionMenusAndSubMenus = CollectionMenuMain });

            return (RedirectToAction("Index", "Menu"));
        }
        else
        {
            TempData["Mensagem"] = "Usuário inexistente ou não esta ativo no sistema, contate o Administrador!";
            return (RedirectToAction("Index", "Login"));
        }  
    }
    catch (Exception Ex)
    {
        throw new Exception(Ex.Message);
    }
}

2) The second question is that I treat all Actions of my application with ActionFilter or I created a ActionFilter that verifies if the session is active. If it is not redirected to /Login/Index . If it is active the access to Action calls, follow the example of my ActionFilter that I use:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var Session = new SessionManager();

    if (Session.IsActive() == false)
    {
        filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
        {
            controller = "Login",
            action = "Index"
        }));
 }

I use it this way in my Actions within most% of% of my application, a common thing to use ok.

[AuthorizedUser]
public ActionResult Cedente()
{
    var CedenteDomain = new Cedente();
    var Model = CedenteDomain.GetItem(_ => _.ID == 1);

    ViewBag.Mensagem = (TempData["Mensagem"] as String);
    return View((Model==null)?new CedenteDto(): Model);
}

3) Let's go to the problem !, Next, this in development, location in my controllers , and up to Visual Studio 2012 local configured    is working correctly parametrizei in IIS7 webconfig to 60 also. But the bigger problem is that    when compile and put in my hosting domain and I try to log it properly but when I click on internal links    inside my application it redirects to the / Login / Index page I do not know why and recently clicked with the button    right click on an internal link and I had it open in new tab and it loaded correctly, the question is, when I click and click    in any menu ex:

Financial

  • 1 - Tickets 2 - Assignor 3 - Cash

It redirects to Login / Index, but when I say open in new tab for Chrome for example it loads correctly, sometimes it redirects to sessionTimeOut .

This does not all happen locally.

4) I was searching the internet and I saw that using Login/Index is very bad tals in order I will not go into detail an alternative would be to use Cache but the objective question would be how to solve it.Details all my menu URLs are configured Session I think it is properly correct.

    
asked by anonymous 11.01.2016 / 14:36

1 answer

2

Rodrigo, I believe that the problem is in the way your hosting service balances the requests, possibly you are being taken to different servers with each request.

As you should be using a In-process Session State , then it is important that the user always access the same server where he first accessed.

Perhaps a solution to your problem would be to use Out-of-Proc State Server , be Session State Service or SQL Session State .

If you decide to use SQL Server as Session State , you can follow this guide at MSDN . In the case of MySQL, you will need to write your own Provider, follows an example found in Code Project

    
11.01.2016 / 15:00