protected static void FillIdentityUser <TKey>(ICrmIdentityUser <TKey> user, Entity entity, Action <ICrmIdentityUser <TKey>, Entity> PopulateUser = null) where TKey : IEquatable <TKey>
        {
            user.AccessFailedCount    = entity.GetAttributeValue <int>("appl_accessfailedcount");
            user.Email                = entity.GetAttributeValue <string>("appl_email");
            user.EmailConfirmed       = entity.GetAttributeValue <bool>("appl_emailconfirmed");
            user.LockoutEnabled       = entity.GetAttributeValue <bool>("appl_lockoutenabled");
            user.LockoutEndDateUtc    = entity.GetAttributeValue <DateTime?>("appl_lockoutenddateutc");
            user.PasswordHash         = entity.GetAttributeValue <string>("appl_passwordhash");
            user.PhoneNumber          = entity.GetAttributeValue <string>("appl_phonenumber");
            user.PhoneNumberConfirmed = entity.GetAttributeValue <bool>("appl_phonenumberconfirmed");
            user.TwoFactorEnabled     = entity.GetAttributeValue <bool>("appl_twofactorenabled");
            user.UserName             = entity.GetAttributeValue <string>("appl_username");
            user.Contact              = entity.Contains("appl_contactid") ? entity.GetAttributeValue <EntityReference>("appl_contactid") : null;
            user.SecurityStamp        = entity.GetAttributeValue <string>("appl_securitystamp");


            if (string.IsNullOrEmpty(user.SecurityStamp))
            {
                // Always ensure the security stamp is present. If not, create a new one and save it to the backend.
                user.SecurityStamp           = Guid.NewGuid().ToString();
                entity["appl_securitystamp"] = user.SecurityStamp;
                XrmCore.UpdateEntity(entity);
            }
            if (PopulateUser != null)
            {
                PopulateUser(user, entity);
            }
        }
 public Task ResetAccessFailedCountAsync(T user)
 {
     return(Task.Factory.StartNew <int>(() =>
     {
         user.AccessFailedCount = 0;
         XrmCore.UpdateEntity(user.AsEntity());
         return user.AccessFailedCount;
     }));
 }
 public Task AddClaimAsync(T user, System.Security.Claims.Claim claim)
 {
     return(Task.Factory.StartNew(() =>
     {
         Entity e = new Entity("appl_webuserclaim", Guid.NewGuid());
         e["appl_claimtype"] = claim.Type;
         e["appl_claimvalue"] = claim.Value;
         e["appl_webuserid"] = user.AsEntityReference();
         XrmCore.CreateEntity(e);
     }));
 }
 public Task RemoveFromRoleAsync(T user, string roleName)
 {
     return(Task.Factory.StartNew(() =>
     {
         EntityCollection col = XrmCore.GetRelated(new Entity("appl_webuser", user.Key), "appl_webuserrole", "appl_webuserid");
         Entity e = col.Entities.FirstOrDefault(x => x.GetAttributeValue <string>("appl_name").Equals(roleName, StringComparison.OrdinalIgnoreCase));
         if (e != null)
         {
             XrmCore.DeleteEntity(e);
         }
     }));
 }
 public Task CreateAsync(T user)
 {
     return(Task.Factory.StartNew(() =>
     {
         EntityCollection col = XrmCore.RetrieveByAttribute("contact", "emailaddress1", user.Email);
         if (col.Entities.Count > 0)
         {
             user.Contact = col.Entities.First().ToEntityReference();
         }
         XrmCore.CreateEntity(user.AsEntity());
     }));
 }
 public Task RemoveClaimAsync(T user, System.Security.Claims.Claim claim)
 {
     return(Task.Factory.StartNew(() =>
     {
         EntityCollection col = XrmCore.GetRelated(new Entity("appl_webuser", user.Key), "appl_webuserclaim", "appl_webuserid");
         Entity e = col.Entities.FirstOrDefault(x => x.GetAttributeValue <string>("appl_claimtype").Equals(claim.Type) && x.GetAttributeValue <string>("appl_claimvalue").Equals(claim.Value));
         if (e != null)
         {
             XrmCore.DeleteEntity(e);
         }
     }));
 }
 public Task <IList <UserLoginInfo> > GetLoginsAsync(T user)
 {
     return(Task.Factory.StartNew <IList <UserLoginInfo> >(() =>
     {
         List <UserLoginInfo> list = new List <UserLoginInfo>();
         EntityCollection col = XrmCore.GetRelated(new Entity("appl_webuser", user.Key), "appl_webuserlogin", "appl_webuserid");
         foreach (Entity e in col.Entities)
         {
             list.Add(new UserLoginInfo(e.GetAttributeValue <string>("appl_loginprovider"), e.GetAttributeValue <string>("appl_providerkey")));
         }
         return list;
     }));
 }
 public Task <IList <System.Security.Claims.Claim> > GetClaimsAsync(T user)
 {
     return(Task.Factory.StartNew <IList <System.Security.Claims.Claim> >(() =>
     {
         List <System.Security.Claims.Claim> list = new List <System.Security.Claims.Claim>();
         EntityCollection col = XrmCore.GetRelated(new Entity("appl_webuser", user.Key), "appl_webuserclaim", "appl_webuserid");
         foreach (Entity e in col.Entities)
         {
             list.Add(new System.Security.Claims.Claim(e.GetAttributeValue <string>("appl_claimtype"), e.GetAttributeValue <string>("appl_claimvalue")));
         }
         return list;
     }));
 }
 public Task RemoveLoginAsync(T user, UserLoginInfo login)
 {
     return(Task.Factory.StartNew(() =>
     {
         EntityCollection col = XrmCore.GetRelated(new Entity("appl_webuser", user.Key), "appl_webuserlogin", "appl_webuserid");
         Entity e = col.Entities.FirstOrDefault(x => x.GetAttributeValue <string>("appl_loginprovider").Equals(login.LoginProvider, StringComparison.OrdinalIgnoreCase) &&
                                                x.GetAttributeValue <string>("appl_providerkey").Equals(login.ProviderKey));
         if (e != null)
         {
             XrmCore.DeleteEntity(e);
         }
     }));
 }
 public Task AddToRoleAsync(T user, string roleName)
 {
     return(Task.Factory.StartNew(() =>
     {
         EntityCollection col = XrmCore.GetRelated(new Entity("appl_webuser", user.Key), "appl_webuserrole", "appl_webuserid");
         if (col.Entities.Count(x => x.GetAttributeValue <string>("appl_name").Equals(roleName, StringComparison.OrdinalIgnoreCase)) == 0)
         {
             Entity e = new Entity("appl_webuserrole", Guid.NewGuid());
             e["appl_name"] = roleName;
             e["appl_webuserid"] = new EntityReference("appl_webuser", user.Key);
             XrmCore.CreateEntity(e);
         }
     }));
 }
 public Task <T> FindAsync(UserLoginInfo login)
 {
     return(Task.Factory.StartNew <T>(() =>
     {
         Entity result = XrmCore.GetWebUserFromLogin(login.LoginProvider, login.ProviderKey);
         if (result != null)
         {
             return (T)CrmIdentityUser <S> .ConvertToIdentityUser <S>(result);
         }
         else
         {
             return default(T);
         }
     }));
 }
 public Task AddLoginAsync(T user, UserLoginInfo login)
 {
     return(Task.Factory.StartNew(() =>
     {
         EntityCollection col = new EntityCollection()
         {
             EntityName = "appl_webuserlogin"
         };
         Entity e = new Entity("appl_webuserlogin");
         e["appl_loginprovider"] = login.LoginProvider;
         e["appl_providerkey"] = login.ProviderKey;
         col.Entities.Add(e);
         XrmCore.AddRelated(new Entity("appl_webuser", user.Key), col, "appl_webuser_appl_webuserlogin");
     }));
 }
 public Task <T> FindByNameAsync(string userName)
 {
     return(Task.Factory.StartNew <T>(() =>
     {
         EntityCollection col = XrmCore.RetrieveByAttribute("appl_webuser", "appl_username", userName);
         if (col.Entities.Count > 0)
         {
             return (T)CrmIdentityUser <S> .ConvertToIdentityUser <S>(col.Entities.First());
         }
         else
         {
             return default(T);
         }
     }));
 }
        public Task <T> FindByIdAsync(S userId)
        {
            return(Task.Factory.StartNew <T>(() =>
            {
                Entity e = null;
                if (typeof(S) == typeof(Guid) || typeof(S) == typeof(string))
                {
                    e = XrmCore.Retrieve("appl_webuser", Guid.Parse(Convert.ToString(userId)));
                }
                else if (typeof(S) == typeof(int))
                {
                    EntityCollection col = XrmCore.RetrieveByAttribute("appl_webuser", "appl_userid", Convert.ToString(userId), CacheResults: false);
                    e = col.Entities.FirstOrDefault();
                }

                return e == null ? default(T): (T)CrmIdentityUser <S> .ConvertToIdentityUser <S>(e);
            }));
        }
 public void HashAllPasswords(Func <string, string> PasswordHasher)
 {
     XrmCore.HashAllPasswords(PasswordHasher);
 }
 public Task UpdateAsync(T user)
 {
     return(Task.Factory.StartNew(() => XrmCore.UpdateEntity(user.AsEntity())));
 }
 public Task DeleteAsync(T user)
 {
     return(Task.Factory.StartNew(() => XrmCore.DeleteEntity("appl_webuser", user.Key)));
 }
        public Task <IList <string> > GetRolesAsync(T user)
        {
            EntityCollection col = XrmCore.GetRelated(new Entity("appl_webuser", user.Key), "appl_webuserrole", "appl_webuserid");

            return(Task.FromResult <IList <string> >(col.Entities.Select(x => x.GetAttributeValue <string>("appl_name")).ToList()));
        }