Esempio n. 1
0
 public User Login(string id, string password)
 {
     using (var ctx = new GitCandyContext())
     {
         var user = ctx.Users.FirstOrDefault(s => s.Name == id || s.Email == id);
         if (user != null)
         {
             using (var pp1 = PasswordProviderPool.Take(user.PasswordVersion))
                 if (user.Password == pp1.Compute(user.ID, user.Name, password))
                 {
                     if (user.PasswordVersion != PasswordProviderPool.LastVersion)
                     {
                         using (var pp2 = PasswordProviderPool.Take())
                         {
                             user.Password        = pp2.Compute(user.ID, user.Name, password);
                             user.PasswordVersion = pp2.Version;
                             ctx.SaveChanges();
                         }
                     }
                     return(user);
                 }
         }
         return(null);
     }
 }
Esempio n. 2
0
        public User CreateAccount(string name, string nickname, string password, string email, string description, out bool badName, out bool badEmail)
        {
            badName  = false;
            badEmail = false;

            using (var ctx = new GitCandyContext())
            //using (TransactionScope transaction = new TransactionScope()) // I don't know why Sqlite not support for TransactionScope
            {
                try
                {
                    var list = ctx.Users.Where(s => s.Name == name || s.Email == email).ToList();
                    badName  = list.Any(s => string.Equals(s.Name, name, StringComparison.OrdinalIgnoreCase));
                    badEmail = list.Any(s => string.Equals(s.Email, email, StringComparison.OrdinalIgnoreCase));

                    if (badName || badEmail)
                    {
                        return(null);
                    }

                    var user = new User
                    {
                        Name            = name,
                        Nickname        = nickname,
                        Email           = email,
                        PasswordVersion = -1,
                        Password        = "",
                        Description     = description,
                        CreationDate    = DateTime.UtcNow,
                    };
                    ctx.Users.Add(user);
                    ctx.SaveChanges();

                    using (var pp = PasswordProviderPool.Take())
                    {
                        user.PasswordVersion = pp.Version;
                        user.Password        = pp.Compute(user.ID, name, password);
                    }
                    ctx.SaveChanges();

                    //transaction.Complete();
                    return(user);
                }
                catch
                {
                    return(null);
                }
            }
        }
Esempio n. 3
0
        public void SetPassword(string name, string newPassword)
        {
            using (var ctx = new GitCandyContext())
            {
                var user = ctx.Users.FirstOrDefault(s => s.Name == name);
                if (user != null)
                {
                    using (var pp = PasswordProviderPool.Take())
                    {
                        user.Password        = pp.Compute(user.ID, user.Name, newPassword);
                        user.PasswordVersion = pp.Version;
                    }

                    var auths = ctx.AuthorizationLogs.Where(s => s.UserID == user.ID);
                    foreach (var auth in auths)
                    {
                        auth.IsValid = false;
                    }
                    ctx.SaveChanges();
                }
            }
        }