コード例 #1
0
        public static IdentityUser GetCurrentlyLoggedInUser(HttpSessionStateBase session = null)
        {
            try
            {
                if (session == null && HttpContext.Current != null && HttpContext.Current.Session != null)
                {
                    session = new HttpSessionStateWrapper(HttpContext.Current.Session);
                }
                if (session != null)
                {
                    IdentityUser user = session[SS_CURRENT_USER] as IdentityUser;
                    if (user == null)
                    {
                        var principal = HttpContext.Current.User as ClaimsPrincipal;
                        if (principal != null && principal.Identity.IsAuthenticated)
                        {
                            IdentityUserDAO.InstitutionCode = InstitutionCode;
                            var userId = HttpContext.Current.User.Identity.GetUserId <long>();
                            if (userId > 0)
                            {
                                user = IdentityUserDAO.Retrieve(userId);
                            }
                            if (user == null)
                            {
                                throw new LogOutUserException($"Called WebUtilities.GetCurrentlyLoggedInUser(): Failed to get user [id: {userId}. instCode: {IdentityUserDAO.InstitutionCode}]");
                            }

                            user.InstitutionCode = IdentityUserDAO.InstitutionCode; //Needful? Maybe not.
                            HttpContext.Current.Session[SS_CURRENT_USER] = user;
                        }
                    }
                    return(user);
                }
                return(null);
            }
            catch (Exception ex) when(!(ex is LogOutUserException))
            {
                Utilities.Logger.Log(ex);
                return(null);
            }
        }