示例#1
0
        public Tuple <AuthenticationResult, User> AddUser(string name, string family, string username, string password, string email, DateTime birthday)
        {
            User _user = new User();

            _user = _dbContext.Users.FirstOrDefault(user => user.UserName == username);
            if (_user != null)
            {
                return(new Tuple <AuthenticationResult, User>(new AuthenticationResult(AuthenticationResultCode.ClientConnectivityError), null));
            }

            _user            = new User();
            _user.Name       = name;
            _user.FamilyName = family;
            _user.UserName   = username;
            _user.Password   = SecurityParameters.MD5Encryption(password);
            _user.Email      = email;
            _user.BirthDate  = birthday;

            _dbContext.Users.Add(_user);
            _dbContext.SaveChanges();

            return(new Tuple <AuthenticationResult, User>(new AuthenticationResult(AuthenticationResultCode.AuthenticationSuccess), _user));
        }
        public Tuple <AuthenticationResult, User> AuthenticateUser(LoginViewModel loginModel, string clientIPAddress)
        {
            if (loginModel.Username == null || loginModel.Password == null || loginModel.ServiceAccessType == ServiceAccesType.None)
            {
                return(new Tuple <AuthenticationResult, User>(new AuthenticationResult(AuthenticationResultCode.InvalidInputParams), null));
            }

            User _user = _dbContext.Users.FirstOrDefault(user => user.UserName == loginModel.Username && user.Password == SecurityParameters.MD5Encryption(loginModel.Password));

            if (_user != null)
            {
                byte[] bHashPassword = MD5.Create().ComputeHash(new System.Text.ASCIIEncoding().GetBytes(loginModel.Password));
                string hashPassword  = "";
                for (int index = 0; index < bHashPassword.Length; index++)
                {
                    hashPassword += string.Format("{0:X2}", bHashPassword[index]);
                }

                if (hashPassword == _user.Password)
                {
                    return(new Tuple <AuthenticationResult, User>(new AuthenticationResult(AuthenticationResultCode.AuthenticationSuccess), _user));
                }

                return(new Tuple <AuthenticationResult, User>(new AuthenticationResult(AuthenticationResultCode.AuthenticationFailure), _user));
            }

            return(new Tuple <AuthenticationResult, User>(new AuthenticationResult(AuthenticationResultCode.AuthenticationFailure), null));
        }
示例#3
0
 public void Add(User item)
 {
     item.Password = SecurityParameters.MD5Encryption(item.Password);
     _dbContext.Users.Add(item);
     _dbContext.SaveChanges();
 }