Exemplo n.º 1
0
 private static void SetPassword(UserModel user, tb_usuario u)
 {
     if (user.xSenha != null)
     {
         var crypto = new SimpleCrypto.PBKDF2();
         var encrypPass = crypto.Compute(user.xSenha);
         u.xSenha = encrypPass;
         u.xSenhaSalt = crypto.Salt;
     }
 }
Exemplo n.º 2
0
        public bool Save(UserModel user)
        {
            try
            {
                using (var db = new PortalEntities())
                {
                    tb_usuario u = new tb_usuario();
                    if (user.idUsuario == null)
                    {
                        SetPassword(user, u);
                        u.xUserName = user.xUserName;
                        u.xFullName = user.xFullName;
                        db.tb_usuario.Add(u);

                        foreach (var prov in user.providers)
                        {
                            u.tb_providers.Add(new tb_providers
                            {
                                idUsuario = u.idUsuario,
                                Provider = prov.Provider,
                                ProviderUserId = prov.ProviderUserId
                            });
                        }

                    }
                    else
                    {
                        u = db.tb_usuario.FirstOrDefault(c => c.xUserName == user.xUserName);
                        SetPassword(user, u);
                        u.xUserName = user.xUserName;
                        u.xFullName = user.xFullName;

                        foreach (var prov in user.providers)
                        {
                            tb_providers provider = null;
                            if (u.tb_providers.Where(c => c.ProviderUserId == prov.ProviderUserId).Count() > 0)
                            {
                                provider = u.tb_providers.FirstOrDefault(c => c.ProviderUserId == prov.ProviderUserId);
                                provider.ProviderUserId = prov.ProviderUserId;
                                provider.Provider = prov.Provider;
                                provider.idUsuario = prov.idUsuario;
                            }
                            else
                            {
                                provider = new tb_providers();
                                provider.ProviderUserId = prov.ProviderUserId;
                                provider.Provider = prov.Provider;
                                provider.idUsuario = prov.idUsuario;
                                u.tb_providers.Add(provider);
                            }
                        }
                    }
                    db.SaveChanges();
                }
                return true;
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }