Exemplo n.º 1
0
        public HttpResponseMessage Login(RegisterUserModel model)
        {
            var responseMsg = PerformOperation(() =>
            {
                ValidateUsername(model.Username);
                ValidateAuthCode(model.AuthCode);
                var usernameToLower = model.Username.ToLower();
                var entityUser = this.context.Users.FirstOrDefault(u => u.Username == model.Username && u.AuthCode == model.AuthCode);
                if (entityUser == null || !entityUser.IsActive)
                {
                    throw new ArgumentException("Invalid username or password.");
                }

                entityUser.SessionKey = GenerateSessionKey(entityUser.Id);
                this.context.SaveChanges();

                var logedinUser = new RegisteredUserModel
                {
                    Username = entityUser.Username,
                    SessionKey = entityUser.SessionKey,
                    UserType = entityUser.UserType
                };
                var response = this.Request.CreateResponse(HttpStatusCode.Created, logedinUser);

                return response;

            });

            return responseMsg;
        }
Exemplo n.º 2
0
        public HttpResponseMessage Register(RegisterUserModel model)
        {
            var responseMsg = PerformOperation(() =>
            {
                ValidateUsername(model.Username);
                //ValidateNickname(model.Nickname);
                ValidateAuthCode(model.AuthCode);
                var usernameToLower = model.Username.ToLower();
                var entityUser = this.context.Users.FirstOrDefault(u => u.Username == usernameToLower || u.Nickname == model.Nickname);
                if (entityUser != null)
                {
                    return this.Request.CreateErrorResponse(HttpStatusCode.Conflict, "Username or nickname is not free.");
                }

                var user = new User
                {
                    Username = usernameToLower,
                    Nickname = model.Nickname,
                    AuthCode = model.AuthCode,
                    RegistrationDate = DateTime.Now,
                    LastVisit = DateTime.Now,
                    IsActive = true,
                    StudentNumber = GetStudentNumber(),
                    AboutMe = model.AboutMe,
                    Birthday = model.Birthday,
                    Email = model.Email,
                    Gender = model.Gender,
                    Hometown = model.Hometown,
                    Occupation = model.Occupation,
                    WebSite = model.WebSite,
                };

                this.context.Users.Add(user);
                this.context.SaveChanges();

                user.SessionKey = GenerateSessionKey(user.Id);
                this.context.SaveChanges();

                var registeredUser = new RegisteredUserModel
                {
                    Username = user.Nickname,
                    SessionKey = user.SessionKey,
                    UserType = entityUser.UserType
                };
                var response = this.Request.CreateResponse(HttpStatusCode.Created, registeredUser);

                return response;
            });

            return responseMsg;
        }