namespace jury_service.Models { public class LoginModel { [Display(Name = "Username")] [Required] public string username { get; set; } [Display(Name = "Password")] [Required] public string Password { get; set; } //protected readonly WebClient Client; //public LoginModel(WebClient client) //{ // Client = client; //} public FCC_Users infoModel; public FCC_Users info; public List<FCC_Users> infos; public bool DoLogin(LoginModel login) { var ffcuser = GetInfoREcordByName(login.username); //Check if user model pulled back from API is null if (ffcuser != null) { //if not null check to see if it has a password/intial value of '1'... if (!String.IsNullOrEmpty(ffcuser.Secret)) { if (ffcuser.Secret == login.Password) { //Check if user login is initial login... if (ffcuser.Initial.Equals(1)) { return true; } } } else { return false; } } return false; } //public bool DoLogin() //{ // if (Initial == "1" && Password != "null") // { // return true; // } // return false; //} //public User GetUser(LoginModel login) //{ // try // { // //Add Request Headers // Client.Headers.Add(HttpRequestHeader.ContentType, "application/json"); // //Make Request to API and return results // return JsonConvert.DeserializeObject<User>(Client.DownloadString(new Uri(Properties.Settings.Default.Api_Url + "Info" + "/?username=" + login.username + "/"))); // } // catch (Exception ex) // { // return null; // } //} public FCC_Users GetInfoREcordByName(string infoName) { WebClient client = new WebClient(); string url = Properties.Settings.Default.Api_Url + "Info/?infoName=" + infoName; info = JsonConvert.DeserializeObject<FCC_Users>(client.DownloadString(url)); return info; } public FCC_Users GetAllInfoRecords(string infoName) { WebClient client = new WebClient(); string url = Properties.Settings.Default.Api_Url + "Info"; infos = JsonConvert.DeserializeObject<List<FCC_Users>>(client.DownloadString(url)); return infos.Where(x => x.Username == infoName).First() } } }
namespace jury_service.Controllers { public class FCCLoginController : Controller { //private FCC_Users username; // // GET: /FCCLogin/ public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(LoginModel login) { if (ModelState.IsValid) { var ffcuser = login.GetAllInfoRecords(login.username); //Check to see if user exists... if (ffcuser != null) { //If user exists, check password matches... //If password matches, check if initial login... if (!String.IsNullOrEmpty(ffcuser.Secret)) { if (ffcuser.Secret == login.Password) { //Check if user login is initial login... if (ffcuser.Initial) { return RedirectToAction("CreatePassword", "Newpassword"); } //else // if (ffcuser != username) { ModelState.AddModelError("Error", "Wrong Username and/or Password"); //ModelState.Clear(); } } } return RedirectToAction("fccindex", "FCCLogin"); } } return View(); } public ActionResult fccindex() { return View(); }
Quote:The First<TSource>(IEnumerable<TSource>) method throws an exception if source contains no elements. To instead return a default value when the source sequence is empty, use the FirstOrDefault method.
The First<TSource>(IEnumerable<TSource>) method throws an exception if source contains no elements. To instead return a default value when the source sequence is empty, use the FirstOrDefault method.
return infos.Where(x => x.Username == infoName).First()
return infos.FirstOrDefault(x => x.Username == infoName);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)