public UserInfo CreateUser(UserInfo user)
{
try
{
TrainingCloudDataContext _dataContext = new TrainingCloudDataContext(); //Linq to SQL DB
var _existUserAccount = _dataContext.UserInfos.SingleOrDefault(u => u.UserAccount == user.UserAccount); // 判斷該用戶是否存在
if (_existUserAccount != null) //如果用戶已經存在
{
throw new UserAccountExistException(user.UserAccount);
}
UserInfo _userAccount = new UserInfo();
_userAccount.UserAccount = user.UserAccount;
_userAccount.Password = user.Password;
_userAccount.Email = user.Email;
_userAccount.Phone = user.Phone;
_dataContext.UserInfos.InsertOnSubmit(_userAccount); //插入數據庫
_dataContext.SubmitChanges();
return GetUserInfo(user.UserAccount);
}
catch (Exception ex)
{
throw new Exception("Create User Account failed:" + ex.Message);
}
//throw new NotImplementedException();
}
public bool DeleteUser(UserInfo user)
{
try
{
TrainingCloudDataContext _dataContext = new TrainingCloudDataContext();
var _existUserAccount = _dataContext.UserInfos.SingleOrDefault(u => u.UserAccount == user.UserAccount);
if (_existUserAccount == null)
{
throw new UserAccountNotExistException(user.UserAccount);
}
_dataContext.UserInfos.DeleteOnSubmit(_existUserAccount);
_dataContext.SubmitChanges();
return true;
}
catch (Exception ex)
{
throw new Exception("Delete User failed:" + ex.Message);
}
//throw new NotImplementedException();
}
public UserInfo GetUserInfo(string userAccount)
{
try
{
TrainingCloudDataContext _dataContext = new TrainingCloudDataContext();
var _existUserAccount = _dataContext.UserInfos.SingleOrDefault(u => u.UserAccount == userAccount);
if (null == _existUserAccount)
{
throw new UserAccountNotExistException(userAccount);
}
return ConvertDBUserToContact(_existUserAccount);
}
catch (Exception ex)
{
throw new Exception("Get User Info failed:" + ex.Message);
}
//throw new NotImplementedException();
}
public bool VerifyUserLogOn(string accountName, string password)
{
try
{
TrainingCloudDataContext _dataContext = new TrainingCloudDataContext();
var _existUserAccount = _dataContext.UserInfos.SingleOrDefault(u => u.UserAccount == accountName);
if (null == _existUserAccount)
{
throw new UserAccountNotExistException(accountName);
}
else
{
string _passWord = HashEncryptPassword(password); //加密
if (_passWord != _existUserAccount.Password)
{
throw new UserPassWordNotMatchException();
}
else
{
return true;
}
}
}
catch (Exception ex)
{
throw new Exception("Log on failed:" + ex.Message);
}
throw new NotImplementedException();
}
private UserInfo ConvertDBUserToContact(UserInfo user)
{
return new UserInfo() { UserAccount = user.UserAccount, Email = user.Email, Phone = user.Phone };
}
private string HashEncryptPassword(string password)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
}
#endregion