Session issues in MVC application using Actionfilters

Asked

Viewed 478 times

0

I have a problem in my application ASP.Net MVC, on authentication. Let’s go through steps:

1) I have my login screen /Login/Index simply log in, if found in the database MySQL, and starts the session and redirects to /Dashboard/Index. This is the user verification code and if it correctly starts the session: [Note:] I use object SessionManager Generic and a ISessionOperation 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 issue is that I deal with all Actions of my application with ActionFilter or created a ActionFilter that checks if the session is active. If it is not redirected to /Login/Index. If active, access to Action call, follow the example of my ActionFilter what 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 mine Actions within the majority of controllers 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 get to the problem! , Next, this developing, local in my Visual Studio 2012, and even the IIS7 configured location is working properly parametrizei in the webconfig sessionTimeOut to 60 as well. But the bigger problem is that when I compile and put it on my hosting domain and try to log it properly but when I click on internal links within my application it redirects to the /Login/Index page I don’t know why and recently clicked with the button mouse right on an internal link and have opened in new tab and it clicked correctly, the question is, when soon and click in any ex menu:

Financial

  • 1 - Tickets 2 - Transferor 3 - Cash

It redirects to Login/Index, but when ordered to open in new tab by Chrome for example it loads correctly, sometimes it redirects also to Login/Index.

This all doesn’t happen locally.

4) I searched the internet and saw what to use Session is very bad tals in order I won’t go into detail an alternative would be to use Cache but the objective question would be how to solve it. Detail all my menu Urls are configured @Url.Action('~/Controller/Action') I believe you are rightly.

  • 1

    Why not use the standard Asp.net mvc ? Identity

1 answer

2

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

How you must be wearing one In-process Session State, then it is important that the user always access the same server where he made the first access.

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

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

  • I understood, yes, it is strange if to each request the balance access several servers even, ai complica use Sesssion. had not thought of this fact, I will try to implement the solution of Provider for Mysql, I put here whether it worked or not. thanks!!!

  • Hello I made a Session object (implementing Session Provider) but I’m having some problems finishing some methods have some example ?

Browser other questions tagged

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