public virtual void AuthorizeMachine(Guid applicationInstance, MachineKey machineKey)
        {
            if (!((MembershipProvider)Membership.Provider).IsValidApplicationMember(applicationInstance, machineKey.Email))
            {
                throw new KeyNotFoundException(machineKey.Email);
            }

            using (var Entities = Repository)
            {
                //Entities.Users_v1_0.Where(u => u.userName == System.Threading.Thread.CurrentPrincipal.Identity.Name).Single().contactId;
                var authority = GetCurrentAuthority(applicationInstance);
                var contacts  = Entities.ContactEmails_v1_0.Where(e => e.Email_v1_0.formattedEmailAddress == machineKey.Email);
                if (contacts.Count() == 0)
                {
                    throw new KeyNotFoundException(machineKey.Email);
                }
                else
                {
                    Contacts_v1_0 contact = contacts.Single().Contacts_v1_0;

                    if (String.IsNullOrEmpty(contact.firstName))
                    {
                        contact.firstName = machineKey.Contact.FirstName;
                    }

                    if (String.IsNullOrEmpty(contact.lastName))
                    {
                        contact.lastName = machineKey.Contact.LastName;
                    }

                    var instances = Entities.ApplicationInstanceMachineKeys_v1_0.Where(i => i.MachineKeys_v1_0.machineKey == machineKey.Key && i.ApplicationInstance_v1_0.instanceUID == applicationInstance);
                    if (instances.Count() == 0)
                    {
                        var machines = Entities.MachineKeys_v1_0.Where(m => m.machineKey == machineKey.Key);
                        if (machines.Count() == 0)
                        {
                            Entities.ApplicationInstanceMachineKeys_v1_0.AddObject(new ApplicationInstanceMachineKeys_v1_0()
                            {
                                ApplicationInstance_v1_0 = Entities.ApplicationInstance_v1_0.Single(a => a.instanceUID == applicationInstance),
                                MachineKeys_v1_0         = new MachineKeys_v1_0()
                                {
                                    contactId = contact.id, machineKey = machineKey.Key, createDate = DateTime.Now, machineDescription = (contact.lastName ?? "") + "," + (contact.firstName ?? "")
                                },
                                authorized     = true,
                                authorizedBy   = authority,
                                authorizedDate = DateTime.Now,
                                createDate     = DateTime.Now,
                                createdBy      = authority
                            });
                        }
                        else
                        {
                            var machine = machines.Single();
                            if (machine.contactId != contact.id)
                            {
                                throw new InvalidOperationException("Machine is not asscoiated with the contact");
                            }
                            Entities.ApplicationInstanceMachineKeys_v1_0.AddObject(new ApplicationInstanceMachineKeys_v1_0()
                            {
                                ApplicationInstance_v1_0 = Entities.ApplicationInstance_v1_0.Single(a => a.instanceUID == applicationInstance),
                                machineKeyId             = machine.id,
                                authorized     = true,
                                authorizedBy   = authority,
                                authorizedDate = DateTime.Now,
                                createDate     = DateTime.Now,
                                createdBy      = authority
                            });
                        }
                        Entities.SaveChanges();
                    }
                    else
                    {
                        var instance = instances.Single();
                        instance.authorized       = true;
                        instance.lastModifiedBy   = authority;
                        instance.lastModifiedDate = DateTime.Now;
                        Entities.SaveChanges();
                    }
                }
            }
        }
        public MembershipUser CreateContact(string firstname, string lastname, string middlename, string email, string username, string password, string passwordQuestion, string passwordAnswer, bool isApproved, out System.Web.Security.MembershipCreateStatus status)
        {
            if (String.IsNullOrEmpty(username))
            {
                throw new ArgumentException("Value cannot be null or empty.", "firstname");
            }
            if (String.IsNullOrEmpty(username))
            {
                throw new ArgumentException("Value cannot be null or empty.", "lastname");
            }
            if (String.IsNullOrEmpty(email))
            {
                throw new ArgumentException("Value cannot be null or empty.", "email");
            }
            if (String.IsNullOrEmpty(username))
            {
                throw new ArgumentException("Value cannot be null or empty.", "userName");
            }
            if (String.IsNullOrEmpty(password))
            {
                throw new ArgumentException("Value cannot be null or empty.", "password");
            }

            int count;

            FindUsersByEmail(email, 0, 1, out count);
            if (count == 0)
            {
                FindUsersByName(username, 0, 1, out count);
            }
            else
            {
                status = MembershipCreateStatus.DuplicateEmail;
                return(null);
            }

            if (count == 0)
            {
                System.Net.Mail.MailAddress address = new System.Net.Mail.MailAddress(email);
                Contact contact = null;
                using (var Entities = Repository)
                {
                    var contacts = Entities.ContactEmails_v1_0.Where(e => e.Email_v1_0.formattedEmailAddress == email);
                    if (contacts.Count() == 1)
                    {
                        status = MembershipCreateStatus.DuplicateEmail;
                        return(null);
                    }
                    else
                    {
                        contact = new Contacts_v1_0
                        {
                            firstName           = firstname,
                            lastName            = lastname,
                            middleName          = middlename,
                            givenName           = firstname,
                            firstPartOfLastName = lastname.GetCharLeftPart(),
                            createDate          = DateTime.Now
                        };
                        Email_v1_0 mail = new Email_v1_0
                        {
                            formattedEmailAddress = address.Address,
                            domainName            = address.Host.Split('.')[0],
                            extension             = address.Host.Split('.')[1],
                            localpart             = address.User,
                            emailTypeId           = 2 //TODO:Detect
                        };
                        ContactEmails_v1_0 contactMail = new ContactEmails_v1_0
                        {
                            Contacts_v1_0 = contact,
                            Email_v1_0    = mail
                        };

                        return(CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, contact, out status));
                    }
                }
            }
            else
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return(null);
            }
        }
        public MembershipUser CreateContact(string firstname, string lastname, string middlename, string email, string username, string password, string passwordQuestion, string passwordAnswer, bool isApproved, out System.Web.Security.MembershipCreateStatus status)
        {
            if (String.IsNullOrEmpty(username)) throw new ArgumentException("Value cannot be null or empty.", "firstname");
            if (String.IsNullOrEmpty(username)) throw new ArgumentException("Value cannot be null or empty.", "lastname");
            if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");
            if (String.IsNullOrEmpty(username)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");
    
            int count;
            FindUsersByEmail(email, 0, 1, out count);
            if (count == 0)
                FindUsersByName(username, 0, 1, out count);
            else
            {
                status = MembershipCreateStatus.DuplicateEmail;
                return null;
            }

            if (count == 0)
            {
                System.Net.Mail.MailAddress address = new System.Net.Mail.MailAddress(email);
                Contact contact = null;
                using (var Entities = Repository)
                {
                    var contacts = Entities.ContactEmails_v1_0.Where(e => e.Email_v1_0.formattedEmailAddress == email);
                    if (contacts.Count() == 1)
                    {
                        status = MembershipCreateStatus.DuplicateEmail;
                        return null;
                    }
                    else
                    {
                        contact = new Contacts_v1_0
                        {
                            firstName = firstname,
                            lastName = lastname,
                            middleName = middlename,
                            givenName = firstname,
                            firstPartOfLastName = lastname.GetCharLeftPart(),
                            createDate = DateTime.Now
                        };
                        Email_v1_0 mail = new Email_v1_0
                        {
                            formattedEmailAddress = address.Address,
                            domainName = address.Host.Split('.')[0],
                            extension = address.Host.Split('.')[1],
                            localpart = address.User,
                            emailTypeId = 2 //TODO:Detect
                        };
                        ContactEmails_v1_0 contactMail = new ContactEmails_v1_0
                        {
                            Contacts_v1_0 = contact,
                            Email_v1_0 = mail
                        };

                        return CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, contact, out status);
                    }
                }
            }
            else
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return null;
            }
              
        }