public HttpResponseMessage PostIsLoggedIn(LoginCredentials login) { var loginReturnStatus = new LoginReturnStatus(); HttpResponseMessage response; if (User.Identity.IsAuthenticated) { AttendeesResult attendeesResultFull = AttendeesManager.I.Get(new AttendeesQuery { Username = User.Identity.Name, CodeCampYearId = Utils.CurrentCodeCampYear, IncludeAttendeesCodeCampYearResult = true }).FirstOrDefault(); if (attendeesResultFull != null) { //var attendeesResult = AttendeesResultStripped(attendeesResultFull); loginReturnStatus.Data = attendeesResultFull; response = Request.CreateResponse(HttpStatusCode.OK, MakeSafeAttendee(attendeesResultFull)); } else { response = Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User Authenticated, but no user record in database found."); } } else { response = Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User Not Authenticated To Server"); loginReturnStatus.Status = "Failed"; loginReturnStatus.Message = "Not Authenticated"; } return response; }
public async Task<HttpResponseMessage> PostFormData() { int attendeesId = -1; // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } //Write to File //string root = HttpContext.Current.Server.MapPath("~/App_Data"); //var provider = new MultipartFormDataStreamProvider(root); //Write to Memory var provider = new MultipartMemoryStreamProvider(); try { // Read the form data and return an async task. await Request.Content.ReadAsMultipartAsync(provider); /* THIS WORKS WITH MultipartMemoryStreamProvider UNCOMMENTED ABOVE */ int bytesUpoaded = -1; using (var memoryStream = new MemoryStream()) { foreach (var item in provider.Contents) { using (Stream stream = item.ReadAsStreamAsync().Result) { if (stream != null) { //Convert Stream to Bytes or something var bytes = new byte[stream.Length]; stream.Read(bytes, 0, (int) stream.Length); memoryStream.Write(bytes, 0, (int) stream.Length); } } } //create new Bite Array Log4NetAllManager.I.Insert(new Log4NetAllResult() { Date = DateTime.Now.AddHours(-3), EllapsedTime = 0, ExceptionMessage = "", ExceptionStackTrace = "", Level = "", Logger = "", Message = "AccountController:PostFormData length: " + memoryStream.Length, MessageLine1 = "", Thread = "", }); var byteArray = new byte[memoryStream.Length]; //Set pointer to the beginning of the stream memoryStream.Position = 0; //Read the entire stream memoryStream.Read(byteArray, 0, (int) memoryStream.Length); if (User.Identity.IsAuthenticated) { var attendeesResult = AttendeesManager.I.Get(new AttendeesQuery { Username = User.Identity.Name }).FirstOrDefault(); if (attendeesResult != null) { attendeesId = attendeesResult.Id; attendeesResult.UserImage = new System.Data.Linq.Binary(byteArray); bytesUpoaded = byteArray.Count(); AttendeesManager.I.Update(attendeesResult); } } } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new LoginReturnStatus() { AttendeeId = attendeesId, Success = true, Status = "success", File = "speaker.jpg" , Message = "bytes uploaded: " + bytesUpoaded.ToString() }); return response; } catch (System.Exception e) { var ret = new LoginReturnStatus() { Success = false, Status = "Failure", File = "speaker.jpg", Message = e.ToString() }; return Request.CreateResponse(HttpStatusCode.Forbidden, ret); } }
public HttpResponseMessage PostLogin(LoginCredentials login) { //if (!ModelState.IsValid) //{ // // throw error (ivalidateable object) // // throw httpresponse exce. // // webdev blog // // webapi pipeline // // tracing system? nuget webapi system diagnostics trace // // attribute routing.org //} var loginReturnStatus = new LoginReturnStatus(); HttpResponseMessage response; if (!String.IsNullOrEmpty(login.Username) && !String.IsNullOrEmpty(login.Password)) { var loginSuccess = Membership.ValidateUser(login.Username, login.Password); if (loginSuccess) { FormsAuthentication.SetAuthCookie(login.Username, login.RememberMe); AttendeesResult attendeesResultFull = AttendeesManager.I.Get(new AttendeesQuery() { CodeCampYearId = Utils.CurrentCodeCampYear, IncludeAttendeesCodeCampYearResult = true, Username = login.Username }).FirstOrDefault(); if (attendeesResultFull != null) { response = Request.CreateResponse(HttpStatusCode.OK, MakeSafeAttendee(attendeesResultFull)); } else { response = Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User Authenticated, but no user record in database found."); } } else { response = Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Username and Password are not valid. Please Try again"); } } else { response = Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Username and Password must both have values"); loginReturnStatus.Status = "Failed"; loginReturnStatus.Message = "Username and Password must both have values"; } return response; }