public RegisterApplicationResultMessage RegisterScriptingApplication(RegisterApplicationMessage message)
        {
            UsernameToken token = SecurityHelper.GetLicenseToken(RequestSoapContext.Current);

            RegisterApplicationResultMessage result = new RegisterApplicationResultMessage();

            // Validate license (application limit)
            if ( userDatabaseManager.ValidateApplicationLicenseLimit(token.Username) )
            {
                // Register application in database.
                bool inserted = userDatabaseManager.RegisterApplication(token.Username, message.ApplicationID);

                if ( inserted )
                {
                    // Sign application.
                    XmlDocument signedDocument = SignScriptingApplication(message.EncryptedScriptingApplicationXml);
                    result.SignedScriptingApplicationXml = signedDocument.OuterXml;
                    result.Message = "Application signed succesfully.";
                    result.IsApplicationRegistered = true;
                }
                else
                {
                    result.IsApplicationRegistered = false;
                    result.Message = "Verify that the application id is valid and try saving again the document.";
                }
            }
            else
            {
                result.IsApplicationRegistered = false;
                result.Message = "Your application license limit doesn't allow more applications to be registered. Please purchase additional licenses.";
            }

            return result;
        }
        public RegisterApplicationResultMessage IsRegisteredApplication(RegisterApplicationMessage message)
        {
            UsernameToken token = SecurityHelper.GetLicenseToken(RequestSoapContext.Current);

            RegisterApplicationResultMessage result = new RegisterApplicationResultMessage();

            if ( userDatabaseManager.RegisteredApplicationExists(token.Username, message.ApplicationID) )
            {
                result.Message = "Application exists.";
                result.IsApplicationRegistered = true;
            }
            else
            {
                result.Message = "Application needs to be registered.";
                result.IsApplicationRegistered = false;
            }

            return result;
        }
        public RegisterApplicationResultMessage RegisterScriptingApplication(RegisterApplicationMessage message)
        {
            SoapEnvelope envelope = new SoapEnvelope();
            envelope.Context.Security.Tokens.AddRange(_security.Tokens);
            envelope.SetBodyObject(message);

            return (RegisterApplicationResultMessage)base.SendRequestResponse("RegisterScriptingApplication",envelope).GetBodyObject(typeof(RegisterApplicationResultMessage));
        }
        private void btnSign_Click(object sender, System.EventArgs e)
        {
            ScriptingApplication application = new ScriptingApplication();
            application.Load(this.txtApplicationPath.Text);
            string encryptedXml = application.Encrypt();

            string hashedPassword = HashPassword(txtUsername.Text, txtPassword.Text);

            UsernameToken token = new UsernameToken(txtUsername.Text, hashedPassword, PasswordOption.SendPlainText);
            token.Id = "LicenseToken";

            // create client message.
            RegisterApplicationMessage message = new RegisterApplicationMessage();
            message.ApplicationID = application.Header.ApplicationID;
            message.EncryptedScriptingApplicationXml = encryptedXml;

            client.Security = new Security();
            client.Security.Tokens.Add(token);
            client.BeginRegisterScriptingApplication(
                message,
                new MessageResultHandler(SignScriptingAppInvoker),
                null);
        }
        public IAsyncResult BeginRegisterScriptingApplication(RegisterApplicationMessage message, MessageResultHandler callback, object state)
        {
            SoapEnvelope envelope = new SoapEnvelope();
            envelope.Context.Security.Tokens.AddRange(_security.Tokens);
            envelope.SetBodyObject(message);

            _clientHandler = callback;
            return base.BeginSendRequestResponse("RegisterScriptingApplication",envelope,new AsyncCallback(EndMessageResultHandler),typeof(RegisterApplicationResultMessage));
        }