Exemplo n.º 1
0
        // GET: Part9FileUpload
        public ActionResult Index()
        {
            //call web-api from TRM data manager @ localhost:56473
            // Get the last Token - order desc by  Id
            Data_WebApi_Jquery.Models.AccessToken _accessToken = _db.AccessTokens
                                                                 .OrderByDescending(p => p.Id)
                                                                 .FirstOrDefault();
            string accessToken = _accessToken.TokenString;

            //get list of FileUpload
            var fileUploads = _db.FileUploads.ToList();
            // Map fileUploads to ViewModel Part9InexView
            List <Part9InexView> part9InexViews = new List <Part9InexView>();

            foreach (FileUpload rec in fileUploads)
            {
                Part9InexView part9InexView = new Part9InexView();
                part9InexView.Id            = rec.Id;
                part9InexView.FileURI       = rec.FileURI;
                part9InexView.FileRemarks   = rec.FileRemarks;
                part9InexView.FileDateStamp = rec.FileDateStamp;
                part9InexView.FileUserStamp = rec.FileUserStamp;
                part9InexViews.Add(part9InexView);
            }

            return(View(part9InexViews));
        }
Exemplo n.º 2
0
        public ActionResult Create(Part9DetailView part9DetailView)
        {
            Data_WebApi_Jquery.Models.AccessToken _accessToken = _db.AccessTokens
                                                                 .OrderByDescending(p => p.Id)
                                                                 .FirstOrDefault();
            string accessToken = _accessToken.TokenString;
            //*************************************
            // First TEST token - if still valid
            //*************************************
            HttpClient TestClient = new HttpClient();

            TestClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
            var resultToken = TestClient.GetAsync("http://localhost:56473/api/values").Result;

            if (!resultToken.IsSuccessStatusCode)
            {
                //Refresh Token
                string newToken = WebClient.Utilities.TokenStuff.refreshTToken();
                accessToken = newToken;
            }
            TestClient.Dispose();
            // After getting the token, all you have to do is below.. - passing token to API - see line#
            if (ModelState.IsValid)
            {
                var file = part9DetailView.UpLdFile; // this is where we pass in the upload file
                                                     // File will be passed in as header attachment
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken); // passing token to the api
                    using (var content = new MultipartFormDataContent())
                    {
                        byte[] Bytes = new byte[file.InputStream.Length + 1];
                        file.InputStream.Read(Bytes, 0, Bytes.Length);
                        var fileContent = new ByteArrayContent(Bytes);
                        fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
                        {
                            FileName = file.FileName
                        };
                        content.Add(fileContent);
                        var requestUri = "http://localhost:56473/api/Upload?userStmp=BX";
                        var result     = client.PostAsync(requestUri, content).Result;
                        if (result.StatusCode == System.Net.HttpStatusCode.Created)
                        {
                            List <string> m = result.Content.ReadAsAsync <List <string> >().Result;
                            ViewBag.Success = m.FirstOrDefault();//m[0] => URI, m[1] => scaned id

                            // Save into your app file Upload table
                        }
                        else
                        {
                            ViewBag.Failed = "Failed !" + result.Content.ToString();
                        }
                    }
                }
            }
            return(View());
        }
Exemplo n.º 3
0
        public static string refreshTToken()
        {
            string _token = "";

            string userName = "******";
            string passWord = "******";

            string BaseAddress = "http://localhost:56473/Token";

            using (var client = new HttpClient {
                BaseAddress = new Uri(BaseAddress)
            })
            {
                var token = client.PostAsync("Token",
                                             new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("grant_type", "password"),
                    new KeyValuePair <string, string>("username", userName),
                    new KeyValuePair <string, string>("password", passWord)
                })).Result.Content.ReadAsAsync <AuthenticationToken>().Result;

                Console.WriteLine("...");

                if (token != null)
                {
                    _token = token.access_token.ToString().Trim();
                    // insert accessToken to AccessToken table
                    // create new AccessToken record
                    Data_WebApi_Jquery.Models.AccessToken accessToken = new Data_WebApi_Jquery.Models.AccessToken();
                    accessToken.Created     = DateTime.Now;
                    accessToken.TokenString = token.access_token;



                    try
                    {
                        using (EmpDBEntities dc = new EmpDBEntities())
                        {
                            dc.AccessTokens.Add(accessToken);
                            dc.SaveChanges();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }

                    return(_token);
                }
            }
            return(_token);
        }
Exemplo n.º 4
0
        public ActionResult Index(Part7IndexView part7IndexView)
        {
            if (Session["SelectedProductName"] != null)
            {
                Test_String = Session["SelectedProductName"].ToString();
            }

            //await GetTokenDetails("*****@*****.**", "123456");

            string userName = part7IndexView.UserName.Trim();
            //string passWord = "******";
            string passWord    = part7IndexView.Password.Trim();
            string BaseAddress = "http://localhost:56473/Token";

            using (var client = new HttpClient {
                BaseAddress = new Uri(BaseAddress)
            })
            {
                var token = client.PostAsync("Token",
                                             new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("grant_type", "password"),
                    new KeyValuePair <string, string>("username", userName),
                    new KeyValuePair <string, string>("password", passWord)
                })).Result.Content.ReadAsAsync <AuthenticationToken>().Result;

                Console.WriteLine("...");

                if (token != null)
                {
                    part7IndexView.access_token = token.access_token.ToString().Trim();

                    client.DefaultRequestHeaders.Authorization =
                        new AuthenticationHeaderValue(token.token_type, token.access_token);

                    // insert accessToken to AccessToken table
                    // create new AccessToken record
                    Data_WebApi_Jquery.Models.AccessToken accessToken = new Data_WebApi_Jquery.Models.AccessToken();
                    accessToken.Created            = DateTime.Now;
                    accessToken.TokenString        = token.access_token;
                    Session["SelectedProductName"] = token.access_token;


                    try
                    {
                        using (EmpDBEntities dc = new EmpDBEntities())
                        {
                            dc.AccessTokens.Add(accessToken);
                            dc.SaveChanges();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
                // actual requests from your api follow here . . .
            }

            return(View(part7IndexView));
        }