private string AddSessionKey(ExamContext context, Users foundUser) { string sessionKey = this.GenerateSessionKey(foundUser.Id); foundUser.SessionKey = sessionKey; context.Entry(foundUser).State = System.Data.EntityState.Modified; context.SaveChanges(); return sessionKey; }
public HttpResponseMessage PostRegisterUser(UserRegisterDTO userJSON) { var responseMsg = base.PerformOperationAndHandleExceptions( () => { if (ModelState.IsValid && userJSON != null) { var context = new ExamContext(); using (context) { this.ValidateUsername(userJSON.Username); this.ValidateDisplayName(userJSON.DisplayName); this.ValidAuthCode(userJSON.AuthCode); IsUserExisting(userJSON, context); Users createdUser = CreateNewUSer(userJSON, context); string sessionKey = this.GenerateSessionKey(createdUser.Id); createdUser.SessionKey = sessionKey; context.Entry(createdUser).State = System.Data.EntityState.Modified; context.SaveChanges(); UserRegisterResponseDTO user = new UserRegisterResponseDTO() { DisplaName = userJSON.DisplayName, Sessionkey = sessionKey, }; var response = this.Request.CreateResponse(HttpStatusCode.Created, user); return response; } } else { var errors = String.Join(" ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); var errorMessage = string.Format("User input validation failed. Errors: {0}", errors); throw new ArgumentException(errorMessage); } }); return responseMsg; }
public HttpResponseMessage PutLogoutUser( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = base.PerformOperationAndHandleExceptions(() => { if (ModelState.IsValid) { var context = new ExamContext(); using (context) { if (sessionKey == null) { throw new ArgumentNullException("Missing sessionKey"); } var user = context.Users.FirstOrDefault(x => x.SessionKey == sessionKey); if (user == null) { throw new ArgumentNullException("Not found user with that sessionkey"); } user.SessionKey = null; context.Entry(user).State = System.Data.EntityState.Modified; context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; }//end using }//end validstate else { var errors = String.Join(" ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); var errorMessage = string.Format("User input validation failed. Errors: {0}", errors); throw new ArgumentException(errorMessage); } }); return responseMsg; }