예제 #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
            {
                var userId = AuthenticationCookieManager.GetAuthenticationCookie().Id;

                if (userId == null)
                {
                    Response.Redirect("/Login");
                }

                AccountUser 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 <AccountUser>(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 accountManagementServiceClient = new AccountManagementService.AccountManagementServiceClient();

                    try
                    {
                        accountManagementServiceClient.Open();
                        user = accountManagementServiceClient.GetAccountUser(userId, Common.SharedClientKey);

                        //Close the connection
                        WCFManager.CloseConnection(accountManagementServiceClient);
                    }
                    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(accountManagementServiceClient, 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
                var accountUser = AuthenticationCookieManager.GetAuthenticationCookie();
                jsonNetResult.Data = accountUser;

                return(jsonNetResult);
            }
        }