コード例 #1
0
        private static bool AuthenticateUser(ControllerContext context, IAuthenticateService authenticateService)
        {
            if (context.HttpContext.User.Identity.IsAuthenticated)
            {
                var userId = context.HttpContext.User.Identity.Name;

                if (!string.IsNullOrWhiteSpace(userId))
                {
                    var sessionSecurityToken = context.HttpContext.Cache[string.Concat(CacheKeyNames.Identity, userId)] as SessionSecurityToken;

                    if (sessionSecurityToken != null)
                    {
                        // If the user's claims principal was located in cache retrieve it from there.
                        context.HttpContext.User = sessionSecurityToken.ClaimsPrincipal;

                        return true;
                    }

                    if (authenticateService.ValidateUser(Int32.Parse(userId)))
                    {
                        // If the user's claims principal was not located in cache set it and repeat the login steps.
                        Login(context.HttpContext, true);

                        return true;
                    }
                }
            }

            return false;
        }