コード例 #1
0
        /// <summary>
        /// //Method is use to Validate User Credentials from Web Server Using WEB API
        /// </summary>
        /// <param name="Username"></param>
        /// <param name="Password"></param>
        public void ValidateUserandGetResponse(string Username, string Password)
        {
            try
            {
                UserLogin objvm = new UserLogin()
                {
                    Username = Username,
                    Password = EncryptandDecryptAES.Encrypt(Password)
                };

                ShareObject.Username = Username;
                using (var client = new WebClient())
                {
                    string ClientToken = ConfigurationManager.AppSettings["CLientIDToken"].ToString();
                    string keyValue    = ConfigurationManager.AppSettings["keyValue"].ToString();
                    string IVValue     = ConfigurationManager.AppSettings["IVValue"].ToString();


                    Uri URI = new Uri(ConfigurationManager.AppSettings["LoginURI"].ToString());
                    client.Headers.Add("Content-Type:application/json");
                    client.Headers.Add("APIKEY", GenerateToken.CreateToken(Username, ClientToken, DateTime.Now.Ticks));
                    client.Headers.Add("Accept:application/json");
                    client.UploadStringCompleted += new UploadStringCompletedEventHandler(Callback);

                    string SerializeData = JsonConvert.SerializeObject(objvm);

                    byte[] buffer = EncryptionDecryptorTripleDES.Encryption(SerializeData, keyValue, IVValue);
                    client.UploadStringAsync(URI, Convert.ToBase64String(buffer));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
コード例 #2
0
        public HttpResponseMessage Post(HttpRequestMessage Request)
        {
            try
            {
                var Responsedata = Request.Content.ReadAsStringAsync().Result;

                string data = EncryptionDecryptorTripleDES.Decryption(Responsedata, keyValue, IVValue);

                Product objpro = new JavaScriptSerializer().Deserialize <Product>(data);

                if (objpro.CLientIDToken != null)
                {
                    var result = ProductBL.InsertProduct(objpro);

                    if (result > 0)
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK, objpro.ProductID));
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Failed"));
                    }
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Failed"));
                }
            }
            catch (Exception)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Failed"));
            }
        }
