Exemple #1
0
        //protected async Task<T> CallApiAsync<T>(string apiUrl, string value, Method method = Method.GET, string paramName = "application/json", ParameterType parameterType = ParameterType.RequestBody, string contentType = "application/json")
        //{

        //    var restClient = new RestClient(ConfigKeys.OmnicxApiBaseUrl);
        //    var restRequest = new RestRequest(apiUrl, method);

        //    if (!string.IsNullOrEmpty(value))
        //    {
        //        var param = new Parameter()
        //        {
        //            Name = paramName,
        //            Type = parameterType,
        //            ContentType = contentType,
        //            Value = value
        //        };
        //        restRequest.AddParameter(param);
        //    }
        //    restRequest.AddHeader("token", Token().Token);
        //    restRequest.AddHeader("SessionId", SessionId());
        //    AddDefaultHeader(ref restRequest);
        //    var cancellationTokenSource = new CancellationTokenSource();

        //    var tcs = new TaskCompletionSource<T>();

        //    restClient.ExecuteAsync(restRequest, response =>
        //    {
        //        try
        //        {
        //            tcs.SetResult(JsonConvert.DeserializeObject<T>(response.Content));
        //            //return tcs.Task;
        //        }
        //        catch (Exception)
        //        {
        //            tcs.SetResult((T)Activator.CreateInstance(typeof(T)));
        //        }
        //    });
        //    return tcs.Task.Result;

        //}
        private AuthToken Token()
        {
            try
            {
                //Token should not be depenent on Session it should be one for all
                //if(HttpContext.Current != null && HttpContext.Current.Session !=null)
                //{
                //    _token = (AuthToken)HttpContext.Current.Session[Constants.SESSION_TOKEN];
                //}


                //generate a new token if token has expired or is NULL
                if (_token != null && _token.Expiration >= DateTime.UtcNow)
                {
                    return(_token);
                }

                _token = TokenClient.GenerateToken(ConfigKeys.OmnicxApiBaseUrl, ConfigKeys.OmnicxAppId, ConfigKeys.OmnicxSharedSecret, ConfigKeys.OmnicxOutboundIp);
                //if(_token.Expiration >= DateTime.UtcNow && !string.IsNullOrEmpty(_token.Token))
                //{
                //    if(HttpContext.Current !=null && HttpContext.Current.Session != null)
                //        HttpContext.Current.Session[Constants.SESSION_TOKEN] = _token;
                //}

                return(_token);
            }
            catch (Exception ex)
            {
                //return new AuthToken();
                throw new SecurityException("Unauthorised Token Request.", ex);
            }
        }
Exemple #2
0
        public async Task <IActionResult> Login(LoginRequest request)
        {
            var principle = TokenClient.GenerateToken(request);
            await HttpContext.SignInAsync(principle, new AuthenticationProperties()
            {
                ExpiresUtc = DateTime.UtcNow.AddMinutes(60)
            });

            return(RedirectToAction("Index"));
        }