コード例 #1
0
        public virtual async Task <JsonResult> AddCertificate(string accountName, HttpPostedFileBase uploadFile)
        {
            if (uploadFile == null)
            {
                return(Json(HttpStatusCode.BadRequest, new[] { Strings.CertificateFileIsRequired }));
            }

            var currentUser = GetCurrentUser();
            var account     = GetAccount(accountName);

            if (currentUser == null)
            {
                return(Json(HttpStatusCode.Unauthorized));
            }

            if (account == null)
            {
                return(Json(HttpStatusCode.NotFound));
            }

            if (ActionsRequiringPermissions.ManageAccount.CheckPermissions(currentUser, account)
                != PermissionsCheckResult.Allowed || !User.WasMultiFactorAuthenticated())
            {
                return(Json(HttpStatusCode.Forbidden, new { Strings.Unauthorized }));
            }

            Certificate certificate;

            try
            {
                using (var uploadStream = uploadFile.InputStream)
                {
                    certificate = await CertificateService.AddCertificateAsync(uploadFile);
                }

                await CertificateService.ActivateCertificateAsync(certificate.Thumbprint, account);
            }
            catch (UserSafeException ex)
            {
                ex.Log();

                return(Json(HttpStatusCode.BadRequest, new[] { ex.Message }));
            }

            var activeCertificateCount = CertificateService.GetCertificates(account).Count();

            if (activeCertificateCount == 1 &&
                SecurityPolicyService.IsSubscribed(account, AutomaticallyOverwriteRequiredSignerPolicy.PolicyName))
            {
                await PackageService.SetRequiredSignerAsync(account);
            }

            return(Json(HttpStatusCode.Created, new { certificate.Thumbprint }));
        }