public async Task <IHttpActionResult> GetAccessTokenByUserName(GetAccessTokenByUserNameInput input)
        {
            RestHTTP http = new RestHTTP();

            RestSharp.RestRequest req = new RestSharp.RestRequest("/oauth/token", RestSharp.Method.POST);
            input.grant_type = "password";
            req.AddObject(input);

            GetAccessTokenByUserNameOutput output = http.HttpPost <GetAccessTokenByUserNameOutput>(req);

            if (output != null && !string.IsNullOrEmpty(output.AccessToken))
            {
                var user = await this.AppUserManager.FindByNameAsync(input.username);

                if (user != null)
                {
                    output.CurrentUserID     = user.Id;
                    output.TwoFactorAuthFlag = user.TwoFactorEnabled;
                    UserReturnModel userSession = this.TheModelFactory.Create(user);
                    output.UserSession = new UserReturnModel();
                    output.UserSession = userSession;
                }
            }

            return(Ok(output));
        }
Example #2
0
        private RestSharp.IRestRequest BuildRestSharpRequest(RestRequest restRequest)
        {
            var restSharpRequest = new RestSharp.RestRequest(restRequest.Resource);

            restSharpRequest.Method        = (RestSharp.Method)Enum.Parse(typeof(RestSharp.Method), restRequest.Method.ToString());
            restSharpRequest.RequestFormat = (RestSharp.DataFormat)Enum.Parse(typeof(RestSharp.DataFormat), restRequest.RequestFormat.ToString());
            restSharpRequest.DateFormat    = restRequest.DateFormat;

            //added by philip to force timeout within 15 seconds for connect
            restSharpRequest.Timeout = 15000;

            //TODO: solve this mapping
            //restSharpRequest.Credentials = restRequest.Credentials;

            if (restRequest.Body != null)
            {
                restSharpRequest.AddBody(restRequest.Body);
            }

            foreach (var item in restRequest.Cookies)
            {
                restSharpRequest.AddCookie(item.Key, item.Value);
            }

            foreach (var item in restRequest.Files)
            {
                if (!string.IsNullOrEmpty(item.Path))
                {
                    restSharpRequest.AddFile(item.Name, item.Path);
                }
                else
                {
                    if (String.IsNullOrEmpty(item.ContentType))
                    {
                        restSharpRequest.AddFile(item.Name, item.Bytes, item.FileName);
                    }
                    else
                    {
                        restSharpRequest.AddFile(item.Name, item.Bytes, item.FileName, item.ContentType);
                    }
                }
            }

            foreach (var item in restRequest.Headers)
            {
                restSharpRequest.AddHeader(item.Key, item.Value);
            }

            foreach (var item in restRequest.UrlSegments)
            {
                restSharpRequest.AddUrlSegment(item.Key, item.Value);
            }

            foreach (var item in restRequest.Objects)
            {
                restSharpRequest.AddObject(item.Key, item.Value);
            }



            foreach (var item in restRequest.Parameters)
            {
                RestSharp.ParameterType t = (RestSharp.ParameterType)Enum.Parse(typeof(RestSharp.ParameterType), item.Type.ToString());

                restSharpRequest.AddParameter(
                    item.Name,
                    item.Value,
                    t
                    );
            }


            return(restSharpRequest);
        }