/// <summary> /// return /// </summary> /// <param name="email"></param> /// <param name="password"></param> /// <returns></returns> public IUserDTO Login(Interfaces.IUserDTO udto) { if (string.IsNullOrWhiteSpace(udto.Email) || string.IsNullOrWhiteSpace(udto.UserPassword)) { return(null); } User u = _ctx.Users.FirstOrDefault(y => y.Email == udto.Email.ToLower()); if (u == null) { return(null); } if (!VerifyPasswordHash(udto.UserPassword, u.PasswordHash, u.PasswordSalt)) { return(null); } var returnUdto = new DAL.Model.DataTransferObjects.UserDTO { Email = u.Email, IsAdmin = u.IsAdmin, UserName = u.UserName, UserId = u.UserId }; return(returnUdto); }
public void Registration(Interfaces.IUserDTO udto) { if (string.IsNullOrWhiteSpace(udto.UserPassword)) { throw new ApplicationException("Password is needed"); } if (string.IsNullOrWhiteSpace(udto.UserName)) { throw new ApplicationException("Username is needed"); } if (_ctx.Users.Any(x => x.UserName == udto.UserName)) { throw new ApplicationException("Username is taken"); } if (_ctx.Users.Any(x => x.Email == udto.Email)) { throw new ApplicationException("There is a registered user with this e-mail address"); } byte[] passwordHash, passwordSalt; CreatePasswordHash(udto.UserPassword, out passwordHash, out passwordSalt); User u = new User { Email = udto.Email.ToLower(), PasswordHash = passwordHash, PasswordSalt = passwordSalt, UserName = udto.UserName }; _ctx.Add(u); _ctx.SaveChanges(); }