// This is post, but only returns a value for login information. Not really a create that makes a new object /// <summary> /// Needs username, password and permission to work. Returns a object if the object exists. /// </summary> /// <param name="loginDetails"></param> /// <returns></returns> public async Task <string> GetLoginRoleAPI(organizerlogin loginDetails) { try { HttpClient client = new HttpClient(); // Using jsonconvert and creates content string jsonString = JsonConvert.SerializeObject(loginDetails); // Lägg in ny objekt var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); // URL vart datan ska skickas string URL = loginBaseURL + loginRole; // Connecting webapi var response = await client.PostAsync(URL, content); if (response.IsSuccessStatusCode) { var responseString = await response.Content.ReadAsStringAsync(); loginDetails = JsonConvert.DeserializeObject <organizerlogin>(responseString); return(loginDetails.permission); // Return the permission } return(null); } catch (Exception e) { Logger.Error(e, "Error Level"); return(null); } }
public async System.Threading.Tasks.Task <ActionResult> Login(organizerlogin loginDetails) { LoginHandler handler = new LoginHandler(); try { // Get admin permission first. permission must be sent same time, otherwise API wont accept it. loginDetails.permission = "organizeradmin"; // Replace old values loginDetails = await handler.UserAuthorized(loginDetails); // If invalid the role is liekly null and the password, username can be wrong. Alternativley the service can be down. if (loginDetails != null) { if (loginDetails.permission != null && loginDetails.permission.Equals("organizer")) { Session["userRole"] = loginDetails.permission; Session["userID"] = loginDetails.Id; return(RedirectToAction("Index", "Organizer")); } // Different redirect than user else if (loginDetails.permission != null && loginDetails.permission.Equals("organizeradmin")) { Session["userRole"] = loginDetails.permission; return(RedirectToAction("Index", "Admin")); } else { TempData["tempErrorMessage"] = "Password or username is wrong"; return(RedirectToAction("Login", "Home")); } } else { // Remove session just in case RemoveAllSessions(); } TempData["tempErrorMessage"] = "Password or username is wrong"; return(RedirectToAction("Login", "Home")); } // Redirect user to Home if username and password does not match catch (Exception e) { TempData["tempErrorMessage"] = e.Message; return(RedirectToAction("Login", "Home")); } }
public async Task <organizerlogin> UserAuthorized(organizerlogin loginDetails) { ObjectHandlerJSON obj = new ObjectHandlerJSON(); organizerlogin details = new organizerlogin(); try { // Checks if user is admin first loginDetails.permission = adminRole; string role = await obj.GetLoginRoleAPI(loginDetails); // If admin details.permission = role; // If user is no admin, but can be a user if (role == null) { // If API returns null for normal user, it will use our database to check if user exists. List <Organizer> organizers = await obj.GetOrganizerList(); foreach (var item in organizers) { if (item.Name == loginDetails.username) { details.username = item.Name; details.Id = item.Id; details.permission = userRole; } } } // If all goes well return(details); } catch { return(null); } }