public static RelyingPartyModel ToViewModel(this RelyingParty relyingParty)
        {
            var model = new RelyingPartyModel
            {
                Name = relyingParty.Name,
                Realm = relyingParty.Realm.AbsoluteUri,
                ExtraData1 = relyingParty.ExtraData1,
                ExtraData2 = relyingParty.ExtraData2,
                ExtraData3 = relyingParty.ExtraData3
            };

            if (relyingParty.EncryptingCertificate != null)
            {
                model.EncryptingCertificate = Convert.ToBase64String(relyingParty.EncryptingCertificate.RawData);
                model.EncryptingCertificateName = relyingParty.EncryptingCertificate.Subject;
            };

            if (relyingParty.ReplyTo != null)
            {
                model.ReplyTo = relyingParty.ReplyTo.AbsoluteUri;
            }

            if (relyingParty.SymmetricSigningKey != null && relyingParty.SymmetricSigningKey.Length != 0)
            {
                model.SymmetricSigningKey = Convert.ToBase64String(relyingParty.SymmetricSigningKey);
            }

            return model;
        }
        public static RelyingParty ToDomainModel(this RelyingPartyModel model)
        {
            var rp = new RelyingParty
            {
                Id         = model.Id,
                Name       = model.Name,
                Realm      = new Uri(model.Realm),
                ExtraData1 = model.ExtraData1,
                ExtraData2 = model.ExtraData2,
                ExtraData3 = model.ExtraData3,
            };

            if (!string.IsNullOrWhiteSpace(model.ReplyTo))
            {
                rp.ReplyTo = new Uri(model.ReplyTo);
            }

            if (!string.IsNullOrWhiteSpace(model.EncryptingCertificate))
            {
                rp.EncryptingCertificate = new X509Certificate2(Convert.FromBase64String(model.EncryptingCertificate));
            }

            if (!string.IsNullOrWhiteSpace(model.SymmetricSigningKey))
            {
                rp.SymmetricSigningKey = Convert.FromBase64String(model.SymmetricSigningKey);
            }

            return(rp);
        }
        public ActionResult Edit(RelyingPartyModel relyingParty)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    TrySetCertificateFromUpload(relyingParty);
                    Repository.Update(relyingParty.ToDomainModel());

                    return RedirectToAction("Index");
                }

                return View(relyingParty);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    ModelState.AddModelError("", ex.InnerException.Message);
                }
                else
                {
                    ModelState.AddModelError("", ex.Message);
                }

                return View(relyingParty);
            }
        }
        public static RelyingPartyModel ToViewModel(this RelyingParty relyingParty)
        {
            var model = new RelyingPartyModel
            {
                Name       = relyingParty.Name,
                Realm      = relyingParty.Realm.AbsoluteUri,
                ExtraData1 = relyingParty.ExtraData1,
                ExtraData2 = relyingParty.ExtraData2,
                ExtraData3 = relyingParty.ExtraData3
            };

            if (relyingParty.EncryptingCertificate != null)
            {
                model.EncryptingCertificate     = Convert.ToBase64String(relyingParty.EncryptingCertificate.RawData);
                model.EncryptingCertificateName = relyingParty.EncryptingCertificate.Subject;
            }
            ;

            if (relyingParty.ReplyTo != null)
            {
                model.ReplyTo = relyingParty.ReplyTo.AbsoluteUri;
            }

            if (relyingParty.SymmetricSigningKey != null && relyingParty.SymmetricSigningKey.Length != 0)
            {
                model.SymmetricSigningKey = Convert.ToBase64String(relyingParty.SymmetricSigningKey);
            }

            return(model);
        }
        private void TrySetCertificateFromUpload(RelyingPartyModel relyingParty)
        {
            if (relyingParty.CertificateUpload != null && relyingParty.CertificateUpload.ContentLength > 0)
            {
                byte[] bytes = new byte[relyingParty.CertificateUpload.InputStream.Length];
                relyingParty.CertificateUpload.InputStream.Read(bytes, 0, bytes.Length);

                relyingParty.EncryptingCertificate = Convert.ToBase64String(bytes);
            }
        }