public IActionResult Login([FromBody] LoginModel2 loginModel) { if (string.IsNullOrEmpty(loginModel.email) || string.IsNullOrEmpty(loginModel.password)) { return(Unauthorized()); } var account = _context.Account.FirstOrDefault(a => a.EMail == loginModel.email); if (account == null || account == default(Account)) { return(Unauthorized()); } else { string preHashText = loginModel.password.Trim() + account.Salt; string hashString = COCOPASS.Helper.NetCoreHelper.Encrypt.ToHMACSHA256HashString(preHashText); if (!hashString.Equals(account.Password)) { return(Unauthorized()); } } var token = new JwtTokenBuilder() .AddSubject("ZeYu") .AddSecurityKey(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["AuthenticationToken:SigningKey"]))) .AddIssuer(Configuration["AuthenticationToken:Issuer"]) .AddAudience(Configuration["AuthenticationToken:Audience"]) .AddClaim("MembershipId", account.ID) .AddExpiry(2) .Build(); return(Ok(token)); }
public ActionResult Login(LoginModel2 model) { if (!ModelState.IsValid) //if (model.CaptchaCodeText != Convert.ToString(Session["Captcha"])) //check if input == to captcha shown { if (Session["time"] == null) { model.CapImage = "data:image/png;base64," + Convert.ToBase64String(new Utility().VerificationTextGenerator()); model.CapImageText = Convert.ToString(Session["Captcha"]); return(View("Sample", model)); } else { time = (int)Session["time"]; time++; Session["time"] = time; Response.Write("Wrong times: " + time.ToString()); if (time >= 3) { model.CapImage = "data:image/png;base64," + Convert.ToBase64String(new Utility().VerificationTextGenerator()); model.CapImageText = Convert.ToString(Session["Captcha"]); return(View("Sample", model));//Returns the view with the input values so that the user doesn't have to retype again } } //Returns the view with the input values so that the user doesn't have to retype again /*if (Session["time"] == null) * { * Session["time"] = time + 1; * Response.Write("Wrong times: 1"); * } * else * { * time = (int)Session["time"]; * time++; * Session["time"] = time; * Response.Write("Wrong times: " + time.ToString()); * if (time >= 3) * { * model.CapImage = "data:image/png;base64," + Convert.ToBase64String(new Utility().VerificationTextGenerator()); * model.CapImageText = Convert.ToString(Session["Captcha"]); * * return View("Sample", model);//Returns the view with the input values so that the user doesn't have to retype again * * } * }*/ return(View()); } else { //MainDbContext db = new MainDbContext(); using (var db = new MainDbContext()) { var usernameCheck = db.Users.FirstOrDefault(u => u.Username == model.Users.Username); var getPassword = db.Users.Where(u => u.Username == model.Users.Username).Select(u => u.Password); var materializePassword = getPassword.ToList(); if (materializePassword.Count != 0) { var password = materializePassword[0]; var decryptedPassword = CustomDecrypt.Decrypt(password); if (model.Users.Username != null && model.Users.Password == decryptedPassword) { var searchrole = db.Role.Where(u => u.Id == usernameCheck.Key_Role).Select(u => u.RoleType); var materializeRole = searchrole.ToList(); var role = materializeRole[0]; var ctx = Request.GetOwinContext(); var authManager = ctx.Authentication; if (role == "doctor") { var checkname = db.Doctor.FirstOrDefault(u => u.Key_Users == usernameCheck.Id); var getname = db.Doctor.Where(u => u.Id == checkname.Id).Select(u => u.FirstName); var materializeName = getname.ToList(); var name = materializeName[0]; var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, name), new Claim(ClaimTypes.Role, role) }, "ApplicationCookie"); authManager.SignIn(identity); return(RedirectToAction("Index", "Doctor")); } else if (role == "FDR") { var checkname = db.FDR.FirstOrDefault(u => u.Key_Users == usernameCheck.Id); //var getname = db.PersonInfo.Where(u => u.Id == checkname.Key_PersonInfo).Select(u => u.FirstName); var getname = db.FDR.Select(u => u.FirstName); var materializeName = getname.ToList(); var name = materializeName[0]; var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, name), new Claim(ClaimTypes.Role, role) }, "ApplicationCookie"); authManager.SignIn(identity); return(RedirectToAction("Index", "FDR")); } else if (role == "nurse") { /*var checkname = db.Nurse.FirstOrDefault(u => u.Key_Users == usernameCheck.Id); * var getname = db.PersonInfo.Where(u => u.Id == checkname.Key_PersonInfo).Select(u => u.FirstName); * var materializeName = getname.ToList(); * var name = materializeName[0];*/ var identity = new ClaimsIdentity(new[] { //new Claim(ClaimTypes.Name, name), new Claim(ClaimTypes.Role, role) }, "ApplicationCookie"); authManager.SignIn(identity); return(RedirectToAction("Index", "Nurse")); } else if (role == "administrator") { /*var checkname = db.Admin.FirstOrDefault(u => u.Key_Users == usernameCheck.Id); * var getname = db.PersonInfo.Where(u => u.Id == checkname.Key_PersonInfo).Select(u => u.FirstName); * var materializeName = getname.ToList(); * var name = materializeName[0];*/ var identity = new ClaimsIdentity(new[] { //new Claim(ClaimTypes.Name, name), new Claim(ClaimTypes.Role, role) }, "ApplicationCookie"); authManager.SignIn(identity); return(RedirectToAction("Index", "Admin")); } } if (Session["time"] == null) { Session["time"] = time + 1; Response.Write("Wrong times: 1"); } else { time = (int)Session["time"]; time++; Session["time"] = time; Response.Write("Wrong times: " + time.ToString()); if (time >= 3) { model.CapImage = "data:image/png;base64," + Convert.ToBase64String(new Utility().VerificationTextGenerator()); model.CapImageText = Convert.ToString(Session["Captcha"]); return(View("Sample", model));//Returns the view with the input values so that the user doesn't have to retype again } } } else { if (Session["time"] == null) { Session["time"] = time + 1; Response.Write("Wrong times: 1"); } else { time = (int)Session["time"]; time++; Session["time"] = time; Response.Write("Wrong times: " + time.ToString()); if (time >= 3) { model.CapImage = "data:image/png;base64," + Convert.ToBase64String(new Utility().VerificationTextGenerator()); model.CapImageText = Convert.ToString(Session["Captcha"]); return(View("Sample", model));//Returns the view with the input values so that the user doesn't have to retype again } } //model.CapImage = "data:image/png;base64," + Convert.ToBase64String(new Utility().VerificationTextGenerator()); //model.CapImageText = Convert.ToString(Session["Captcha"]); ModelState.AddModelError("", "Invalid username or password"); //Should always be declared on the end of an action method } return(View(model)); } } }