Пример #1
0
        /// <summary>
        /// Route to call active users
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        public static ActiveUser GetActiveUser(Login login)
        {
            var activeUser = new ActiveUser();
            var result     = new RestClient(ConfigurationManager.AppSettings["WebAPIURL"]).Execute(
                HttpRestClient.RestRequest(Method.POST, "Account/Login")
                .AddObject(login)).Content;
            UserToken token = null;

            try
            {
                token = JsonConvert.DeserializeObject <ApiResult <UserToken> >(result).Results?.FirstOrDefault();
            }
            catch (System.Exception)
            {
            }

            if (token != null)
            {
                if (!string.IsNullOrEmpty(token.SystemMessage))
                {
                    activeUser.SystemMessage = token.SystemMessage;
                }
                else
                {
                    var tokenRequest = HttpRestClient.RestRequest(Method.GET, "Account/ActiveUser");
                    tokenRequest.AddHeader("Authorization", "bearer " + token.AccessToken);
                    activeUser           = JsonConvert.DeserializeObject <ApiResult <ActiveUser> >(new RestClient(ConfigurationManager.AppSettings["WebAPIURL"]).Execute(tokenRequest).Content).Results?.FirstOrDefault();
                    activeUser.AuthToken = token.AccessToken;
                }
            }
            return(activeUser);
        }
Пример #2
0
        public static int LogOut(ActiveUser activeUser)
        {
            var request = HttpRestClient.RestRequest(Method.POST, "Account/LogOut");

            request.AddHeader("Content-Type", "application/json; charset=utf-8");
            request.AddHeader("Authorization", "bearer " + activeUser.AuthToken);
            request.OnBeforeDeserialization = resp =>
            {
                resp.ContentType = "application/json";
                resp.Content     = resp.Content.Replace("[]", "{}");
            };
            var result = JsonConvert.DeserializeObject <ApiResult <int> >(new RestClient(ConfigurationManager.AppSettings["WebAPIURL"]).Execute(request).Content).Results?.FirstOrDefault();

            return(result.HasValue ? (int)result : 0);
        }