コード例 #3
0
 public string Post(HttpRequestMessage Request)
 {
     try
     {
         if (Request != null)
         {
             var            Responsedata  = Request.Content.ReadAsStringAsync().Result;
             string         keyValue      = ConfigurationManager.AppSettings["keyValue"].ToString();
             string         IVValue       = ConfigurationManager.AppSettings["IVValue"].ToString();
             string         data          = EncryptionDecryptorTripleDES.Decryption(Responsedata, keyValue, IVValue);
             LoginViewModel objVM         = new JavaScriptSerializer().Deserialize <LoginViewModel>(data);
             var            val           = LoginBL.ValidateLoginUser(objVM.Username, objVM.Password);
             string         SerializeData = JsonConvert.SerializeObject(val);
             byte[]         buffer        = EncryptionDecryptorTripleDES.Encryption(SerializeData, keyValue, IVValue);
             return(Convert.ToBase64String(buffer));
         }
         else
         {
             return("Failed");
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
コード例 #4
0
        /// <summary>
        /// Method takes Product object as input for Pushing Data on Web Server
        /// </summary>
        /// <param name="Product"></param>
        public void DataPusher(Product Product)
        {
            try
            {
                using (var client = new WebClient())
                {
                    string ClientToken = ConfigurationManager.AppSettings["CLientIDToken"].ToString();
                    Uri    URI         = new Uri(ConfigurationManager.AppSettings["LiveURI"].ToString());
                    client.Headers.Add("Content-Type:application/json");
                    client.Headers.Add("Accept:application/json");

                    //Token APIKEY
                    client.Headers.Add("APIKEY",
                                       GenerateToken.CreateToken(ShareObject.Username, ClientToken, DateTime.Now.Ticks));
                    //Setting Call back method
                    client.UploadStringCompleted += new UploadStringCompletedEventHandler(Callback);

                    //key and IV
                    string keyValue = ConfigurationManager.AppSettings["keyValue"].ToString();
                    string IVValue  = ConfigurationManager.AppSettings["IVValue"].ToString();

                    //Serializing Object
                    string SerializeData = JsonConvert.SerializeObject(Product);
                    //Encrypting Serialized Object
                    byte[] buffer = EncryptionDecryptorTripleDES.Encryption(SerializeData, keyValue, IVValue);

                    //Converting bytes To Base64String and then Upload data
                    client.UploadStringAsync(URI, Convert.ToBase64String(buffer));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
コード例 #5
0
        /// <summary>
        /// Callback Method Gets Called when WEB API is Giving Response
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Callback(object sender, UploadStringCompletedEventArgs e)
        {
            string ClientToken = ConfigurationManager.AppSettings["CLientIDToken"].ToString();
            string keyValue    = ConfigurationManager.AppSettings["keyValue"].ToString();
            string IVValue     = ConfigurationManager.AppSettings["IVValue"].ToString();

            if (e.Error != null)
            {
            }
            if (e.Result != null || !string.IsNullOrEmpty(e.Result))
            {
                string finalData = JToken.Parse(e.Result).ToString();

                string data = EncryptionDecryptorTripleDES.Decryption(finalData, keyValue, IVValue);

                UserLogin userlogin = JsonConvert.DeserializeObject <UserLogin>(data);

                if (userlogin != null)
                {
                    if (!string.IsNullOrEmpty(userlogin.RegistrationID))
                    {
                        var resultdata = LoginBL.CheckUserExists(userlogin.Username, userlogin.Password); //Local Database check

                        if (!string.IsNullOrEmpty(resultdata))
                        {
                            ShareObject.CLientIDToken = userlogin.RegistrationID;
                            MessageBox.Show("Login Successfully");
                            this.Hide();
                            Login frm1 = new Login();
                            frm1.Close();
                            AddProduct addpro = new AddProduct();
                            addpro.Show();
                        }
                        else
                        {
                            var result = LoginBL.InsertLoginData(userlogin);

                            if (result != 0)
                            {
                                ShareObject.CLientIDToken = userlogin.RegistrationID;
                                MessageBox.Show("Login Successfully");
                                this.Hide();
                                Login frm1 = new Login();
                                frm1.Close();
                                AddProduct addpro = new AddProduct();
                                addpro.Show();
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("InValid Credentials");
                    }
                }
                else
                {
                    MessageBox.Show("InValid Credentials");
                }
            }
            else
            {
                MessageBox.Show("InValid Credentials");
            }
        }
コード例 #6
0
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            try
            {
                string keyValue        = ConfigurationManager.AppSettings["keyValue"].ToString();
                string IVValue         = ConfigurationManager.AppSettings["IVValue"].ToString();
                string SmartWeb_APIKEY = ConfigurationManager.AppSettings["APIKEY"].ToString();

                IEnumerable <string> tokenHeaders;
                if (actionContext.Request.Headers.TryGetValues("APIKEY", out tokenHeaders))
                {
                    string   tokens = tokenHeaders.First();
                    string   key    = Encoding.UTF8.GetString(Convert.FromBase64String(tokens));
                    string[] parts  = key.Split(new char[] { ':' });

                    if (tokens != null)
                    {
                        string Windows_APIKEY = parts[0]; //Hash 1 (Received in API request)
                        string hash2          = parts[1]; //Hash 2

                        //Hash 2 Decryption
                        string DecryHash1 = EncryptionDecryptorTripleDES.Decryption(hash2, keyValue, IVValue);
                        //Spliting Values
                        string[] datapart = DecryHash1.Split(new char[] { ':' });
                        // 1) Hash 2 Contains Username
                        string username = datapart[0];
                        // 2) Hash 2 Contains Ticks
                        long ticks = long.Parse(datapart[1]);

                        DateTime currentdate = new DateTime(ticks);

                        //Comparing Current Date with date sent
                        if (currentdate.Date == DateTime.Now.Date)
                        {
                            //Hash 1 Decryption
                            string DecryAPIKEY = EncryptionDecryptorTripleDES.Decryption(Windows_APIKEY, keyValue, IVValue);

                            // DecryHash2 Contains ClientToken
                            if (string.Equals(DecryAPIKEY, SmartWeb_APIKEY, comparisonType: StringComparison.InvariantCulture) == true)
                            {
                                return(true);
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }