Пример #1
0
        public JsonNetResult GetCurrentUser()
        {
            //This is used often by the UI to refresh the user, wrap in a Try/Catch in case of intermitent failures:

            try
            {
                string       userId = AuthenticationCookieManager.GetAuthenticationCookie().Id;
                PlatformUser user   = null;

                #region (Plan A) Get data from Redis Cache

                try
                {
                    //First we attempt to get the user from the Redis Cache

                    IDatabase cache = CoreServices.RedisConnectionMultiplexers.RedisMultiplexer.GetDatabase();

                    string hashKey   = "user:"******"-", "");
                    string hashField = "model";

                    var redisValue = cache.HashGet(hashKey, hashField);

                    //con.Close();

                    if (redisValue.HasValue)
                    {
                        user = JsonConvert.DeserializeObject <PlatformUser>(redisValue);
                    }
                }
                catch (Exception e)
                {
                    var error = e.Message;
                }

                #endregion

                if (user == null)
                {
                    #region (Plan B) Get data from WCF

                    //If a failure occurs, or the redis cache is empty we get the user from the WCF service
                    var platformManagementServiceClient = new PlatformManagementService.PlatformManagementServiceClient();

                    try
                    {
                        platformManagementServiceClient.Open();
                        user = platformManagementServiceClient.GetPlatformUser(userId, Common.SharedClientKey);

                        //Close the connection
                        WCFManager.CloseConnection(platformManagementServiceClient);
                    }
                    catch (Exception e)
                    {
                        #region Manage Exception

                        string exceptionMessage = e.Message.ToString();

                        var    currentMethod       = System.Reflection.MethodBase.GetCurrentMethod();
                        string currentMethodString = currentMethod.DeclaringType.FullName + "." + currentMethod.Name;

                        // Abort the connection & manage the exception
                        WCFManager.CloseConnection(platformManagementServiceClient, exceptionMessage, currentMethodString);

                        #endregion
                    }

                    #endregion
                }

                //Update the local cookies:
                AuthenticationCookieManager.SaveAuthenticationCookie(user);

                JsonNetResult jsonNetResult = new JsonNetResult();
                jsonNetResult.Formatting = Newtonsoft.Json.Formatting.Indented;
                jsonNetResult.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; //<-- Convert UTC times to LocalTime
                jsonNetResult.Data = user;

                return(jsonNetResult);
            }
            catch
            {
                // Use cookies instead
                JsonNetResult jsonNetResult = new JsonNetResult();
                jsonNetResult.Formatting = Newtonsoft.Json.Formatting.Indented;
                jsonNetResult.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; //<-- Convert UTC times to LocalTime
                jsonNetResult.Data = AuthenticationCookieManager.GetAuthenticationCookie();

                return(jsonNetResult);
            }
        }