0

I have my controller SomeController and its inherited from ApiController, also, I have an ActionFilter:

FilterConfig

public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            //filters.Add(new HandleErrorAttribute());
            GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());
        }
    }

ErrorLogService

public static class ErrorLogService
    {
        public static void LogError(Exception ex, string metodo, string clase)
        {
            Utilidades.EnviarErrorHTTP(ex, null, metodo, clase);
        }
    }

LogExceptionFilterAttribute

public class LogExceptionFilterAttribute : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext context)
        {            
            //TODO
        }
    }

Well, the session is handled by the ApiController and in my SomeController I can use it like:

var session = TokenUser;

But, there nothing in my ErrorLogService to invoke the function to know the token.

Is there a way to share this variable if it is different in each session? (TokenUser is an object).

torvin
  • 6,515
  • 1
  • 37
  • 52
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
  • Your question is unclear - where is `TokenUser` instantiated? What does it contain? Please put the relevant code in the question. – NightOwl888 Mar 13 '18 at 21:24
  • Usually `Request.Properties` dictionary is used for passing state between different layers of ASP.NET Web Api. Check [this answer](https://stackoverflow.com/questions/15059161/webapi-how-to-pass-state-from-filter-to-controller) for details. – CodeFuller Mar 14 '18 at 05:12

1 Answers1

0

I found a way to do it.

In the Global.asax you must add the next code:

protected void Application_PostAuthorizeRequest()
        {
            System.Web.HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
        }

And now, you are available to use Session:

var session = System.Web.HttpContext.Current.Session;
session["token"] = sesion;

And the variable session would persist in the application.

Benjamin RD
  • 11,516
  • 14
  • 87
  • 157