using System.Web.Mvc; using System.Web.UI; using Neo.Afx.Security; using System; using System.Configuration; using System.Web.Profile; using System.Linq; using Neo.LegitimateLicences.Common; using System.Web.Security; using Neo.Afx.Services.Audits; using Neo.LegitimateLicences.Common.ViewModels; using Neo.Afx.Admin.Security; using DPUruNet; using System.Collections.Generic; namespace Neo.Afx.Test.WebConsole.Controllers.Auth { // Don't inherit from Controller // as it has the Authorize attribute public partial class AuthController : Controller { [AllowAnonymous] [OutputCache(Location = OutputCacheLocation.None)] public ActionResult Login() { return RedirectToAction("SignIn"); } [AllowAnonymous] [OutputCache(Location = OutputCacheLocation.None)] public ActionResult SignIn() { ViewBag.Controller = "Authentication"; ViewBag.FingerprintAuthenticationEnabled = ConfigurationManager.AppSettings["FingerprintAuthenticationEnabled"].ToString().ToLower() == "true"; return View("SignIn", new ResponsiveAppBaseModel()); } [AllowAnonymous] [OutputCache(Location = OutputCacheLocation.None)] public ActionResult CaptureFingerPrint(string token) { ViewBag.Controller = "Authentication"; var database = new Neo.Afx.Admin.Security.SecurityDatabase(); ViewBag.FingerprintAuthenticationEnabled = ConfigurationManager.AppSettings["FingerprintAuthenticationEnabled"].ToString().ToLower() == "true"; var viewModel = new ResponsiveAppBaseModel(); viewModel.Token = token; var validation = database.ValidateFingerPrintRequest(token); if (!String.IsNullOrEmpty(validation)) { return View("InvalidToken", viewModel); } return View("CaptureFingerPrint", viewModel); } [OutputCache(Location = OutputCacheLocation.None)] public ActionResult SignOut() { DomainAuthenticationProvider.Signout(); return RedirectToAction("SignIn", "Auth"); } #region JSON endpoints #region SignInUser // POST: /Home/SignInUser [HttpPost] [AllowAnonymous] public JsonResult SignInUser(string userName, string password, string fingerprint) { bool success = false; var returnUrl = DomainAuthenticationProvider.DefaultUrl; if (!Profile.IsAnonymous) { return Json(new { success, returnUrl = returnUrl, passwordExpired = false, alreadyLoggedIn = true }); } if (ConfigurationManager.AppSettings["FingerprintAuthenticationEnabled"].ToString().ToLower() == "true") { if (!string.IsNullOrEmpty(fingerprint)) { success = DomainAuthenticationProvider.ValidateUser(fingerprint, false, ref userName); } } else { success = DomainAuthenticationProvider.ValidateUser(userName, password, false); } if (success) { Session["DateLoggedIn"] = DateTime.Now; var profile = new UserProfile(); profile = (UserProfile)ProfileBase.Create(userName, success); if (profile != null) { var auditDb = new AuditsDatabase(); auditDb.WriteEntry(profile.UserID, "Application", "User logged in", null, false, false, 1 /*user*/, profile.UserID); //if (profile.Roles.Count() == 1 && profile.Roles.First() == 1004 /*verifier*/) //{ // returnUrl = ConfigurationManager.AppSettings["BaseUrl"] + "#Afx/Verification"; //} //returnUrl = (HttpContext.Request.Cookies["LastPage"] != null) ? ConfigurationManager.AppSettings["BaseUrl"] + HttpContext.Request.Cookies["LastPage"].Value : ConfigurationManager.AppSettings["BaseUrl"]; } } return Json(new { success, returnUrl = returnUrl, passwordExpired = false, alreadyLoggedIn = false }); } #endregion #region SaveFingerPrintTemplate [HttpPost] [AllowAnonymous] public JsonResult SaveFingerPrintTemplate(List fingerPrints, string token) { var database = new SecurityDatabase(); var errorMessage = ""; List preenrollmentFmds = new List(); bool success = false; var validation = database.ValidateFingerPrintRequest(token); if (!String.IsNullOrEmpty(validation)) { success = false; return Json(new { success, returnUrl = DomainAuthenticationProvider.DefaultUrl }); } var user = database.GetUserByRequestToken(token); foreach (var fingerPrint in fingerPrints) { byte[] bytes = Convert.FromBase64String(fingerPrint); var fmd = Importer.ImportFmd(bytes, Constants.Formats.Fmd.DP_PRE_REGISTRATION, Constants.Formats.Fmd.DP_PRE_REGISTRATION); preenrollmentFmds.Add(fmd.Data); } DataResult resultEnrollment = DPUruNet.Enrollment.CreateEnrollmentFmd(Constants.Formats.Fmd.DP_REGISTRATION, preenrollmentFmds); if (resultEnrollment.ResultCode == Constants.ResultCode.DP_SUCCESS) { try { database.SaveUserFingerPrint(resultEnrollment.Data.Bytes, user.UserID, (long)Defaults.SystemUserID); database.SaveUserFingerPrintRequest(user.UserID, false, (long)Defaults.SystemUserID); success = true; } catch (Exception ex) { success = false; errorMessage = Neo.Afx.Common.ExceptionHelper.GetInnermostMessage(ex); } } else { errorMessage = "Save failed, please contact the site administrator for more assistance."; } var results = new { ErrorMessage = errorMessage }; return Json(new { success, returnUrl = DomainAuthenticationProvider.DefaultUrl, passwordExpired = false, ErrorMessage = errorMessage }); } #endregion #endregion } }