public IActionResult Index(LoginViewModel model) { if (!ModelState.IsValid) { return(View(model)); } if (string.IsNullOrWhiteSpace(model.Password)) { ModelState.AddModelError(nameof(model.Password), "رمز عبور را وارد نمایید."); return(View(model)); } var res = baseRepository.ExecuteCommand(conn => conn.Query <Company>("SELECT [Id], [Code], [Name], [Enabled], [Password], [PasswordHash], [PasswordSalt] FROM [TavanirStage].[Basic].[Companies] WHERE [Username] = @Username", new { model.Username }).FirstOrDefault()); if (res == null || res.Id == null || Equals(res.Id, Guid.Empty)) { ModelState.AddModelError(nameof(model.Username), "نام کاربری یافت نشد."); return(View(model)); } if (!Equals(res.Password, model.Password) && !hashingPassword.VerifyPassword(res.PasswordHash, res.PasswordSalt, model.Password)) { ModelState.AddModelError(nameof(model.Password), "رمز عبور صحیح نیست."); return(View(model)); } if (!res.Enabled) { ModelState.AddModelError(string.Empty, "حساب کاربری شما فعال نمیباشد."); return(View(model)); } string companyId = res.Id.ToString(); HttpContext.Session.SetString("CompanyId", companyId); HttpContext.Session.SetString("CompanyCode", res.Code); HttpContext.Session.SetString("CompanyName", res.Name); HttpContext.Session.SetString("DashUrl", string.Concat(configuration.GetSection("DashboardAddress").Value, companyId)); if (HttpContext.Session.HasKey("Code")) { HttpContext.Session.Remove("Code"); } return(Redirect("/Home/Index")); }
private bool CreateHash(string password, string storedPass) { string storedPassword = storedPass; byte[] hashByte = Convert.FromBase64String(storedPassword); HashingPassword hash = new HashingPassword(hashByte); if (hash.VerifyPassword(password)) { return(true); } return(false); }