Example #1
0
        public SessionObject Login(string loginIdorEmail, string hashedPassword, int deviceType = 0, string clientId = "")
        {
            if (string.IsNullOrEmpty(loginIdorEmail))
            {
                throw new ApiException("username can't be empty.", "RequireParameter_username");
            }
            if (string.IsNullOrEmpty(hashedPassword))
            {
                throw new ApiException("hashedPassword can't be empty.", "RequireParameter_hashedPassword");
            }

            int timeout = 60;

            var nowUser = _authenticationService.GetUserByLoginId(loginIdorEmail);

            if (nowUser == null)
            {
                throw new ApiException("Account Not Exists", "Account_NotExits");
            }

            #region Verify Password
            if (!string.Equals(nowUser.Password, hashedPassword))
            {
                throw new ApiException("Wrong Password", "Account_WrongPassword");
            }
            #endregion

            if (!nowUser.IsActive)
            {
                throw new ApiException("The user is inactive.", "InactiveUser");
            }

            UserDevice existsDevice = _authenticationService.GetUserDevice(nowUser.UserId, deviceType);// Session.QueryOver<UserDevice>().Where(x => x.AccountId == nowAccount.Id && x.DeviceType == deviceType).SingleOrDefault();
            if (existsDevice == null)
            {
                string passkey = MD5CryptoProvider.GetMD5Hash(nowUser.UserId + nowUser.LoginName + DateTime.UtcNow.ToString() + Guid.NewGuid().ToString());
                existsDevice = new UserDevice()
                {
                    UserId      = nowUser.UserId,
                    CreateTime  = DateTime.UtcNow,
                    ActiveTime  = DateTime.UtcNow,
                    ExpiredTime = DateTime.UtcNow.AddMinutes(timeout),
                    DeviceType  = deviceType,
                    SessionKey  = passkey
                };

                _authenticationService.AddUserDevice(existsDevice);
            }
            else
            {
                existsDevice.ActiveTime  = DateTime.UtcNow;
                existsDevice.ExpiredTime = DateTime.UtcNow.AddMinutes(timeout);
                _authenticationService.UpdateUserDevice(existsDevice);
            }
            nowUser.Password = "";
            return(new SessionObject()
            {
                SessionKey = existsDevice.SessionKey, LogonUser = nowUser
            });
        }
Example #2
0
        public SessionObject Login(string phone, string hashedPassword, int deviceType = 0, string clientId = "")
        {
            if (string.IsNullOrEmpty(phone))
            {
                throw new ApiException("用户名不能为空。", "RequireParameter_userphone");
            }
            if (string.IsNullOrEmpty(hashedPassword))
            {
                throw new ApiException("hashedPassword 不能为空.", "RequireParameter_hashedPassword");
            }

            int timeout = 60;

            var nowUser = _authenticationService.GetUserByPhone(phone);

            if (nowUser == null)
            {
                throw new ApiException("帐户不存在", "Account_NotExits");
            }

            #region 验证密码
            if (!string.Equals(nowUser.Password, hashedPassword))
            {
                throw new ApiException("错误的密码", "Account_WrongPassword");
            }
            #endregion

            if (!nowUser.IsActive)
            {
                throw new ApiException("用户处于非活动状态.", "InactiveUser");
            }

            UserDevice existsDevice = _authenticationService.GetUserDevice(nowUser.UserId, deviceType);
            // Session.QueryOver<UserDevice>().Where(x => x.AccountId == nowAccount.Id && x.DeviceType == deviceType).SingleOrDefault();
            if (existsDevice == null)
            {
                string passkey = MD5CryptoProvider.GetMD5Hash(nowUser.UserId + nowUser.Phone + DateTime.UtcNow + Guid.NewGuid());
                existsDevice = new UserDevice()
                {
                    UserId      = nowUser.UserId,
                    CreateTime  = DateTime.UtcNow,
                    ActiveTime  = DateTime.UtcNow,
                    ExpiredTime = DateTime.UtcNow.AddMinutes(timeout),
                    DeviceType  = deviceType,
                    SessionKey  = passkey
                };
                _authenticationService.AddUserDevice(existsDevice);
            }
            else
            {
                existsDevice.ActiveTime  = DateTime.UtcNow;
                existsDevice.ExpiredTime = DateTime.UtcNow.AddMinutes(timeout);
                _authenticationService.UpdateUserDevice(existsDevice);
            }
            nowUser.Password = "";
            return(new SessionObject()
            {
                SessionKey = existsDevice.SessionKey, LogonUser = nowUser
            });
        }
Example #3
0
        public ResponseDTO Login(int username, string password)//post  需要用对象表述??
        {
            string md5pwd;

            if (password == null)
            {
                md5pwd = "";
            }
            else
            {
                md5pwd = MD5CryptoProvider.GetMD5Hash(password);
            };
            UserInfo logUser = uss.GetUserInfo(username, md5pwd);

            string passkey = MD5CryptoProvider.GetMD5Hash(logUser.Id + logUser.Password + DateTime.UtcNow + Guid.NewGuid());

            logUser.SessionKey = passkey;
            uss.UpdateUserInfo(logUser);

            HttpContext.Current.Session["USERNAME"] = logUser.Password;
            string x = HttpContext.Current.Session["USERNAME"].ToString();
            IDictionary <string, object> _id = new Dictionary <string, object>();

            _id["logUser"]    = logUser;
            _id["SessionKey"] = passkey;

            return(new ResponseDTO(true, "OK", logUser));
        }
Example #4
0
        public RegisterTests()
        {
            var mapper = GetMapper();
            var roleServiceLoggerMock       = GetLoggerMock <RoleService>();
            var accauntServiceLoggerMock    = GetLoggerMock <AccountService>();
            var accauntControllerLoggerMock = GetLoggerMock <AccountController>();
            var cryptoProvider = new MD5CryptoProvider();

            var roleService    = new RoleService(UnitOfWork, mapper, roleServiceLoggerMock.Object);
            var accauntService = new AccountService(
                UnitOfWork,
                roleService,
                cryptoProvider,
                mapper,
                accauntServiceLoggerMock.Object);

            _accountController = new AccountController(accauntService, mapper, accauntControllerLoggerMock.Object);
        }
Example #5
0
        public SessionObject1 AnonymousLogin(string ip, int deviceType = 0, string clientId = "")
        {
            if (string.IsNullOrEmpty(ip))
            {
                throw new ApiException("ip地址不能为空。", "RequireParameter_ip");
            }

            int timeout = 60;

            UserDevice existsDevice = _authenticationService.GetUserDevice(ip, deviceType);

            // Session.QueryOver<UserDevice>().Where(x => x.AccountId == nowAccount.Id && x.DeviceType == deviceType).SingleOrDefault();
            if (existsDevice == null)
            {
                string passkey = MD5CryptoProvider.GetMD5Hash(ip + DateTime.UtcNow + Guid.NewGuid());
                existsDevice = new UserDevice()
                {
                    IP          = ip,
                    CreateTime  = DateTime.UtcNow,
                    ActiveTime  = DateTime.UtcNow,
                    ExpiredTime = DateTime.UtcNow.AddMinutes(timeout),
                    DeviceType  = deviceType,
                    SessionKey  = passkey
                };
                _authenticationService.AddUserDevice(existsDevice);
            }
            else
            {
                existsDevice.ActiveTime  = DateTime.UtcNow;
                existsDevice.ExpiredTime = DateTime.UtcNow.AddMinutes(timeout);
                _authenticationService.UpdateUserDevice(existsDevice);
            }
            return(new SessionObject1()
            {
                SessionKey = existsDevice.SessionKey, Ip = ip
            });
        }