public Identity CreateIdentity(string email, string password, Roles role = Roles.User)
        {
            Identity result = null;

            var identity = new Identity ()
            {
                ExternalIdentifier = Guid.NewGuid().ToString(),
                Email = email,
                Password = this._PasswordUtility.HashPassword(password ?? this._PasswordUtility.Generate ()),
                Role = role
            };

            result = this._IdentityRepository.Add (identity);

            this._Logger.Debug("Identity created : {0}", result != null);

            if (result != null) {
                try
                {
                    var configuration = ConfigurationHelper.Get<ApplicationConfiguration> ();

                    if (configuration != null) {
                        this.SendIdentityEmail (identity, "AccountActivation");
                    }
                }
                catch(Exception exception)
                {
                    this._Logger.ErrorException("Erreur lors de l'envoi du mail d'activation à " + email + " : " + exception.ToString(), exception);
                }
            }

            return result;
        }
        public UserIdentity(Identity identity)
        {
            this.UserName = identity.ExternalIdentifier;

            var roles = Enum.GetValues(typeof(Roles));
            var userRoles = new List<string>();

            foreach(var role in roles)
            {
                if((Roles)role <= identity.Role)
                {
                    userRoles.Add(role.ToString());
                }
            }

            this.Claims = userRoles;
        }
        private Identity UpdateIdentityEmail(Identity identity, string newEmail)
        {
            // Try to load the identity
            Identity result = null;

            // If identity supplied
            if (identity != null) {
                // Update its email
                identity.Email = newEmail;
                identity.EmailConfirmed = false;

                // Persist the changes
                result = this._IdentityRepository.Update (identity);

                if (result != null) {
                    this.SendIdentityEmail (identity, "AccountActivationEmailChanged");
                }
            }

            return result;
        }
        private void SendIdentityEmail(Identity identity, string mailTemplateId)
        {
            var configuration = ConfigurationHelper.Get<ApplicationConfiguration> ();

            if (configuration != null) {
                var template = this._MailTemplateRepository.GetByName (mailTemplateId);

                if (template != null) {
                    FluentEmail
                        //.From (configuration.AddressForEmailFromSystem, configuration.NameForEmailFromSystem)
                        .FromDefault()
                        .To (identity.Email)
                        .UsingTemplate (template, identity)
                        .Send ();
                } else {
                    throw new Exception ("Could not find the mail template with code \"" + mailTemplateId + "\"");
                }
            }
        }