public async Task <IActionResult> Edit(string id, [Bind("Id,Uidname,Version,Inscompany,Policyno,Vehicleno,Userid,Status,Firstname,Lastname,Lastmod,Startdate,Enddate")] VehiclePolicies _Policies)
        {
            if (id != _Policies.Id)
            {
                return(NotFound());
            }

            await _akvclient.UpdateSecret(_Policies);

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(_Policies);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!VehPoliciesMasterExists(_Policies.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(_Policies));
        }
Beispiel #2
0
        public async Task <IActionResult> Create([Bind("Id,Uidname,Version,Inscompany,Policyno,Vehicleno,Userid,Status,Firstname,Lastname,Lastmod,Startdate,Enddate")] VehiclePolicies vehPoliciesMaster)
        {
            if (ModelState.IsValid)
            {
                _context.Add(vehPoliciesMaster);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(vehPoliciesMaster));
        }
        public async Task <IActionResult> Create([Bind("Id,Uidname,Version,Inscompany,Policyno,Vehicleno,Userid,Status,Firstname,Lastname,Lastmod,Startdate,Enddate")] VehiclePolicies _Policies)
        {
            _Policies.Id = Guid.NewGuid().ToString();

            await _akvclient.CreateSecret(_Policies);

            if (ModelState.IsValid)
            {
                _context.Add(_Policies);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(_Policies));
        }
Beispiel #4
0
        public async Task CreateSecret(VehiclePolicies policydata)
        {
            // Create the content for the Policy data to be stored as Secret
            Insdata akvdata = new Insdata {
                Id       = policydata.Id, Inscompany = policydata.Inscompany,
                Policyno = policydata.Policyno, Userid = policydata.Userid, Vehicleno = policydata.Vehicleno
            };

            //Create a JSON String of the Policy data to be stored as Secret
            string insurancepolicysecret = JsonConvert.SerializeObject(akvdata);

            byte[] datatoencrypt = System.Text.Encoding.UTF8.GetBytes(insurancepolicysecret);
            string keyUri        = string.Format("https://{0}.vault.azure.net/keys/{1}", _keyVaultName, _keyName);
            string keyVaultUri   = string.Format("https://{0}.vault.azure.net", _keyVaultName);


            //Encrypt the data before it is stored as a Secret
            KeyOperationResult result = await _keyVaultClient.EncryptAsync(keyUri, JsonWebKeyEncryptionAlgorithm.RSAOAEP256,
                                                                           datatoencrypt);

            byte[] encdata       = result.Result;
            string encrypteddata = Convert.ToBase64String(encdata);

            //Set the Policy Start and Expiry Data to be added as attributes to the secret
            SecretAttributes attribs = new SecretAttributes
            {
                Enabled   = true,
                Expires   = DateTime.UtcNow.AddYears(1),
                NotBefore = DateTime.UtcNow
            };

            IDictionary <string, string> alltags = new Dictionary <string, string>
            {
                { "InsuranceCompany", policydata.Inscompany }
            };
            string contentType = "DigitalInsurance";

            // Create a Secret with the encrypted Policy data
            SecretBundle bundle = await _keyVaultClient.SetSecretAsync(keyVaultUri, policydata.Uidname,
                                                                       encrypteddata, alltags, contentType, attribs);

            string bundlestr = bundle.Value;

            policydata.Version   = bundle.SecretIdentifier.Version;
            policydata.Lastmod   = bundle.Attributes.Updated;
            policydata.Startdate = bundle.Attributes.NotBefore;
            policydata.Enddate   = bundle.Attributes.Expires;
        }
Beispiel #5
0
        public async Task <bool> PostVehPoliciesMaster([FromBody] VehiclePolicies vehPoliciesMaster)
        {
            bool isValid = false;

            if (!ModelState.IsValid)
            {
                // return BadRequest(ModelState);
                return(isValid);
            }

            var _vehPoliciesMaster = await _context.VehPoliciesMaster.SingleOrDefaultAsync(m => m.Policyno == vehPoliciesMaster.Policyno);

            if (_vehPoliciesMaster == null)
            {
                //return NotFound();
                return(isValid);
            }

            if (vehPoliciesMaster.Id.Equals(_vehPoliciesMaster.Id) &&
                "Active".Equals(_vehPoliciesMaster.Status) &&
                (_vehPoliciesMaster.Enddate >= DateTime.UtcNow) &&
                (DateTime.UtcNow >= _vehPoliciesMaster.Startdate) &&
                vehPoliciesMaster.Policyno.Equals(_vehPoliciesMaster.Policyno) &&
                vehPoliciesMaster.Vehicleno.Equals(_vehPoliciesMaster.Vehicleno) &&
                vehPoliciesMaster.Firstname.Equals(_vehPoliciesMaster.Firstname) &&
                vehPoliciesMaster.Lastname.Equals(_vehPoliciesMaster.Lastname) &&
                vehPoliciesMaster.Inscompany.Equals(_vehPoliciesMaster.Inscompany)
                )
            {
                try
                {
                    isValid = await _akvclient.ValidateAsync(_vehPoliciesMaster);
                }
                catch (Exception ex)
                {
                    string error = ex.StackTrace;
                }
            }
            return(isValid);
        }
