Beispiel #1
0
        public void Register(string login, string password, string firstName, string lastName, string role)
        {
            if (string.IsNullOrEmpty(login)) throw new ArgumentNullException("login");
            if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password");
            if (string.IsNullOrEmpty(firstName)) throw new ArgumentNullException("firstName");
            if (string.IsNullOrEmpty(lastName)) throw new ArgumentNullException("lastName");
            if (string.IsNullOrEmpty(role)) throw new ArgumentNullException("role");

            var existingRole = Context.Roles.FirstOrDefault(x => x.Name == role);
            if (existingRole == null)
            {
                throw new ArgumentException(string.Format("Role {0} does not exist", role));
            }

            var user = new User
            {
                Id = Guid.NewGuid(),
                Login = login,
                Password = password.GetSHA512(),
                FirstName = firstName,
                LastName = lastName,
                Role = existingRole
            };

            Context.Users.Add(user);
            Context.SaveChanges();
        }
Beispiel #2
0
        internal void Login(string username, TethysContext context)
        {
            var user = context.Users.FirstOrDefault(x => x.Login == username);

            if (user == null) return;

            LoggedUser = user;
        }
Beispiel #3
0
        public bool TryLogin(string login, string password, TethysContext context)
        {
            var user = context.Users.Include("Role").FirstOrDefault(x => x.Login == login);

            if (user == null) return false;

            if (user.Password != password.GetSHA512()) { return false; }

            LoggedUser = user;
            HttpContext.Current.Session.Timeout = 24 * 60;

            return true;
        }
Beispiel #4
0
 public void Logout()
 {
     LoggedUser = null;
 }