Ejemplo n.º 1
0
        public async Task InvokeAsync(HttpContext context)
        {
            UserContext.Current.Clear();
            UserContext.Current.IsAuthenticated = false;

            LoadContextValuesFromCookie(context.Request.Cookies);
            LoadContextValuesFromHeader(context.Request.Headers);
            var userAgent = context.Request.Headers["User-Agent"];

            UserContext.Current.IsMobile  = UserAgentHelper.IsMobile(userAgent);
            UserContext.Current.UserAgent = userAgent;

            if (!string.IsNullOrEmpty(UserContext.Current.Token))
            {
                _contextManager.LoadUserContext(UserContext.Current.Token);
                var result = await context.AuthenticateAsync(TokenDefaults.AuthenticationScheme);

                if (result?.Principal != null)
                {
                    context.User = result.Principal;
                }
            }

            await _next(context);
        }