Beispiel #6
0
        public async Task <IActionResult> Edit(string id, [Bind("Id,Uidname,Version,Inscompany,Policyno,Vehicleno,Userid,Status,Firstname,Lastname,Lastmod,Startdate,Enddate")] VehiclePolicies vehPoliciesMaster)
        {
            if (id != vehPoliciesMaster.Id)
            {
                return(NotFound());
            }

            if ("Active".Equals(vehPoliciesMaster.Status))
            {
                // Status is already active, no action
                return(View(vehPoliciesMaster));
            }
            vehPoliciesMaster.Status  = "Active";
            vehPoliciesMaster.Lastmod = DateTime.UtcNow;


            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(vehPoliciesMaster);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!VehPoliciesMasterExists(vehPoliciesMaster.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            return(View(vehPoliciesMaster));
        }
        public async Task <bool> ValidateAsync(VehiclePolicies vehPoliciesMaster)
        {
            bool   isValid     = false;
            string keyUri      = string.Format("https://{0}.vault.azure.net/keys/{1}", _keyVaultName, _keyName);
            string keyVaultUri = string.Format("https://{0}.vault.azure.net", _keyVaultName);
            var    bundle      = await _keyVaultClient.GetSecretAsync(keyVaultUri, vehPoliciesMaster.Uidname);

            string decryptedstring = bundle.Value;

            byte[]             encdata = Convert.FromBase64String(decryptedstring);
            KeyOperationResult result  = await _keyVaultClient.DecryptAsync(keyUri, JsonWebKeyEncryptionAlgorithm.RSAOAEP256, encdata);

            byte[]           decrypteddata = result.Result;
            string           secretdata    = System.Text.Encoding.UTF8.GetString(decrypteddata);
            SecretAttributes attributes    = bundle.Attributes;

            if (attributes.Expires < System.DateTime.UtcNow || attributes.NotBefore > System.DateTime.UtcNow)
            {
                isValid = false;
            }
            VehiclePolicies _vehPoliciesMaster = JsonConvert.DeserializeObject <VehiclePolicies>(secretdata);

            if (vehPoliciesMaster.Id.Equals(_vehPoliciesMaster.Id) &&
                vehPoliciesMaster.Policyno.Equals(_vehPoliciesMaster.Policyno) &&
                vehPoliciesMaster.Userid.Equals(_vehPoliciesMaster.Userid) &&
                vehPoliciesMaster.Vehicleno.Equals(_vehPoliciesMaster.Vehicleno) &&
                vehPoliciesMaster.Inscompany.Equals(_vehPoliciesMaster.Inscompany)
                )
            {
                isValid = true;
            }
            else
            {
                return(isValid);
            }
            return(isValid);
        }
Beispiel #8
0
        public async Task UpdateSecret(VehiclePolicies policydata)
        {
            //Create the updated Policy data to be stored as a new version of the Secret
            Insdata akvdata = new Insdata
            {
                Id         = policydata.Id,
                Inscompany = policydata.Inscompany,
                Policyno   = policydata.Policyno,
                Userid     = policydata.Userid,
                Vehicleno  = policydata.Vehicleno
            };

            //Create the JSON String of the updated Policy Object
            string insurancepolicysecret = JsonConvert.SerializeObject(akvdata);

            byte[] datatoencrypt = System.Text.Encoding.UTF8.GetBytes(insurancepolicysecret);
            string keyUri        = string.Format("https://{0}.vault.azure.net/keys/{1}", _keyVaultName, _keyName);
            string keyVaultUri   = string.Format("https://{0}.vault.azure.net", _keyVaultName);


            KeyOperationResult result = null;
            //Get the metadata from the existing Secret in Key Vault
            SecretBundle bundle = await _keyVaultClient.GetSecretAsync(keyVaultUri, policydata.Uidname);

            if (bundle == null)
            {
                throw new ApplicationException("Error locating Secret data to update");
                //No need to execute the rest of the steps if the Secret cannot be retrieved
            }
            SecretAttributes             _attribs     = bundle.Attributes;
            string                       _contentType = bundle.ContentType;
            IDictionary <string, string> dic          = bundle.Tags;

            //Create the attributes for the updated Secret
            SecretAttributes attribsNew = new SecretAttributes
            {
                Enabled   = true,
                Expires   = _attribs.Expires,
                NotBefore = DateTime.UtcNow
            };

            IDictionary <string, string> alltags = dic;
            string contentType = _contentType;

            // Encrypt the updated Secret data
            result = await _keyVaultClient.EncryptAsync(keyUri, JsonWebKeyEncryptionAlgorithm.RSAOAEP256,
                                                        datatoencrypt);

            byte[] encdata       = result.Result;
            string encrypteddata = Convert.ToBase64String(encdata);

            //Create a new version of the Secret by calling the SetSecret Method, and using the attributes from the previous version of the Secret
            bundle = await _keyVaultClient.SetSecretAsync(keyVaultUri, policydata.Uidname,
                                                          encrypteddata, alltags, contentType, attribsNew);

            string bundlestr = bundle.Value;

            policydata.Version   = bundle.SecretIdentifier.Version;
            policydata.Lastmod   = bundle.Attributes.Updated;
            policydata.Startdate = bundle.Attributes.NotBefore;
            policydata.Enddate   = bundle.Attributes.Expires;
        }
Beispiel #9
0
        public async Task <IActionResult> PutVehPoliciesMaster([FromRoute] string id, [FromBody] VehiclePolicies vehPoliciesMaster)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != vehPoliciesMaster.Id)
            {
                return(BadRequest());
            }

            _context.Entry(vehPoliciesMaster).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!VehPoliciesMasterExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }